JComboBox操作不会执行

时间:2017-05-16 01:32:54

标签: java eclipse actionlistener 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;
                         }
                     }
                    }
                    };}

1 个答案:

答案 0 :(得分: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);