[英]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.