Java 按钮操作侦听器错误
我需要帮助解决这个问题。BlueJ上的错误消息是“找不到符号变量SevenentsFrame。我是一个初学者,不知道该怎么做,已经为此代码工作了几个小时Java 按钮操作侦听器错误,java,swing,jframe,jbutton,actionlistener,Java,Swing,Jframe,Jbutton,Actionlistener,我需要帮助解决这个问题。BlueJ上的错误消息是“找不到符号变量SevenentsFrame。我是一个初学者,不知道该怎么做,已经为此代码工作了几个小时 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JButton; public class SevenContine
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);
}
}
您正在从Swing API创建一个JFrame
,而不是seventions
的实例,它也是一个JFrame
SevenContinents SevenContinentsFrame = new SevenContinents();
而这个
SevenContinentsFrame.add(button1);
您正在引用一个不存在的名称。您已在main方法中创建了一个具有该名称的变量,但它在sevencontents
构造函数中不可见。只需删除该名称即可。例如
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);
}
}
你的类名是
sevencontents
,你正在尝试访问不存在的sevencontentsFrame
。使用sevencontents
和viola!将所有引用更改为sevencontentsFrame
。这是你的完整代码。请尝试一下,这是经过测试的,如果你使用的IDE比使用BlueJ ch更好Ange是用这条线制作的
这不是JFrame这是你的类名
JFrame SevenContinentsFrame = new JFrame();
这是什么
SevenContinentsFrame.add(button1);
另外,您并没有添加容器,最好使用数组作为按钮
容器的代码
Container cp = getContentPane();
cp.add(pane);
最后一件事,我把你们的工具取下来,换上了,我把所有的按钮都添加到面板上
package answer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SevenContinents extends JFrame
{
public static void main(String[] args)
{
SevenContinents SevenContinentsFrame = new SevenContinents();
SevenContinentsFrame.setVisible(true);
}
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
public SevenContinents()
{
//BUTTONS!!!
setSize(500,500);
setDefaultCloseOperation(SevenContinents.EXIT_ON_CLOSE);
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
JPanel pane = new JPanel();
pane.add(button1);
pane.add(button2);
pane.add(button3);
pane.add(button4);
pane.add(button5);
pane.add(button6);
pane.add(button7);
Container cp = getContentPane();
cp.add(pane);
button1.addActionListener(new addButtonWatcher());
button2.addActionListener(new addButtonWatcher());
button3.addActionListener(new addButtonWatcher());
button4.addActionListener(new addButtonWatcher());
button5.addActionListener(new addButtonWatcher());
button6.addActionListener(new addButtonWatcher());
button7.addActionListener(new addButtonWatcher());
}
private class addButtonWatcher implements ActionListener{
@Override
public void actionPerformed(ActionEvent a)
{
Object buttonPressed= a.getSource();
if(buttonPressed.equals(button1))
{
}
if(buttonPressed.equals(button2))
{
System.out.println("You chose...");
}
if(buttonPressed.equals(button3))
{
System.out.println("You chose...");
}
if(buttonPressed == button4)
{
System.out.println("You chose...");
}
if(buttonPressed == button5)
{
System.out.println("You chose...");
}
if(buttonPressed == button6)
{
System.out.println("You chose...");
}
if(buttonPressed == button7)
{
System.out.println("You chose...");
}
}
}
}
你还应该解释你对代码做了哪些更改,以及为什么与用户的代码相比,代码可以工作。这是一个更有效的答案,如果答案正确,可能会导致投票。
package answer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SevenContinents extends JFrame
{
public static void main(String[] args)
{
SevenContinents SevenContinentsFrame = new SevenContinents();
SevenContinentsFrame.setVisible(true);
}
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
public SevenContinents()
{
//BUTTONS!!!
setSize(500,500);
setDefaultCloseOperation(SevenContinents.EXIT_ON_CLOSE);
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
JPanel pane = new JPanel();
pane.add(button1);
pane.add(button2);
pane.add(button3);
pane.add(button4);
pane.add(button5);
pane.add(button6);
pane.add(button7);
Container cp = getContentPane();
cp.add(pane);
button1.addActionListener(new addButtonWatcher());
button2.addActionListener(new addButtonWatcher());
button3.addActionListener(new addButtonWatcher());
button4.addActionListener(new addButtonWatcher());
button5.addActionListener(new addButtonWatcher());
button6.addActionListener(new addButtonWatcher());
button7.addActionListener(new addButtonWatcher());
}
private class addButtonWatcher implements ActionListener{
@Override
public void actionPerformed(ActionEvent a)
{
Object buttonPressed= a.getSource();
if(buttonPressed.equals(button1))
{
}
if(buttonPressed.equals(button2))
{
System.out.println("You chose...");
}
if(buttonPressed.equals(button3))
{
System.out.println("You chose...");
}
if(buttonPressed == button4)
{
System.out.println("You chose...");
}
if(buttonPressed == button5)
{
System.out.println("You chose...");
}
if(buttonPressed == button6)
{
System.out.println("You chose...");
}
if(buttonPressed == button7)
{
System.out.println("You chose...");
}
}
}
}