我需要帮助解决这个问题。 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);
}
}
答案 0 :(得分:1)
JFrame SevenContinentsFrame = new JFrame();
您是从Swing API创建JFrame
,而不是SevenContients
的实例,也是JFrame
。
SevenContinents SevenContinentsFrame = new SevenContinents();
这就是......
SevenContinentsFrame.add(button1);
您引用的是不存在的名称。您已在main方法中创建了具有该名称的变量,但它在SevenContinents
构造函数中不可见。只需删除名称即可。 E.g。
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);
}
}
答案 1 :(得分:0)
您的班级名称为SevenContinents
,您正在尝试访问不存在的SevenContinentsFrame
。使用SevenContinentsFrame
和中提琴更改对SevenContinents
的所有引用!
答案 2 :(得分:0)
这是你的完整代码尝试这个它经过测试,如果你使用任何其他IDE而不是BlueJ更改是在这行中做的更好
它不是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...");
}
}
}
}