一个JButton来确定基于JComboBox选择的动作

时间:2023-01-27 21:38:45

I developed a simple JFrame that is composed of a panel containing a label, a combo box and a button. All I want is to redirect the user to different panels based on his choice from the combo box. I seem to have an error when making the button check the chosen item. Here's what I've done so far...

我开发了一个简单的JFrame,它由一个包含标签,组合框和按钮的面板组成。我想要的是根据他在组合框中的选择将用户重定向到不同的面板。当按钮检查所选项目时,我似乎有错误。这是我到目前为止所做的......

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test 
{

    public static void main(String [] args)
    {

        // The Frame Design Part...
        final JFrame ApplicationFrame=new JFrame("Bounceable Ball Game");
        ApplicationFrame.setVisible(true);
        ApplicationFrame.setSize(600,600);
        ApplicationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ApplicationFrame.setLocationRelativeTo(null);
        JMenuBar ApplicationMenuBar=new JMenuBar();
        JMenu File=new JMenu("File");
        JMenu Edit=new JMenu("Edit");
        JMenu Play=new JMenu("Play");
        ApplicationMenuBar.add(File);
        JMenuItem Exit=new JMenuItem("Exit");
        File.add(Exit);
        ApplicationMenuBar.add(Edit);
        ApplicationMenuBar.add(Play);
        ApplicationFrame.setJMenuBar(ApplicationMenuBar);
        JPanel ApplicationPanel =new JPanel(new GridBagLayout());
        GridBagConstraints DesignConstraints=new GridBagConstraints();
        DesignConstraints.insets=new Insets(10,10,10,10);
        DesignConstraints.gridx=0;
        DesignConstraints.gridy=1;
        JLabel GameChoiceLabel=new JLabel("Please Select The Game That You Want To Play...");
        ApplicationPanel.add(GameChoiceLabel,DesignConstraints);
        DesignConstraints.gridx=0;
        DesignConstraints.gridy=2;
        ApplicationPanel.add(GameChoiceComboBox,DesignConstraints);
        DesignConstraints.gridx=0;
        DesignConstraints.gridy=3;
        JButton GameChoiceButton=new JButton("Play Game");  
        GameChoiceButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) 
            {
                JComboBox comboBox = (JComboBox) e.getSource();
                Object selected = comboBox.getSelectedItem();
                if(selected.toString().equals("TennisBall"))
                {
                    ApplicationMenuBar.add(File);
                    JMenuItem Exit=new JMenuItem("Exit");
                    File.add(Exit);
                    ApplicationMenuBar.add(Edit);
                    ApplicationMenuBar.add(Play);
                    ApplicationFrame.setJMenuBar(ApplicationMenuBar);
                    JPanel ApplicationPanel1 =new JPanel(new GridBagLayout());
                    ApplicationFrame.setContentPane(ApplicationPanel1);
                }
            }
        });
        ApplicationPanel.add(GameChoiceButton,DesignConstraints);
        ApplicationFrame.setContentPane(ApplicationPanel);
    }
}

Thanks in advance :)

提前致谢 :)

2 个解决方案

#1


2  

You're adding the ActionListener to a JButton:

您正在将ActionListener添加到JButton:

GameChoiceButton.addActionListener(new ActionListener(){

But then you're trying to cast the source of the ActionEvent (which will always be that JButton) to a JComboBox (which it will never be):

但是你要尝试将ActionEvent的源代码(它将永远是JButton)转换为JComboBox(它永远不会是):

JComboBox comboBox = (JComboBox) e.getSource();

Just refer to the JComboBox directly instead of trying to finagle it out of the getSource() function. You'll either have to pass the JComboBox into the ActionListener, or call a method in the outer class that accesses the JComboBox, or make the JComboBox final.

只需直接引用JComboBox,而不是尝试从getSource()函数中获取它。您必须将JComboBox传递给ActionListener,或者调用访问JComboBox的外部类中的方法,或者使JComboBox成为最终版本。

#2


3  

The problem is this line:

问题是这一行:

JComboBox comboBox = (JComboBox) e.getSource();

JComboBox comboBox =(JComboBox)e.getSource();

This is in the action when the GameChoiceButton is clicked. But then you get the source (which is the button) and try to cast it to a JComboBox.

这是在点击GameChoiceButton时的动作。但是你得到了源(这是按钮)并尝试将其转换为JComboBox。

To make your code work, make these changes:

要使代码正常工作,请进行以下更改:

final JComboBox GameChoiceComboBox=new JComboBox();  //MAKE THIS FINAL
...

GameChoiceButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) 
    {
        //USE THE COMBO BOX DIRECTLY and getSelectedItem()
        if(GameChoiceComboBox.getSelectedItem().equals("TennisBall"))  

        {
            JMenuBar ApplicationMenuBar=new JMenuBar();
            JMenu File=new JMenu("File");
            JMenu Edit=new JMenu("Edit");

#1


2  

You're adding the ActionListener to a JButton:

您正在将ActionListener添加到JButton:

GameChoiceButton.addActionListener(new ActionListener(){

But then you're trying to cast the source of the ActionEvent (which will always be that JButton) to a JComboBox (which it will never be):

但是你要尝试将ActionEvent的源代码(它将永远是JButton)转换为JComboBox(它永远不会是):

JComboBox comboBox = (JComboBox) e.getSource();

Just refer to the JComboBox directly instead of trying to finagle it out of the getSource() function. You'll either have to pass the JComboBox into the ActionListener, or call a method in the outer class that accesses the JComboBox, or make the JComboBox final.

只需直接引用JComboBox,而不是尝试从getSource()函数中获取它。您必须将JComboBox传递给ActionListener,或者调用访问JComboBox的外部类中的方法,或者使JComboBox成为最终版本。

#2


3  

The problem is this line:

问题是这一行:

JComboBox comboBox = (JComboBox) e.getSource();

JComboBox comboBox =(JComboBox)e.getSource();

This is in the action when the GameChoiceButton is clicked. But then you get the source (which is the button) and try to cast it to a JComboBox.

这是在点击GameChoiceButton时的动作。但是你得到了源(这是按钮)并尝试将其转换为JComboBox。

To make your code work, make these changes:

要使代码正常工作,请进行以下更改:

final JComboBox GameChoiceComboBox=new JComboBox();  //MAKE THIS FINAL
...

GameChoiceButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) 
    {
        //USE THE COMBO BOX DIRECTLY and getSelectedItem()
        if(GameChoiceComboBox.getSelectedItem().equals("TennisBall"))  

        {
            JMenuBar ApplicationMenuBar=new JMenuBar();
            JMenu File=new JMenu("File");
            JMenu Edit=new JMenu("Edit");