[英]What's wrong with my code?
该代码也正在编译和执行,但是方法actionPerformed()
不能正确执行。 我的意思是,单击“确定”按钮后, JTextField
没有任何内容。 即使使用e.getSource()
之后也不会执行任何操作。 System.out.println("I am done ")
工作正常,但是t.setText("Hey there")
无法工作。 代码有什么问题?? 如果有人可以,请帮助我。
并且您能否详细说明一下为什么不在Panel
上添加JButton
和JTextField
不可见? 为什么添加面板以使按钮和文本字段可见很重要。 没有它,为什么不可见?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class A implements ActionListener {
JFrame f;
JButton b;
JPanel p;
JLabel l;
JTextField t;
A(String s) {
JFrame f=new JFrame(s);
f.setVisible(true);
f.setSize(400,400);
JButton b= new JButton("OK");
JTextField t=new JTextField();
JPanel p=new JPanel();
f.add(p);
p.setBounds(0,0,300,300);
p.add(b);
b.setBounds(30,40,80,80);
p.add(t);
t.setBounds(100,200,80,80);
b.addActionListener(this);
t.addActionListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b) {
t.setText("Hey There");
}
System.out.println("I m done!!");
}
public static void main(String[] args) {
System.out.println("Hey there");
new A("First App");
}
}
通过在构造函数中重新声明b
变量,使阴影变暗,这意味着该类中的b
字段与GUI中显示的字段不同。 不要这样做,问题就解决了。
即,您正在执行以下操作:
class A {
JButton b; // this guy stays null!
public A() {
JButton b = new JButton(...);
}
解决方案是这样做:
class A {
JButton b;
public A() {
b = new JButton(...); //note the difference?
}
你问:
为什么添加面板以使按钮和文本字段在摆动中可见很重要?
这是布局管理器的问题。 默认情况下,JPanel使用FlowLayout,它允许您添加组件,例如从左上角在书架中填写书籍。 另一方面,JFrame的contentPane使用BorderLayout,如果默认情况下向其添加内容,则仅显示最后添加的内容,并将填充GUI。 请注意,您正在尝试setBounds(...)
并且不应该这样做。 让布局管理器执行此操作。
您正在隐藏构造函数中的所有字段。
删除重新声明,它将按预期工作。
像这样:
A(String s) {
f = new JFrame(s);
f.setVisible(true);
f.setSize(400, 400);
b = new JButton("OK");
t = new JTextField();
p = new JPanel();
f.add(p);
p.setBounds(0, 0, 300, 300);
p.add(b);
b.setBounds(30, 40, 80, 80);
p.add(t);
t.setBounds(100, 200, 80, 80);
b.addActionListener(this);
t.addActionListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
注意,我们没有重新声明任何这些变量。 这就是为什么您的活动没有触发的原因; 声明的字段仍然为null
。
为什么不使用匿名内部类而不是做什么呢?
就像是:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class A {
JFrame f;
JButton b;
JPanel p;
JLabel l;
JTextField t;
A(String s) {
f = new JFrame(s);
f.setVisible(true);
f.setSize(400, 400);
p = new JPanel();
p.setBounds(0, 0, 300, 300);
t = new JTextField();
t.setBounds(100, 200, 80, 80);
p.add(t);
b = new JButton("OK");
b.setBounds(30, 40, 80, 80);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t.setText("Hello! World.");
}
});
p.add(b);
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
System.out.println("Hey there");
new A("First App");
}
}
我没有执行此代码,但我确信这将起作用,并且这种方法将帮助您更好地组织代码。 这就是我通常编写代码的方式。
如果您使用的是Java 8,则最好使用lambda表达式,因为ActionListener
是单个抽象方法接口。
如果您想看看用途,那就是这样:
b.addActionListener(e -> t.setText("Hello! World."));
为了更好地理解您可以看一下这个视频。 它用于JavaFX,但是匿名内部类和lambda表达式的概念保持不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.