2013-03-19 55 views
0

嗨,我的代碼到目前爲止是這樣的:點擊一個按鈕,打開一個組合框。我想在ComboBox上選擇一個選項,並根據選擇哪個選項,我想使用getSelectIndex()打開另一個組合框。從另一個類的方法創建一個actionlistener

這裏是我的代碼的部分是相關的。我知道我必須讓其他組合框不可見或被刪除,但目前我只是想讓組合框出現。正如你所看到的,我已經插入了按鈕的actionlistener,它可以工作並打開組合框。但是,在組合框中選擇一個字符串時,不會發生任何事件。但是,當我運行它時,不會出現組合框。

public class Work extends JFrame { 
// variables for JPanel 

    private JPanel buttonPanel; 
    private JButton timeButton; 

    public Work() 
{ 
     setLayout(new BorderLayout()); 

     buttonPanel = new JPanel(); 
    buttonPanel.setBackground(Color.RED); 
    buttonPanel.setPreferredSize(new Dimension(400, 500)); 
     add(buttonPanel,BorderLayout.WEST); 
     timeButton = new JButton("Time"); 
    buttonPanel.add(timeButton); 


    buttontime clickTime = new buttontime(); // event created when time button is clicked 
    timeButton.addActionListener(clickTime); 

    Time timeObject = new Time(); 
    timeObject.SelectTime(); 
    buttontime2 selectDest = new buttontime2(); 
    timeObject.getAirportBox().addActionListener(selectDest); 



    } 



     public class buttontime implements ActionListener { //creating actionlistener for clicking on timebutton to bring up a combobox 
    public void actionPerformed(ActionEvent clickTime) { 
      Time timeObject = new Time(); 
      timeObject.SelectTime(); 
      add(timeObject.getTimePanel(),BorderLayout.EAST); 
      timeObject.getTimePanel().setVisible(true); 
      timeObject.getTimePanel().revalidate() ; 
      timeObject.getAirportBox().setVisible(true); 


    } 
    } 





      public class buttontime2 implements ActionListener{ 
    public void actionPerformed(ActionEvent selectDest) { 
    Time timeObject = new Time(); 
    timeObject.SelectTime(); 


    if(timeObject.getAirportBox().getSelectedIndex() == 1) { 



    timeObject.getEastMidBox().setVisible(true); 

    } 

    else if(timeObject.getAirportBox().getSelectedIndex() == 2) { 

    timeObject.getBirmBox().setVisible(true); 
} 
    else if(timeObject.getAirportBox().getSelectedIndex() == 3) { 

    timeObject.getMancbox().setVisible(true); 
    } 
    else if(timeObject.getAirportBox().getSelectedIndex() == 4) { 
timeObject.getHeathBox().setVisible(true); 
    } 

    } 
    } 



public static void main (String args[]) { 
events mainmenu = new events(); //object is created 


mainmenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
mainmenu.setSize(800,500); 
mainmenu.setVisible(true); 
mainmenu.setLayout(new BorderLayout()); 
mainmenu.setTitle("Learning how to use GUI"); 
mainmenu.setBackground(Color.BLUE); 
mainmenu.setResizable(false); 

} 
} 

我其他的上課時間

  import javax.swing.JOptionPane; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

class Time 
{ 

    private JComboBox timeAirportbox;//comboboxes declared 
    private JComboBox eastMidbox; 
    private JComboBox mancBox; 
    private JComboBox heathBox; 
    private JComboBox birmBox; 
    private String[] airport = {"","EM", "Bham", "Manc", "Heath"};//array of airports declared 
    private String[] destination = {"","NY", "Cali", "FlO", "MIAMI", "Tokyo"};//array  of destinations declared 
    private JPanel timePanel; 

    public void SelectTime() { 



//combobox objects created 

    timePanel = new JPanel(); 
    timePanel.setBackground(Color.BLUE); 
    timePanel.setPreferredSize(new Dimension(400, 400)); 

    timeAirportbox = new JComboBox(airport);//array is inserted into the JComboBox 
    timePanel.add(timeAirportbox); 
    timeAirportbox.setVisible(false); 


    eastMidbox = new JComboBox(destination); 
    timePanel.add(eastMidbox); 
    eastMidbox.setVisible(false); 

    mancBox = new JComboBox(destination); 
    timePanel.add(mancBox); 
    mancBox.setVisible(false); 

    heathBox = new JComboBox(destination); 
    timePanel.add(heathBox); 
    heathBox.setVisible(false); 

    birmBox = new JComboBox(destination); 
    timePanel.add(birmBox); 
    birmBox.setVisible(false); 




} 



    public JPanel getTimePanel() { 
    return timePanel; 
    } 

    public JComboBox getAirportBox() { 
    return timeAirportbox;  
    } 

    public JComboBox getEastMidBox() { 
    return eastMidbox; 
    }  

    public JComboBox getMancbox() { 
    return mancBox; 
    } 

    public JComboBox getHeathBox() { 
    return heathBox; 
    } 

    public JComboBox getBirmBox() { 
    return birmBox; 
    }  





    } 

回答

2

內置在工作構造方法中沒有使用的時間對象:

Time timeObject = new Time(); 
    timeObject.SelectTime(); 
    buttontime2 selectDest = new buttontime2(); 
    timeObject.getAirportBox().addActionListener(selectDest); 

當你只將動作監聽selectedDest到ComboBox沒有使用的時間對象,那麼監聽器將永遠不會被調用。

你可以做兩件事情,使其工作:

  • 將創建監聽器的代碼,並在第一個監聽buttontime
  • 分配給COMBOX創建時間對象一次並儲存起來作爲您的Work實例的成員。由於您的偵聽器是Work類的非靜態內部類,它將能夠使用它而不是創建新的Time對象。

編輯:我沒有看到,在你的第二個聽衆,你又建立一個新的Time對象。這個對象實際上與之前創建的對象不同,因此修改對象不會影響其他對象。您應該創建一次Time對象並將其存儲爲Work類的成員變量,然後在偵聽器中使用此對象而不是重新創建它。

需要明確的是,像這樣做:

public class Work extends JFrame { 

    // ... 

    private Time timeObject; 

    public Work() { 

     // ... 

     timeObject = new Time(); 
     timeObject.SelectTime(); 
     buttontime2 selectDest = new buttontime2(); 
     timeObject.getAirportBox().addActionListener(selectDest); 

    } 

    public class buttontime implements ActionListener { 
     public void actionPerformed(ActionEvent clickTime) { 
      // use timeObject, don't create it and don't call SelectTime() 
      // example: 
      add(timeObject.getTimePanel(),BorderLayout.EAST); 
      // .... 
     } 
    } 

    public class buttontime2 implements ActionListener { 
     public void actionPerformed(ActionEvent clickTime) { 
      // use timeObject, don't create it and don't call SelectTime() 

     } 
    } 
} 

另外要注意:

  • 你不應該擴展JFrame的,沒有理由這樣做。重構您的代碼,以便您的框架只是Work類的成員變量。
  • 跟着Java standard code conventions,特別是類名正確的用例:buttonlistener應該是ButtonListener,方法應該以小寫字母開頭:SelectTime應該是selectTime
+0

感謝您的回覆,我會去看看我能否做到這一點。我很困惑的對象的概念,雖然我怎麼可以存儲這個作爲一個成員變量 – user2188332 2013-03-19 21:24:08

+0

非常感謝你擴大了我的理解 – user2188332 2013-03-19 21:28:33

相關問題