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

第二次尝试

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

最新更新