简体   繁体   English

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

[英]Creating an actionlistener from a method in another class

Hi well my code so far does something like this: Click a button, opens a combobox. 嗨,我的代码到目前为止做了这样的事情:点击一个按钮,打开一个组合框。 I want to select an option on the ComboBox and depending on which option is picked i want to open another combobox using getSelectIndex(). 我想在ComboBox上选择一个选项,根据选择的选项,我想使用getSelectIndex()打开另一个组合框。

Here are parts of my code which are relevant. 以下是我的代码的相关部分。 I know I have to make the other comboboxes not visible or removed but at the moment I'm just trying to make a combobox appear. 我知道我必须让其他组合框不可见或被删除,但此刻我只是想让组合框出现。 As you can see i have inserted the actionlistener for the button which works and opens the combobox.however when selecting a string in the combobox no event occurs. 正如你所看到的,我已经为按钮运行了动作滑动器并打开了组合框。但是当在组合框中选择一个字符串时,没有发生任何事件。 However when I run it, no comboboxes appear. 但是当我运行它时,没有出现组合框。

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

}
}

my other class TIME 我的其他课时

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





    }

The Time object that is built in Work constructor is not used: 不使用在Work构造函数中构建的Time对象:

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

As you are only applying the action listener selectedDest to the combobox of that timeObject, which is not used, then the listener will never be called. 由于您只将动作侦听器selectedDest应用于未使用的timeObject的组合框,因此永远不会调用侦听器。

You can do two things to make it work: 你可以做两件事来使它工作:

  • Move the code that creates the listener and assign it to the combox in the first listener buttontime 移动创建侦听器的代码,并在第一个侦听器buttontime将其分配给buttontime
  • Create the Time object only once and store it as a member of your Work instance. 仅创建一次Time对象并将其存储为Work实例的成员。 As your listener is a non-static inner classes of the Work class, it will be able to use it instead of creating a new Time object. 由于您的侦听器是Work类的非静态内部类,因此它将能够使用它而不是创建新的Time对象。

Edit: I didn't see that in your second listener, you were AGAIN building a new Time object. 编辑:我没有看到你的第二个听众,你正在建立一个新的时间对象。 This object is really a different one than the one you have created earlier, so modifying one will not affect the other. 这个对象实际上与您之前创建的对象不同,因此修改一个对象不会影响另一个对象。 You really should create the Time object once and store it as a member variable of your Work class, and then use this object in your listeners instead of recreating it. 您真的应该创建一次Time对象并将其存储为Work类的成员变量,然后在侦听器中使用此对象而不是重新创建它。

To be clear, do it like this: 要清楚,这样做:

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()

        }
    }
}

Also to note: 另请注意:

  • You should not extends JFrame, there is no reason to do so. 你不应该扩展JFrame,没有理由这样做。 Refactor your code so that your frame is just a member variable of your Work class. 重构代码,使您的框架只是Work类的成员变量。
  • Follow Java standard code conventions , especially use case properly with class names: buttonlistener should be ButtonListener , and method should start with lowercase: SelectTime should be selectTime . 遵循Java标准代码约定 ,尤其是使用类名称的用例: buttonlistener应该是ButtonListener ,方法应该以小写开头: SelectTime应该是selectTime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM