2017-05-16 34 views
0

ユーザーがガソリンスタンドを選択したとしましょう。フレームが変更され、ユーザーが場所を選択できるようになり、その場所にあるガソリンスタンドのリストがコンボボックスの下に表示されます。JComboBoxアクションは実行されません

if文の中に別のアクションリスナーを追加しようとしましたが、if else文とtry文も試してみましたが、どちらも出力を表示しません。だから私はこれをどのように解決するのですか?前もって感謝します。これは私が

action2 = new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         JComboBox locationSelected = (JComboBox) e.getSource(); 

         if (locationSelected == Kuala Lumpur) { 
          System.out.println("Address 1"); 
         } 
        } 
       }; 
を実行しようとしましたものです

これは、第二アクションリスナーを追加する前に私のコードの短縮バージョン

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Testing5 { 
private JFrame frame1, frame2; 
private ActionListener action, action2; 
private JButton PetrolStations, back, Foods; 
private JComboBox locationChooser; 
final static String[] location = {"Petaling Jaya", "Port Klang", "Kuala Lumpur"}; 

public void HELPMEGUI() { 

    frame1 = new JFrame("Frame 1"); 
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

    JPanel contentPanel = new JPanel(new GridLayout(5,2)); 

    PetrolStations = new JButton ("PetrolStations"); 
    Foods = new JButton ("Foods");  
    back = new JButton ("Back"); 

    locationChooser = new JComboBox(location); 

    action = new ActionListener() { 
     public void actionPerformed(ActionEvent ae) { 
      JButton button = (JButton) ae.getSource();   

      if (button == PetrolStations) { 
       frame2 = new JFrame("FRAME 2"); 
       frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
       frame2.add(locationChooser, BorderLayout.NORTH); 
       frame2.add(back, BorderLayout.SOUTH); 

       frame2.setSize(300, 300); 
       frame2.setLocationRelativeTo(null); 
       frame2.setVisible(true); 
       frame1.setVisible(false); 

      } 

      else if (button == Foods) { 
       frame2 = new JFrame("FRAME 2"); 
       frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
       frame2.add(locationChooser, BorderLayout.NORTH); 
       frame2.add(back, BorderLayout.SOUTH); 

       frame2.setSize(300, 300); 
       frame2.setLocationRelativeTo(null); 
       frame2.setVisible(true); 
       frame1.setVisible(false); 

      } 

      else if (button == back) { 
       frame1.setVisible(true); 
       frame2.setVisible(false); 
       frame2.dispose(); 
      } 
     } 
    }; 

    PetrolStations.addActionListener(action); 
    Foods.addActionListener(action); 
    back.addActionListener(action); 
    locationChooser.addActionListener(action2); 

    contentPanel.add(PetrolStations); 
    contentPanel.add(Foods); 

    frame1.getContentPane().add(contentPanel); 
    frame1.setSize(640, 400); 
    frame1.setVisible(true); 
    frame1.setLocationRelativeTo(null); 

} 

public static void main(String...args) { 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
      new Testing5().HELPMEGUI(); 
     } 
    }); 
} 

} 

EDITです

2n

action2 = new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
        int temp; 

        if(e.getSource() == locationChooser) { 
         temp = locationChooser.getSelectedIndex(); 

         switch (temp) { 
         case 0: System.out.println("Address 1"); break; 
         case 1: System.out.println("Address 2"); break; 
         } 
        } 
        } 
        };} 
+1

は 'action2'この@MadProgrammer(それの' null'なので) – MadProgrammer

+0

を初期化されることはありません、私は新しいアクション – Farhan

+1

まあ、それはunhelp – MadProgrammer

答えて

0

を試してみてくださいdはあなたのアクションリスナーの定義のコンテキストを示していないので、私はあなたが間違った場所にそれを持っているかもしれないと思います。ここで私はそれをやった方法です、それが動作しているようです:

PetrolStations.addActionListener(action); 
    Foods.addActionListener(action); 
    back.addActionListener(action); 
    action2 = new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 
      int temp; 

      if (e.getSource() == locationChooser) { 
       temp = locationChooser.getSelectedIndex(); 

       switch (temp) { 
        case 0: 
         System.out.println("Address 1"); 
         break; 
        case 1: 
         System.out.println("Address 2"); 
         break; 
       } 
      } 
     } 
    }; 
    locationChooser.addActionListener(action2); 
+0

私がこのことを質問しても申し訳ないのですが、私は編集します。しかし、このコードは第1アクションリスナーの中に2番目のアクションリスナーを追加する前のコードです – Farhan

+0

2番目のアクションリスナーを表示すると、私はあなたのためにより良い答えが表示されます... –

+0

私は2つのコードセット私が試した – Farhan

関連する問題