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のコンボボックスにのみアクションリスナーを適用しているため、リスナーが呼び出されることはありません。

あなたはそれを機能させるために2つのことをすることができます:

  • リスナーを作成するコードを移動し、最初のリスナーのcomboxに割り当てますbuttontime
  • Timeオブジェクトを1回だけ作成し、Workインスタンスのメンバーとして保存します。リスナーはWorkクラスの非静的内部クラスであるため、新しいTimeオブジェクトを作成する代わりにそれを使用できます。

編集: 2番目のリスナーで、新しい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 に答える