0

嗨,到目前为止,我的代码是这样的:单击一个按钮,打开一个组合框。我想在 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;
    }       





    }
4

1 回答 1

2

不使用 Work 构造函数中内置的 Time 对象:

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

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

你可以做两件事来让它工作:

  • 移动创建侦听器的代码并将其分配给第一个侦听器中的组合框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 标准代码约定,尤其是正确使用类名的用例:buttonlistener应该是ButtonListener,方法应该以小写开头:SelectTime应该是selectTime
于 2013-03-19T21:17:13.983 回答