Java快速上手之实验六
1. 编写ItemEventDemo.java,当选中或取消选中单选钮、复选钮和列表框时显示所选的结果。
2.编写GUIExample.java,当选中或取消选中单选钮、复选钮时在标签中显示相应结果。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;public class ItemEventDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("选项事件");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(400, 300);frame.setLayout(new FlowLayout());// 创建标签用于显示事件结果JLabel resultLabel = new JLabel("组件事件结果显示在此处");frame.add(resultLabel);// 创建单选按钮JRadioButton radioButton1 = new JRadioButton("radioButton1");JRadioButton radioButton2 = new JRadioButton("radioButton2");JRadioButton radioButton3 = new JRadioButton("radioButton3");ButtonGroup radioGroup = new ButtonGroup();radioGroup.add(radioButton1);radioGroup.add(radioButton2);radioGroup.add(radioButton3);frame.add(radioButton1);frame.add(radioButton2);frame.add(radioButton3);// 添加单选按钮监听器ItemListener radioListener = e -> {if (e.getStateChange() == ItemEvent.SELECTED) {resultLabel.setText("组件事件:" + ((JRadioButton) e.getItem()).getText() + " 被选中");}};radioButton1.addItemListener(radioListener);radioButton2.addItemListener(radioListener);radioButton3.addItemListener(radioListener);// 创建复选框JCheckBox checkBox1 = new JCheckBox("checkBox1");JCheckBox checkBox2 = new JCheckBox("checkBox2");frame.add(checkBox1);frame.add(checkBox2);// 添加复选框监听器ItemListener checkBoxListener = e -> {JCheckBox source = (JCheckBox) e.getItem();if (e.getStateChange() == ItemEvent.SELECTED) {resultLabel.setText("组件事件:" + source.getText() + " 被选中");} else {resultLabel.setText("组件事件:" + source.getText() + " 被取消");}};checkBox1.addItemListener(checkBoxListener);checkBox2.addItemListener(checkBoxListener);// 创建下拉列表框String[] options = {"第一项", "第二项", "第三项"};JComboBox<String> comboBox = new JComboBox<>(options);frame.add(comboBox);// 添加下拉列表监听器comboBox.addItemListener(e -> {if (e.getStateChange() == ItemEvent.SELECTED) {resultLabel.setText("组件事件:" + e.getItem() + " 被选中");}});// 设置窗口可见frame.setVisible(true);}
}2.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class GUIExample {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("个人信息调查");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(600, 400);frame.setLayout(new GridLayout(5, 1));// 年龄组面板JPanel agePanel = new JPanel();agePanel.setBorder(BorderFactory.createTitledBorder("年龄段:"));JRadioButton age1 = new JRadioButton("5~15岁");JRadioButton age2 = new JRadioButton("16~25岁");JRadioButton age3 = new JRadioButton("26~35岁");JRadioButton age4 = new JRadioButton("36~45岁");JRadioButton age5 = new JRadioButton("46~55岁");ButtonGroup ageGroup = new ButtonGroup();ageGroup.add(age1);ageGroup.add(age2);ageGroup.add(age3);ageGroup.add(age4);ageGroup.add(age5);agePanel.add(age1);agePanel.add(age2);agePanel.add(age3);agePanel.add(age4);agePanel.add(age5);// 兴趣爱好面板JPanel hobbyPanel = new JPanel();hobbyPanel.setBorder(BorderFactory.createTitledBorder("兴趣爱好:"));JCheckBox hobby1 = new JCheckBox("上网聊天交友");JCheckBox hobby2 = new JCheckBox("体育/户外健身");JCheckBox hobby3 = new JCheckBox("汽车/购物");JCheckBox hobby4 = new JCheckBox("旅游/度假", true);JCheckBox hobby5 = new JCheckBox("时尚服装化妆品");hobbyPanel.add(hobby1);hobbyPanel.add(hobby2);hobbyPanel.add(hobby3);hobbyPanel.add(hobby4);hobbyPanel.add(hobby5);// 输出结果面板JPanel resultPanel = new JPanel();resultPanel.setBorder(BorderFactory.createTitledBorder("调查的结果为:"));JTextField resultField = new JTextField(40);resultField.setEditable(false);resultPanel.add(resultField);// 按钮面板JPanel buttonPanel = new JPanel();JButton submitButton = new JButton("提交");JButton clearButton = new JButton("清空");buttonPanel.add(submitButton);buttonPanel.add(clearButton);// 添加面板到主窗口frame.add(agePanel);frame.add(hobbyPanel);frame.add(resultPanel);frame.add(buttonPanel);// 事件监听:提交按钮submitButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {StringBuilder result = new StringBuilder("你是一个");// 获取选中的年龄段if (age1.isSelected()) result.append("5~15岁的人");else if (age2.isSelected()) result.append("16~25岁的人");else if (age3.isSelected()) result.append("26~35岁的人");else if (age4.isSelected()) result.append("36~45岁的人");else if (age5.isSelected()) result.append("46~55岁的人");else result.append("未选择年龄段的人");// 获取选中的兴趣爱好result.append(",你比较喜欢");boolean hasHobby = false;if (hobby1.isSelected()) {result.append("上网聊天交友");hasHobby = true;}if (hobby2.isSelected()) {if (hasHobby) result.append("、");result.append("体育/户外健身");hasHobby = true;}if (hobby3.isSelected()) {if (hasHobby) result.append("、");result.append("汽车/购物");hasHobby = true;}if (hobby4.isSelected()) {if (hasHobby) result.append("、");result.append("旅游/度假");hasHobby = true;}if (hobby5.isSelected()) {if (hasHobby) result.append("、");result.append("时尚服装化妆品");}if (!hasHobby) result.append("暂无兴趣爱好");result.append("。");// 显示结果resultField.setText(result.toString());}});// 事件监听:清空按钮clearButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {ageGroup.clearSelection();hobby1.setSelected(false);hobby2.setSelected(false);hobby3.setSelected(false);hobby4.setSelected(false);hobby5.setSelected(false);resultField.setText("");}});// 显示窗口frame.setVisible(true);}
}