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);}
}