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

Java 从另一个类中的方法创建actionlistener,java,swing,awt,actionlistener,Java,Swing,Awt,Actionlistener,嗨,到目前为止,我的代码是这样的:单击一个按钮,打开一个组合框。我想在组合框上选择一个选项,根据选择的选项,我想使用getSelectIndex()打开另一个组合框 下面是我的代码中与此相关的部分。我知道我必须使其他组合框不可见或删除,但目前我只是试图使组合框出现。正如您所看到的,我为按钮插入了actionlistener,该按钮可以工作并打开组合框。但是,在组合框中选择字符串时,不会发生任何事件。但是,当我运行它时,没有出现组合框 public class Work extends JFram

嗨,到目前为止,我的代码是这样的:单击一个按钮,打开一个组合框。我想在组合框上选择一个选项,根据选择的选项,我想使用getSelectIndex()打开另一个组合框

下面是我的代码中与此相关的部分。我知道我必须使其他组合框不可见或删除,但目前我只是试图使组合框出现。正如您所看到的,我为按钮插入了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;
    }       





    }

未使用工作构造函数中内置的时间对象:

  Time timeObject = new Time();
  timeObject.SelectTime();
  buttontime2 selectDest = new buttontime2();
  timeObject.getAirportBox().addActionListener(selectDest);
由于您仅将操作侦听器
selectedtest
应用于该时间对象的组合框(未使用),因此将永远不会调用侦听器

您可以做两件事使其工作:

  • 移动创建侦听器的代码并将其分配给第一个侦听器中的combox
    buttontime
  • 只创建一次时间对象,并将其存储为工作实例的成员。由于侦听器是工作类的非静态内部类,所以它将能够使用它,而不是创建新的时间对象
编辑:在第二个侦听器中,我没有看到您再次构建了一个新的时间对象。此对象实际上与您先前创建的对象不同,因此修改一个对象不会影响另一个对象。您确实应该创建一次时间对象并将其存储为工作类的成员变量,然后在侦听器中使用该对象,而不是重新创建它

要清楚,请按如下方式操作:

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,没有理由这样做。重构代码,使框架只是工作类的一个成员变量
  • 遵循,尤其是使用类名称的用例:
    buttonlistener
    应该是
    buttonlistener
    ,方法应该以小写开头:
    SelectTime
    应该是
    SelectTime

谢谢你的回复,我会看看我是否能做到这一点。但是我对对象的概念感到困惑,我如何将其存储为一个成员变量非常感谢你拓宽了我的理解