我需要帮助解决这个问题。 BlueJ 上的错误消息显示“找不到符号 - 变量 SevenContinentsFrame。我是一个初学者,不知道该怎么做,并且已经在这个代码上工作了几个小时。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
public class SevenContinents extends JFrame implements ActionListener
{
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
public SevenContinents()
{
//BUTTONS!!!
setSize(1500,1000);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
SevenContinentsFrame.add(button1);
SevenContinentsFrame.add(button2);
SevenContinentsFrame.add(button3);
SevenContinentsFrame.add(button4);
SevenContinentsFrame.add(button5);
SevenContinentsFrame.add(button6);
SevenContinentsFrame.add(button7);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource == btn1)
{
System.out.println("You chose...");
}
if(e.getSource == btn2)
{
System.out.println("You chose...");
}
if(e.getSource == btn3)
{
System.out.println("You chose...");
}
if(e.getSource == btn4)
{
System.out.println("You chose...");
}
if(e.getSource == btn5)
{
System.out.println("You chose...");
}
if(e.getSource == btn6)
{
System.out.println("You chose...");
}
if(e.getSource == btn7)
{
System.out.println("You chose...");
}
}
public static void main(String[] args)
{
JFrame SevenContinentsFrame = new JFrame();
SevenContinentsFrame.setVisible(true);
SevenContinentsFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
}
}
最佳答案
JFrame SevenContinentsFrame = new JFrame();
您正在从 Swing API 创建一个 JFrame
,而不是 SevenContients
的实例,它也是一个 JFrame
。
SevenContinents SevenContinentsFrame = new SevenContinents();
还有这个...
SevenContinentsFrame.add(button1);
您引用的名称不存在。您已在 main 方法中创建了一个具有该名称的变量,但它在 SevenContinents
构造函数中不可见。只需删除名称即可。例如
add(button1);
add(button2);
.
.
.
您还需要将 ()
添加到 getSource
,为操作监听器中引用的按钮使用正确的变量名称,并更改布局以便按钮显示。
更正的代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
public class SevenContinents extends JFrame implements ActionListener
{
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
public SevenContinents()
{
//BUTTONS!!!
setSize(1500, 1000);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
setLayout(new FlowLayout());
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(button6);
add(button7);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1) {
System.out.println("You chose...");
}
if (e.getSource() == button2) {
System.out.println("You chose...");
}
if (e.getSource() == button3) {
System.out.println("You chose...");
}
if (e.getSource() == button4) {
System.out.println("You chose...");
}
if (e.getSource() == button5) {
System.out.println("You chose...");
}
if (e.getSource() == button6) {
System.out.println("You chose...");
}
if (e.getSource() == button7) {
System.out.println("You chose...");
}
}
public static void main(String[] args)
{
SevenContinents SevenContinentsFrame = new SevenContinents();
SevenContinentsFrame.setVisible(true);
SevenContinentsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
关于java - 按钮 Action 监听器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21213854/