我們知道下拉選單元件JComboBox可以選擇列表中的其中一項並展示在下拉選單框中。而選擇了列表專案要讓程式執行什麼動作,這就要用到監聽:addItemListener(item的改變事件),具體是怎麼操作的呢?今天我們就來一起做一個範例,一步一步的構建這個介面的元件和它們的監聽事件
1
首先設計一下程式介面,包括一個JFrame視窗,視窗中有一個JComboBox下拉選單,一個文字輸入框,新增按鈕,刪除按鈕,和資訊展示區域
3
編寫新增按鈕和刪除按鈕的監聽類和對應的監聽處理方法。
加完之後,點選按鈕和選擇下拉選單就會有對應的操作了。
4
最後附上程式原始碼,複製程式碼到eclipse就可以直接執行,歡迎大家交流討論。source:package swing;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;/**?*綜合練習:建立一個星座選擇器:?*下拉選單可以選擇自己的星座、增加星座和刪除星座?*/public class Demo21_StarSelector {private JPanel panel=new JPanel();private JComboBox cmb=new JComboBox();? ? //建立JComboBoxprivate JLabel label=new JLabel("新增新星座:");private JLabel showInfo=new JLabel();? ? //用於顯示資訊private JTextField jtf=new JTextField(16);? ? //用於輸入資訊private JButton buttonAdd=new JButton("新增");private JButton buttonDel=new JButton("刪除");public Demo21_StarSelector() {showInfo.setFont(new Font("宋體",1,25));showInfo.setPreferredSize(new Dimension(500, 50));JFrame frame=new JFrame("選擇你的星座");cmb.addItem("--請選擇--");? ? //向下拉選單中新增一項cmb.addItem("巨蟹座");cmb.addItem("獅子座");cmb.addItem("雙魚座");panel.add(cmb);panel.add(label);panel.add(jtf);panel.add(buttonAdd);panel.add(buttonDel);panel.add(showInfo);frame.add(panel);buttonAdd.addActionListener(new MyActionListener(this));? ? //「新增」按鈕的事件buttonDel.addActionListener(new MyActionListener(this));? ? //「刪除」按鈕的事件cmb.addItemListener(new MyItemListener(this));? ? //下拉選單的事件SwingUtils.setCenter(frame);//設定表單大小600*800並居中frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {new Demo21_StarSelector();}public JLabel getShowInfo() {return showInfo;}public JPanel getPanel() {return panel;}public JTextField getJtf() {return jtf;}public JComboBox getCmb() {return cmb;}}//監聽選中事件class MyItemListener implements ItemListener{private Demo21_StarSelector demo21_StarSelector;public MyItemListener(Demo21_StarSelector demo21_StarSelector) {this.demo21_StarSelector = demo21_StarSelector;}@Overridepublic void itemStateChanged(ItemEvent e){JPanel panel = demo21_StarSelector.getPanel();JLabel showInfo = demo21_StarSelector.getShowInfo();String str=e.getItem().toString();panel.add(showInfo);showInfo.setText("您選擇的星座是:"+str);}}//監聽新增和刪除按鈕事件class MyActionListener implements ActionListener{private Demo21_StarSelector demo21_StarSelector;public MyActionListener(Demo21_StarSelector demo21_StarSelector) {this.demo21_StarSelector = demo21_StarSelector;}@Overridepublic void actionPerformed(ActionEvent e){JTextField jtf = demo21_StarSelector.getJtf();JComboBox cmb = demo21_StarSelector.getCmb();JPanel panel = demo21_StarSelector.getPanel();JLabel showInfo = demo21_StarSelector.getShowInfo();String command=e.getActionCommand();//新增按鈕處理if(command.equals("新增")){if(jtf.getText().length()!=0){cmb.addItem(jtf.getText());? ? //新增項panel.add(showInfo);showInfo.setText("新增成功,新增了:"+jtf.getText());}else{panel.add(showInfo);showInfo.setText("請輸入要新增星座");}}//刪除按鈕處理if(command.equals("刪除")){if(cmb.getSelectedIndex()!=-1){//先獲得要刪除的項的值String strDel=cmb.getSelectedItem().toString();if(strDel.contentEquals("--請選擇--")) {showInfo.setText("請選擇要刪除的星座");}else {cmb.removeItem(strDel);? ? //刪除項panel.add(showInfo);showInfo.setText("刪除成功,刪除了:"+strDel);}}else{panel.add(showInfo);showInfo.setText("請選擇要刪除的星座");}}}}g.JPanel;import javax.swing.JTextField;/**?*綜合練習:建立一個星座選擇器:?*下拉選單可以選擇自己的星座、增加星座和刪除星座?*/public class Demo21_StarSelector {private JPanel panel=new JPanel();private JComboBox cmb=new JComboBox();? ? //建立JComboBoxprivate JLabel label=new JLabel("新增新星座:");private JLabel showInfo=new JLabel();? ? //用於顯示資訊private JTextField jtf=new JTextField(16);? ? //用於輸入資訊private JButton buttonAdd=new JButton("新增");private JButton buttonDel=new JButton("刪除");public Demo21_StarSelector() {showInfo.setFont(new Font("宋體",1,25));showInfo.setPreferredSize(new Dimension(500, 50));JFrame frame=new JFrame("選擇你的星座");cmb.addItem("--請選擇--");? ? //向下拉選單中新增一項cmb.addItem("巨蟹座");cmb.addItem("獅子座");cmb.addItem("雙魚座");panel.add(cmb);panel.add(label);panel.add(jtf);panel.add(buttonAdd);panel.add(buttonDel);panel.add(showInfo);frame.add(panel);buttonAdd.addActionListener(new MyActionListener(this));? ? //「新增」按鈕的事件buttonDel.addActionListener(new MyActionListener(this));? ? //「刪除」按鈕的事件cmb.addItemListener(new MyItemListener(this));? ? //下拉選單的事件SwingUtils.setCenter(frame);//設定表單大小600*800並居中frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {new Demo21_StarSelector();}public JLabel getShowInfo() {return showInfo;}public JPanel getPanel() {return panel;}public JTextField getJtf() {return jtf;}public JComboBox getCmb() {return cmb;}}//監聽選中事件class MyItemListener implements ItemListener{private Demo21_StarSelector demo21_StarSelector;public MyItemListener(Demo21_StarSelector demo21_StarSelector) {this.demo21_StarSelector = demo21_StarSelector;}@Overridepublic void itemStateChanged(ItemEvent e){JPanel panel = demo21_StarSelector.getPanel();JLabel showInfo = demo21_StarSelector.getShowInfo();String str=e.getItem().toString();panel.add(showInfo);showInfo.setText("您選擇的星座是:"+str);}}//監聽新增和刪除按鈕事件class MyActionListener implements ActionListener{private Demo21_StarSelector demo21_StarSelector;public MyActionListener(Demo21_StarSelector demo21_StarSelector) {this.demo21_StarSelector = demo21_StarSelector;}@Overridepublic void actionPerformed(ActionEvent e){JTextField jtf = demo21_StarSelector.getJtf();JComboBox cmb = demo21_StarSelector.getCmb();JPanel panel = demo21_StarSelector.getPanel();JLabel showInfo = demo21_StarSelector.getShowInfo();String command=e.getActionCommand();//新增按鈕處理if(command.equals("新增")){if(jtf.getText().length()!=0){cmb.addItem(jtf.getText());? ? //新增項panel.add(showInfo);showInfo.setText("新增成功,新增了:"+jtf.getText());}else{panel.add(showInfo);showInfo.setText("請輸入要新增星座");}}//刪除按鈕處理if(command.equals("刪除")){if(cmb.getSelectedIndex()!=-1){//先獲得要刪除的項的值String strDel=cmb.getSelectedItem().toString();if(strDel.contentEquals("--請選擇--")) {showInfo.setText("請選擇要刪除的星座");}else {cmb.removeItem(strDel);? ? //刪除項panel.add(showInfo);showInfo.setText("刪除成功,刪除了:"+strDel);}}else{panel.add(showInfo);showInfo.setText("請選擇要刪除的星座");}}}}