JComboBox 操作不会执行

JComboBox action won't perform

假设用户选择了加油站。框架将发生变化,用户将能够选择一个位置,该位置的加油站列表将显示在组合框下方。

所以我尝试在 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");
                        }
                    }
                };

第二次尝试

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

您没有显示动作侦听器定义的上下文,所以我认为您可能把它放错了地方。这是我的做法,它似乎有效:

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