java - JComboBox 和 JButton 监听事件的问题

标签 java swing jbutton actionlistener jcombobox

我正在尝试创建一个程序,用户从 JComboBox 中选择颜色,然后按 JButton 将 JPanel 颜色更改为用户选择的颜色。 我似乎无法让操作事件逻辑正常工作。

我已设置操作监听器来获取所选项目值并将其应用为面板的所选背景,但它无法编译。

我的尝试

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class EventTest extends JFrame implements ActionListener
{

    EventTest()
    {

        super("My First Frame");

        //Create a JPanel and content frame
        JPanel panel = new JPanel();
        Container contentPane = getContentPane();

        //Create a list of colours for our JCombo Box
        String colours[] = { "Red", "Yellow", "Green", "Blue", "Orange" };

        //Create a JCombo Box
        JComboBox colourSelector = new JComboBox ( colours );
        colourSelector.addActionListener ( this );  //Add listener to the box
        panel.add ( colourSelector );   //Add combobox to the panel

        //Create a JButton
        JButton changeColour = new JButton ( "Change Colour" ); //Create a new JButton.
        changeColour.addActionListener ( this ); //Add listener to the button.
        panel.add ( changeColour ); //Add button to the panel.

        //Add the content to the pane
        contentPane.add ( panel );

        //Set window parameters
        setTitle ( "Lab4" );
        setSize ( 800, 600 );
        setVisible ( true );

    }//End Constructor


    public static void main ( String args[] )
    {
        EventTest myFrame = new EventTest();
    }

    //Action Listener - An action listener that changes the colour of our JPanel to the
    //colour selected by the user.
    public void actionPerformed ( ActionEvent e)
    {

        int selectedItem = colourSelector.getSelectedItem();
        panel.setBackground ( Color.selectedItem ); //Set the background colour based on our list.

    }


}//End Class

最佳答案

您的基本问题是变量范围之一。您在构造函数中定义的变量无法在构造函数的范围之外访问。

您可以使用匿名监听器解决问题,但为了教育目的,您可能应该在不同的方法实例字段中创建要访问的变量,例如:

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class EventTest extends JFrame implements ActionListener {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new EventTest();
            }
        });
    }

    private JComboBox<String> colourSelector;
    private String colours[] = {"Red", "Yellow", "Green", "Blue", "Orange"};
    private JPanel panel;

    EventTest() {

        super("My First Frame");

        //Create a JPanel and content frame
        Container contentPane = getContentPane();
        panel = new JPanel();

        //Create a JCombo Box
        colourSelector = new JComboBox(colours);
        colourSelector.addActionListener(this);  //Add listener to the box
        panel.add(colourSelector);   //Add combobox to the panel

        //Create a JButton
        JButton changeColour = new JButton("Change Colour"); //Create a new JButton.
        changeColour.addActionListener(this); //Add listener to the button.
        panel.add(changeColour); //Add button to the panel.

        //Add the content to the pane
        contentPane.add(panel);

        //Set window parameters
        setTitle("Lab4");
        setSize(800, 600);
        setVisible(true);

    }//End Constructor

    //Action Listener - An action listener that changes the colour of our JPanel to the
    //colour selected by the user.
    public void actionPerformed(ActionEvent e) {

//        private String colours[] = {"Red", "Yellow", "Green", "Blue", "Orange"};

        String selectedItem = (String)colourSelector.getSelectedItem();
        System.out.println(selectedItem);
        switch (selectedItem) {
            case "Red": 
                panel.setBackground(Color.RED);
                break;
            case "Yellow": 
                panel.setBackground(Color.YELLOW);
                break;
            case "Green": 
                panel.setBackground(Color.GREEN);
                break;
            case "Blue": 
                panel.setBackground(Color.BLUE);
                break;
            case "Orange": 
                panel.setBackground(Color.ORANGE);
                break;
        }
    }

}//End Class

关于java - JComboBox 和 JButton 监听事件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47005709/

相关文章:

java - 无法解析 Android 开发者教程网页上的符号 "setText"

java - 如何在 Swing 中显示打印进度对话框?

java - 如何更新 Java Gui 中的变量?

java - JButton 仅在可见性设置为 True-Java 时才起作用

java - 为什么我不能dispose()?

java - java中读取网页源码与原始网页源码不同

java - 指定使用 wsimport 构造 stub 时要使用的 bean

java - 具有可折叠 JPanel 的容器的首选布局管理器是什么

java - JButton 设置位置不起作用

java - commons cli 复杂参数列表