《Java注冊(cè)界面設(shè)計(jì).doc》由會(huì)員分享,可在線閱讀,更多相關(guān)《Java注冊(cè)界面設(shè)計(jì).doc(5頁珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
.
Java注冊(cè)界面設(shè)計(jì)
package test;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;
import java.util.Vector;
public class Register_GUI {
public Register_GUI() {
RegisterFrame rf = new RegisterFrame();
rf.setVisible(true);
}
public static void main(String args[]) {
new Register_GUI();
}
}
class RegisterFrame extends JFrame { // 框架類
/**
*
*/
private static final long serialVersionUID = -3779096743730354383L;
private Toolkit tool;
public RegisterFrame() {
setTitle("用戶注冊(cè)");
tool = Toolkit.getDefaultToolkit();
Dimension ds = tool.getScreenSize();
int w = ds.width;
int h = ds.height;
setBounds((w - 300) / 2, (h - 300) / 2, 300, 300);
setResizable(false);
RegisterPanel rp = new RegisterPanel(this);
add(rp);
}
}
class RegisterPanel extends JPanel implements ActionListener { // 容器類
/**
*
*/
private static final long serialVersionUID = -
7078727217525013349L;
private JLabel titlelabel, namelabel, pwdlabel1, pwdlabel2, sexlabel,
agelabel, classlabel;
private JTextField namefield, agefield;
private JPasswordField pwdfield1, pwdfield2;
private JButton commitbtn, resetbtn, cancelbtn;
private JRadioButton rbtn1, rbtn2;
private JComboBox combo;
private Vector
v;
private GridBagLayout gbl;
private GridBagConstraints gbc;
private JPanel panel;
private Box box;
private JFrame iframe;
RegisterPanel(JFrame frame) {
iframe = frame;
titlelabel = new JLabel("用戶注冊(cè)");
titlelabel.setFont(new Font("華文彩云", Font.BOLD, 24));
namelabel = new JLabel("用 戶 名:");
pwdlabel1 = new JLabel("密 碼:");
pwdlabel2 = new JLabel("確認(rèn)密碼:");
sexlabel = new JLabel("性 別:");
agelabel = new JLabel("年 齡:");
classlabel = new JLabel("所屬班級(jí):");
namefield = new JTextField(16);
pwdfield1 = new JPasswordField(16);
pwdfield1.setEchoChar(*);
pwdfield2 = new JPasswordField(16);
pwdfield2.setEchoChar(*);
agefield = new JTextField(16);
rbtn1 = new JRadioButton("男");
rbtn2 = new JRadioButton("女");
rbtn1.setSelected(true);
ButtonGroup bg = new ButtonGroup();
bg.add(rbtn1);
bg.add(rbtn2);
v = new Vector();
v.add("ACCP1");
v.add("ACCP2");
v.add("軟件開發(fā)");
v.add("網(wǎng)絡(luò)編程");
v.add("計(jì)算機(jī)應(yīng)用");
combo = new JComboBox(v);
commitbtn = new JButton("注冊(cè)");
commitbtn.addActionListener(this);
resetbtn = new JButton("重置");
resetbtn.addActionListener(this);
cancelbtn = new JButton("取消");
cancelbtn.addActionListener(this);
panel = new JPanel();
panel.add(rbtn1);
panel.add(rbtn2);
Border border = BorderFactory.createTitledBorder("");
panel.setBorder(border);
box = Box.createHorizontalBox();
box.add(commitbtn);
box.add(Box.createHorizontalStrut(30));
box.add(resetbtn);
box.add(Box.createHorizontalStrut(30));
box.add(cancelbtn);
gbl = new GridBagLayout();
setLayout(gbl);
gbc = new GridBagConstraints();
addCompnent(titlelabel, 0, 0, 4, 1);
add(Box.createVerticalStrut(20));
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
gbc.weighty = 100;
addCompnent(namelabel, 0, 1, 1, 1);
addCompnent(namefield, 1, 1, 4, 1);
addCompnent(pwdlabel1, 0, 2, 1, 1);
addCompnent(pwdfield1, 1, 2, 4, 1);
addCompnent(pwdlabel2, 0, 3, 1, 1);
addCompnent(pwdfield2, 1, 3, 4, 1);
addCompnent(sexlabel, 0, 4, 1, 1);
addCompnent(panel, 1, 4, 1, 1);
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
addCompnent(agelabel, 2, 4, 1, 1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addCompnent(agefield, 3, 4, 2, 1);
addCompnent(classlabel, 0, 5, 4, 1);
addCompnent(combo, 1, 5, 4, 1);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
addCompnent(box, 0, 6, 4, 1);
}
public void addCompnent(Component c, int x, int y, int w, int h) {
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
add(c, gbc);
}
public void actionPerformed(ActionEvent e) {
Register rinfo = new Register();
if (e.getSource() == commitbtn) {
rinfo.name = namefield.getText().trim();
rinfo.password = new String(pwdfield1.getPassword());
rinfo.sex = rbtn1.isSelected() ? "男" : "女";
rinfo.age = agefield.getText().trim();
rinfo.nclass = combo.getSelectedItem().toString();
if (rinfo.name.length() == 0) {
JOptionPane.showMessageDialog(null, "\t 用戶名不能為空");
return;
}
if (rinfo.password.length() == 0) {
JOptionPane.showMessageDialog(null, "\t 密碼不能為空 ");
return;
}
if (!rinfo.password.equals(new String(pwdfield2.getPassword()))) {
JOptionPane.showMessageDialog(null, "密碼兩次輸入不一致,請(qǐng)重新輸入");
return;
}
if (rinfo.age.length() == 0) {
JOptionPane.showMessageDialog(null, "\t 年齡不能為空");
return;
}
int age = Integer.parseInt(rinfo.age);
if (age <= 0 || age > 100) {
JOptionPane.showMessageDialog(null, "\t 年齡輸入不合法");
return;
}
JOptionPane.showMessageDialog(null, "\t 注冊(cè)成功!" +" \n 姓名:"+rinfo.name+" \n 性別:"+rinfo.sex+"\n 年齡:"+rinfo.age+"\n 班級(jí): "+rinfo.nclass);
}
if (e.getSource() == resetbtn) {
namefield.setText("");
pwdfield1.setText("");
pwdfield2.setText("");
rbtn1.isSelected();
agefield.setText("");
combo.setSelectedIndex(0);
}
if (e.getSource() == cancelbtn) {
iframe.dispose();
}
}
}
class Register {
String name;
String password;
String sex;
String age;
String nclass;
}
精選word范本!
鏈接地址:http://zhongcaozhi.com.cn/p-5415401.html