2017-05-16 33 views
0

让说用户选择加油站。该框架将改变,用户将能够选择一个位置,该位置的加油站列表将显示在组合框下方。JComboBox行动将不会执行

所以我已经尝试在if语句内添加另一个动作侦听器,使用另一个if else,并且我也尝试了switch语句,但都不会显示输出。那么我该如何解决这个问题?提前致谢。

这是我的代码的缩短版添加第二个动作监听

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

} 

编辑:

这就是我试图做

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

         if (locationSelected == Kuala Lumpur) { 
          System.out.println("Address 1"); 
         } 
        } 
       }; 

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'从未初始化(它的'null') – MadProgrammer

+0

@MadProgrammer这是代码之前,我加入了新的动作 – Farhan

+1

嗯,这是unhelp – MadProgrammer

回答

0

你不显示你的行动侦听器定义的范围内,所以我想你可能有它在错误的地方。以下是我做到了,而且似乎工作:

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

如果我没有在这个问题上说明这一点,我很抱歉,我会编辑。但是,这个代码是我在第一个动作监听器中添加第二个动作监听器之前的代码 – Farhan

+0

让我看看第二个动作监听器,我会看看我是否有更好的答案... –

+0

我已经添加了两组代码我曾尝试过 – Farhan