gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:31:36 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,用户从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com