文本字段、标签和按钮 [英] Text Fields, Labels and Buttons
问题描述
我在理解 GUI 以及为什么我的程序无法正常运行时遇到了一些困难.是因为我必须扩展到 JFrame 类吗?这是一个代码:
I am having some difficulty understanding GUIs and why my program won't run properly. Is it because I have to extend to a JFrame class? Here is a code:
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 等的符号.
I am getting a lot of errors stating that the program cannot find the symbols for my ActionListeners etc.
错误是:
_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");
推荐答案
Wow... 好吧,首先,ActionListener
被错误地拼写为ActionListner
".仔细查看这些单词的拼写.简单的印刷错误会产生语法错误.
Wow... OK, first, ActionListener
is spelled incorrectly as "ActionListner
." Look very closely at the spelling of those words. Simple typographical errors generate syntax errors.
您的其余问题归结为这个非常简单的警告:订单很重要.您的订单应如下所示:
The rest of your problems boil down to this very simple caveat: order matters. Your order should be as follows:
1) 声明和创建对象;2) 声明并创建所有依赖对象;3)配置对象;4) 操作对象.
1) declare and create objects; 2) declare and create all dependent objects; 3) configure objects; 4) manipulate objects.
这意味着您的代码:
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Richter Scale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JFrame frame = new JFrame();
不起作用,因为您试图弄乱尚未创建的框架.首先创建它,如下所示:
Won't work because you're trying to mess with a frame that hasn't yet been created. Create it first, like so:
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Richter Scale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
同样适用于您的标签和按钮.
Same holds true for your label and your button.
JLabel rictherlabel = new JLabel ("Ricther: ");
需要先来
panel.add(label);
这篇关于文本字段、标签和按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!