我在理解GUI时遇到了一些困难,以及为什么我的程序无法正常运行。是因为我必须扩展到JFrame类吗?这是一个代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Lab_10
{
public static void main (String[] args)
{
final double EARTHQUAKE_RATE= 8.0;
final int FRAME_WIDTH= 300;
final int FRAME_HEIGHT= 200;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Richter Scale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JFrame frame = new JFrame();
JLabel label = new JLabel();
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
panel.add(rictherfield);
panel.add(rictherlabel);
add(panel);
JLabel rictherlabel = new JLabel ("Ricther: ");
final int FIELD_WIDTH = 10;
JTextField rictherField = new JTextField(FIELD_WIDTH);
richterField.setText("" + EARTHQUAKE_RATE);
JButton button = new JButton("Enter");
ActionListner listner = new AddLabelListener();
button.addActionListner(listner);
class AddLabelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
label.setText("Most structures fall");
}
}
}
}
我收到很多错误,说明程序找不到我的ActionListeners等符号。
错误是:
_10.java:2: error: cannot find symbol
import java.awt.event.ActionListner;
^
symbol: class ActionListner
location: package java.awt.event
Lab_10.java:17: error: cannot find symbol
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
^
symbol: variable frame
location: class Lab_10
Lab_10.java:18: error: cannot find symbol
frame.setTitle("Richter Scale");
^
symbol: variable frame
location: class Lab_10
Lab_10.java:19: error: cannot find symbol
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
symbol: variable frame
location: class Lab_10
Lab_10.java:20: error: cannot find symbol
frame.setVisible(true);
^
symbol: variable frame
location: class Lab_10
Lab_10.java:30: error: cannot find symbol
panel.add(button);
^
symbol: variable button
location: class Lab_10
Lab_10.java:32: error: cannot find symbol
panel.add(rictherfield);
^
symbol: variable rictherfield
location: class Lab_10
Lab_10.java:33: error: cannot find symbol
panel.add(rictherlabel);
^
symbol: variable rictherlabel
location: class Lab_10
Lab_10.java:34: error: cannot find symbol
add(panel);
^
symbol: method add(JPanel)
location: class Lab_10
Lab_10.java:40: error: cannot find symbol
richterField.setText("" + EARTHQUAKE_RATE);
^
symbol: variable richterField
location: class Lab_10
Lab_10.java:45: error: cannot find symbol
ActionListner listner = new AddLabelListener();
^
symbol: class ActionListner
location: class Lab_10
Lab_10.java:45: error: cannot find symbol
ActionListner listner = new AddLabelListener();
^
symbol: class AddLabelListener
location: class Lab_10
Lab_10.java:49: error: cannot find symbol
class AddLabelListener implements ActionListener
^
symbol: class ActionListener
location: class Lab_10
Lab_10.java:53: error: local variable label is accessed from within inner class; needs to be declared final
label.setText("Most structures fall");
答案 0 :(得分:2)
问题1:
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Richter Scale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JFrame frame = new JFrame();
您正在尝试使用尚未创建的对象。帧对象是在稍后的时间点创建的。它应该如下:
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Richter Scale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
问题2:
代码中的问题相同。 兄弟,首先你需要声明一个对象,然后只有你可以使用它并在它上面实现函数。如果您没有先声明对象,编译器将如何解析它并将其视为有效变量。
panel.add(button);
panel.add(label);
panel.add(rictherfield);
panel.add(rictherlabel);
add(panel);
JLabel rictherlabel = new JLabel ("Ricther: ");
final int FIELD_WIDTH = 10;
JTextField rictherField = new JTextField(FIELD_WIDTH);
richterField.setText("" + EARTHQUAKE_RATE);
JButton button = new JButton("Enter");
'button','richterlabel'& 'richterField'在声明之前使用。由于编译器不知道dese变量在执行时的意思,它会抛出无法找到符号。
首先声明它们然后使用它们。希望你明白这一点。
问题3:
import java.awt.event.ActionListner;
ActionListner不是Event包中的类。它应该是:
import java.awt.event.ActionListener;
寻找其他问题,同时开始研究这个问题。
答案 1 :(得分:2)
哇......好的,首先,ActionListener
拼写错误为“ActionListner
”。仔细看看这些单词的拼写。简单的印刷错误会产生语法错误。
其余的问题归结为这个非常简单的警告:订购事项。您的订单应如下:
1)声明并创建对象; 2)声明并创建所有依赖对象; 3)配置对象; 4)操纵物体。
这意味着你的代码:
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Richter Scale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JFrame frame = new JFrame();
无法正常工作,因为您正在尝试使用尚未创建的帧。首先创建它,如下所示:
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Richter Scale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
同样适用于您的标签和按钮。
JLabel rictherlabel = new JLabel ("Ricther: ");
需要来之前
panel.add(label);