2012-08-02 85 views
0

下面是我的代碼,我試過每一件事情。我嘗試添加getSelectedItem()等,所有無濟於事,ComboBox只能使用一次,它甚至不會更新。幫幫我吧!JComboBox只能使用一次,並沒有更新

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.*; 

import javax.swing.*; 


public class MainClass extends JFrame implements ActionListener { 
    String[] AmtOfPpl = {"1", "2", "3", "4"}; 

    private JLabel jlabel = new JLabel("Please select a table:"); 
    private JButton a01 = new JButton("A01"); 
    private JButton a02 = new JButton("A02"); 
    private JButton a03 = new JButton("A03"); 
    private JButton a04 = new JButton("A04"); 
    private JButton a05 = new JButton("A05"); 
    private JButton a06 = new JButton("A06"); 
    private JButton a07 = new JButton("A07"); 
    private JButton a08 = new JButton("A08"); 
    private JButton a09 = new JButton("A09"); 
    private JButton a10 = new JButton("A10"); 

    private JButton c01 = new JButton("C01"); 
    private JButton c02 = new JButton("C02"); 
    private JButton c03 = new JButton("C03"); 
    private JButton c04 = new JButton("C04"); 
    private JButton c05 = new JButton("C05"); 
    private JButton c06 = new JButton("C06"); 
    private JButton c07 = new JButton("C07"); 
    private JButton c08 = new JButton("C08"); 
    private JButton c09 = new JButton("C09"); 
    private JButton c10 = new JButton("C10"); 
    private JButton c11 = new JButton("C11"); 
    private JButton c12 = new JButton("C12"); 

    private JLabel FstName = new JLabel("*First Name: "); 
    private JLabel LstName = new JLabel("*Last Name: "); 
    private JLabel Addrss = new JLabel("Address: "); 
    private JLabel PstlCode = new JLabel("Postal Code: "); 
    private JLabel HndPne = new JLabel("*Handphone No.: "); 
    private JLabel AoP = new JLabel("*Amount of People: "); 
    private JLabel Mndtry = new JLabel("Fields marked with * are mandatory."); 
    private JTextField FName = new JTextField(); 
    private JTextField LName = new JTextField(); 
    private JTextField Addr = new JTextField(); 
    private JTextField PCode = new JTextField(); 
    private JTextField HP = new JTextField(); 
    private JComboBox<?> AmtPpl = new JComboBox<Object>(AmtOfPpl); 


    public MainClass() { 

     JPanel p1 = new JPanel(); 
     p1.setLayout(new GridLayout(2,5,10,25)); 
     p1.add(a01); 
     p1.add(a02); 
     p1.add(a03); 
     p1.add(a04); 
     p1.add(a05); 
     p1.add(a10); 
     p1.add(a09); 
     p1.add(a08); 
     p1.add(a07); 
     p1.add(a06); 

     JPanel p2 = new JPanel(); 
     p2.setLayout(new GridLayout(6,2,2,2)); 
     p2.add(c01); 
     p2.add(c02); 
     p2.add(c03); 
     p2.add(c04); 
     p2.add(c05); 
     p2.add(c06); 
     p2.add(c07); 
     p2.add(c08); 
     p2.add(c09); 
     p2.add(c10); 
     p2.add(c11); 
     p2.add(c12); 

     a01.addActionListener(this); 
     a02.addActionListener(this); 
     a03.addActionListener(this); 
     a04.addActionListener(this); 
     a05.addActionListener(this); 
     a06.addActionListener(this); 
     a07.addActionListener(this); 
     a08.addActionListener(this); 
     a09.addActionListener(this); 
     a10.addActionListener(this); 

     c01.addActionListener(this); 
     c02.addActionListener(this); 
     c03.addActionListener(this); 
     c04.addActionListener(this); 
     c05.addActionListener(this); 
     c06.addActionListener(this); 
     c07.addActionListener(this); 
     c08.addActionListener(this); 
     c09.addActionListener(this); 
     c10.addActionListener(this); 
     c11.addActionListener(this); 
     c12.addActionListener(this); 

     AmtPpl.setSelectedIndex(3); 
     AmtPpl.addActionListener(this); 

     setLayout(new BorderLayout(20,10)); 
     add(jlabel, BorderLayout.NORTH); 
     add(p1, BorderLayout.CENTER); 
     add(p2, BorderLayout.EAST); 


    } 

    public static void main(String[] args) { 
     MainClass frame = new MainClass(); 
     frame.setTitle("Table Reservation System"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(550, 300); 
     frame.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 

     JPanel InputPanel = new JPanel(); 
     InputPanel.setLayout(new GridLayout(7,2)); 
     InputPanel.add(FstName); 
     InputPanel.add(FName); 
     InputPanel.add(LstName); 
     InputPanel.add(LName); 
     InputPanel.add(Addrss); 
     InputPanel.add(Addr); 
     InputPanel.add(PstlCode); 
     InputPanel.add(PCode); 
     InputPanel.add(HndPne); 
     InputPanel.add(HP); 
     InputPanel.add(AoP); 
     InputPanel.add(AmtPpl); 
     InputPanel.add(Mndtry); 

     if(e.getSource()==a01) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==a02) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==a03) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==a04) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==a05) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==a06) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==a07) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==a08) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==a09) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==a10) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c01) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c02) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c03) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c04) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c05) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c06) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c07) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c08) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c09) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c10) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c11) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
     else if(e.getSource()==c12) { 
      JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
     } 
    } 
} 

在此先感謝! (:

PS如果你有關於如何清理代碼的提示,請分享!謝謝!順便說一句,這是一個表預訂系統的簡單編碼,但不完整,即將實現MySQL服務器和後端。

+0

你是什麼意思「只能用一次?」。第一個明顯的解決方法是調用e.getSource()一次,並在所有if語句中使用返回的值。 – John3136 2012-08-02 02:41:12

+0

@ John3136當我說只能使用一次,當我在下拉列表中選擇一個項目時,就是這樣。我無法改變我的選擇。 – Roflnator 2012-08-02 02:43:59

+0

請學習java命名約定並堅持使用它們。 – kleopatra 2012-08-02 08:18:59

回答

2

基本上發生在這裏

AmtPpl.addActionListener(this); 

的問題,在執行時,它建立另一個對話框並顯示。所以是的,價值在改變,但是因爲你顯示的是另一個對話框,所以它重寫了它。

+0

那麼我該如何解決它?我無法刪除這部分代碼。 編輯:是的!有用!刪除這部分代碼實際上有效!非常感謝! – Roflnator 2012-08-02 02:51:53

2

actionPerformed充滿了一堆重複的至少請嘗試通過封裝了大量的代碼通過從actionPerformed刪除,以除去雜波:。

if(e.getSource()==a01) { 
     JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 
    } 

& TC,並製作方法如下所示:

private void showOptionPane() { 
    JPanel InputPanel = new JPanel(); 
    InputPanel.setLayout(new GridLayout(7,2)); 
    InputPanel.add(FstName); 
    InputPanel.add(FName); 
    InputPanel.add(LstName); 
    InputPanel.add(LName); 
    InputPanel.add(Addrss); 
    InputPanel.add(Addr); 
    InputPanel.add(PstlCode); 
    InputPanel.add(PCode); 
    InputPanel.add(HndPne); 
    InputPanel.add(HP); 
    InputPanel.add(AoP); 
    InputPanel.add(AmtPpl); 
    InputPanel.add(Mndtry); 

    JOptionPane.showConfirmDialog(null, InputPanel, "Please enter the required information", JOptionPane.OK_CANCEL_OPTION); 


} 
+0

您是否嘗試過運行代碼?按鈕不在jcombobox中。當按鈕被點擊時,jcombobox在對話框中。如果我刪除按鈕的actionlistener,按鈕將不起作用。 – Roflnator 2012-08-02 02:49:12