从另一个类中的方法创建actionlistener

时间:2013-03-19 21:03:26

标签: java swing awt actionlistener

您好我的代码到目前为止做了类似的事情:点击一个按钮,打开一个组合框。我想在ComboBox上选择一个选项,根据选择的选项,我想使用getSelectIndex()打开另一个组合框。

以下是我的代码的相关部分。我知道我必须让其他组合框不可见或被删除,但此刻我只是想让组合框出现。正如你所看到的,我已经为按钮运行了动作滑动器并打开了组合框。但是当在组合框中选择一个字符串时,没有发生任何事件。但是当我运行它时,没有出现组合框。

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





    }

1 个答案:

答案 0 :(得分:2)

不使用在Work构造函数中构建的Time对象:

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

由于您只将动作侦听器selectedDest应用于未使用的timeObject的组合框,因此永远不会调用侦听器。

你可以做两件事来让它发挥作用:

  • 移动创建侦听器的代码并将其分配给第一个侦听器中的combox buttontime
  • 仅创建一次Time对象,并将其存储为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