java基础之Swing技术
实现一个用户注册界面
package sah;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
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.SwingConstants;
public class RegistTest extends JFrame implements ActionListener {
private JLabel jl_name = new JLabel("用户名:");
private JLabel jl_secrete = new JLabel("请输入初始密码:");
private JLabel jl_secrete2 = new JLabel("请再次输入密码:");
private JLabel jl_sex = new JLabel("性别:");
private JLabel jl_hobit = new JLabel("兴趣爱好:");
private JLabel jl_grade = new JLabel("学历:");
private JLabel jl_title = new JLabel("欢迎您的加入");
private JLabel jl_message = new JLabel("请填写用户信息");
private JButton ok = new JButton("确定");
private JButton clean = new JButton("清空");
private JTextField text_name = new JTextField(20);
private JPasswordField text_secrete = new JPasswordField(20);
private JPasswordField text_secrete2 = new JPasswordField(20);
private JRadioButton jrb_man = new JRadioButton("男",true);
private JRadioButton jrb_woman = new JRadioButton("女");
/*private JCheckBox jcb_read = new JCheckBox("阅读");
private JCheckBox jcb_swim = new JCheckBox("游泳");
private JCheckBox jcb_travel = new JCheckBox("旅游");
private JCheckBox jcb_other = new JCheckBox("其他");*/
private String[] hobit = {"阅读","游泳","旅游","其他"};
private JCheckBox[] jcb_hobit = new JCheckBox[hobit.length];
private JComboBox jcb_grade = new JComboBox();
private JPanel jp_hobit = new JPanel();
private JPanel jp_sex = new JPanel();
private String[] grade = {"高中","专科","本科","硕士研究生","博士研究生"};
public RegistTest() {
// TODO Auto-generated constructor stub
super("用户注册窗口");
init();
}
public void init() {
setSize(480,300);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(jl_title,BorderLayout.NORTH);
JPanel jp = new JPanel();
JPanel jp1 = new JPanel();
jcb_grade = new JComboBox(grade);
jp_sex.add(jrb_man);
jp_sex.add(jrb_woman);
for(int i =0;i<hobit.length;i++) {
jcb_hobit[i] = new JCheckBox(hobit[i]);
jp_hobit.add(jcb_hobit[i]);
}
ButtonGroup bg1 = new ButtonGroup();
ButtonGroup bg2 = new ButtonGroup();
bg1.add(jrb_man);
bg1.add(jrb_woman);
jp.setLayout(new GridLayout(6,2));
jp.add(jl_name);
jl_name.setHorizontalAlignment(SwingConstants.RIGHT);
jl_secrete.setHorizontalAlignment(SwingConstants.RIGHT);
jl_secrete2.setHorizontalAlignment(SwingConstants.RIGHT);
jl_sex.setHorizontalAlignment(SwingConstants.RIGHT);
jl_hobit.setHorizontalAlignment(SwingConstants.RIGHT);
jl_grade.setHorizontalAlignment(SwingConstants.RIGHT);
jl_title.setHorizontalAlignment(SwingConstants.CENTER);
jp.add(text_name);
jp.add(jl_secrete);
jp.add(text_secrete);
jp.add(jl_secrete2);
jp.add(text_secrete2);
jp.add(jl_sex);
jp.add(jp_sex);
jp.add(jl_hobit);
jp.add(jp_hobit);
jp.add(jl_grade);
jp.add(jcb_grade);
jp1.add(ok);
jp1.add(clean);
add(jp,BorderLayout.CENTER);
add(jp1,BorderLayout.SOUTH);
jp.setBorder(BorderFactory.createTitledBorder("请填写用户注册信息"));
ok.addActionListener(this);
clean.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
StringBuffer sb = new StringBuffer();
// TODO Auto-generated method stub
if(e.getSource().equals(ok)) {
String name = text_name.getText().trim();
if(name.equals("")) {
JOptionPane.showMessageDialog(this, "用户名不能为空,请重新输入");
text_name.requestFocus(true);
return;
}else {
sb.append("用户名:").append(name);
}
String psw = new String(text_secrete.getPassword());
String repsw = new String(text_secrete2.getPassword());
if(!psw.equals(repsw)) {
JOptionPane.showMessageDialog(this, "两次输入的密码不一致");
text_secrete.requestFocus(true);
return;
}else {
sb.append(";\n密码:").append(psw);
}
if(psw.equals("") ) {
JOptionPane.showMessageDialog(this, "密码不能为空,请重新输入");
text_secrete.requestFocus(true);
return;
}else {
sb.append(";\n密码:").append(psw);
}
String sex = jrb_man.isSelected() ?jrb_man.getText():jrb_woman.getText();
sb.append(";\n性别:").append(sex).append(";\n兴趣爱好:");
for(int i =0;i<hobit.length;i++) {
sb.append(jcb_hobit[i].isSelected()?hobit[i]+",":" ");
}
String degree = grade[jcb_grade.getSelectedIndex()];
sb.append(";\n学历:").append(degree);
JOptionPane.showMessageDialog(this, sb.toString());
}
if(e.getSource().equals(clean)) {
text_name.setText("");
text_secrete.setText("");
text_secrete2.setText("");
jrb_man.setSelected(true);
for(int i=0;i<grade.length;i++) {
jcb_hobit[i].setSelected(false);
}
}
}
public static void main(String[] args) {
RegistTest rst = new RegistTest();
}
}
运行结果如下