JComboBox和JButton监听事件的问题

时间:2017-10-29 22:02:43

标签: 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

1 个答案:

答案 0 :(得分:2)

您的基本问题是可变范围之一。您在构造函数中定义的变量不能在构造函数的范围内访问。

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

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