实验目的:
1.掌握Swing常用的组件的使用;
2.掌握程序界面开发的步骤;
3.掌握事件监听器的使用。
实验内容:
如图1界面,在文本框中输入姓名,选择性别(单选)、爱好(可多选)、未来职业(下拉框),单击“确定”按钮,弹出消息提示框,如图2,单击“退出”按钮结束运行。
(界面代码已给出,请阅读理解,补充事件监听代码)
图1 信息输入界面
需要解决的问题:
- 两个按钮绑定同一个监听器,如何判断用户单击了哪个按钮?
(2)如何判断单选按钮、复选框的选中状态及标题文本的获取?
(3)如何退出Java程序?
源代码:
//Selectframe.java文件:
import javax.swing.*;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.*;
public class Selectframe extends Jframe implements ActionListener { //实现动作监听器窗体
private static final long serialVersionUID = 1L;
private JLabel labName = new JLabel("姓名:");
private JTextField tf = new JTextField(10); //文本框
private JLabel sex = new JLabel("性别:");
private JRadioButton male= new JRadioButton("男");
private JRadioButton female = new JRadioButton("女");
private JLabel labLove = new JLabel("爱好:");
private JCheckBox cbMusic = new JCheckBox("音乐");
private JCheckBox cbSport = new JCheckBox("运动");
private JCheckBox cbWeb = new JCheckBox("上网");
private JLabel labDo = new JLabel("未来职业:");
private String[] labs = {"软件工程师","网络工程师","数据库工程师","其它"};
private JComboBox labCombo = new JComboBox(labs);
private JButton butOk = new JButton("确定");
private JButton butExit = new JButton("退出");
private ButtonGroup bg = new ButtonGroup(); //单选按钮组
private JPanel pan1 = new JPanel(); //创建面板1
private JPanel pan2 = new JPanel();
private JPanel pan3 = new JPanel();
private JPanel pan4 = new JPanel();
private JPanel pan5 = new JPanel();
public Selectframe() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
this.setLayout(new GridLayout(5,1));
this.setTitle("兴趣爱好选择");
this.setSize(500, 300);
this.setLocationRelativeTo(null);
//面板1加入姓名文本框
pan1.add(labName);
pan1.add(tf);
//面板2加入性别单选组
pan2.add(sex);
pan2.add(male);
pan2.add(female);
bg.add(male);
bg.add(female);
//面板3 加入爱好复选框
pan3.add(labLove);
pan3.add(cbMusic);
pan3.add(cbSport);
pan3.add(cbWeb);
//面板4 加入未来职业下拉框
pan4.add(labDo);
pan4.add(labCombo);
//面板5 加入两个按钮
pan5.add(butOk);
pan5.add(butExit);
//把5个中间层窗口添加到主界面
this.add(pan1);
this.add(pan2);
this.add(pan3);
this.add(pan4);
this.add(pan5);
butOk.addActionListener(this);
butExit.addActionListener(this);
}
//完成实现 ActionListener接口,并完成注册监听器.....
//***************begin*****************
public void actionPerformed(ActionEvent e) {
if(e.getSource()==butOk) {
StringBuffer sb=new StringBuffer();
sb.append("我是"+ tf.getText());
sb.append("n性别:");
if(male.isSelected()){
sb.append(male.getText() +" ");
}
if(female.isSelected()){
sb.append(female.getText()+" ");}
sb.append("n爱好:");
if(cbMusic.isSelected()){
sb.append(cbMusic.getText() + " ");}
if(cbSport.isSelected()){
sb.append(cbSport.getText()+" ");}
if(cbWeb.isSelected()){
sb.append(cbWeb.getText()+" ");}
sb.append("n未来职业: ");
if(true){
sb.append(labCombo.getSelectedItem()+" ");
}
JOptionPane.showMessageDialog(this, sb);
}
if(e.getSource()==butExit){
System.exit(0);
}
}
//***************end*****************
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
Selectframe sf= new Selectframe();
sf.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
sf.setVisible(true);
}
}



