我正在 java 中使用 awt 包编写程序,但未添加我的组件
I am writing a program using awt pakage in java but my components are not getting added
我正在使用 AWT 包编写一个程序,其中我还实现了 ActionListner,它显示了我在按钮上进行的点击次数。
package awtlistenerdemo;
import java.awt.*;
import java.awt.event.*;
public class AwtListenerDemo implements ActionListener {
TextField t1 = new TextField(30);
Button b;
int count = 0;
Frame f;
AwtListenerDemo() {
f = new Frame("Action Listener Example");
b = new Button("Ok");
f.setLayout(null);
b.setBounds(100, 100, 60, 20);
t1.setBounds(100, 200, 80, 30);
f.add(b);
f.add(t1);
b.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
count++;
t1.setText("Button Clicked" + count + "Times");
}
}
public static void main(String[] args) {
Frame f = new Frame("Action Listener Example");
f.setVisible(true);
f.setSize(300, 300);
}
}
TextField t1 = new TextField(30);
Button b;
int count = 0;
Frame f;
AwtListenerDemo() {
b = new Button("Ok");
f = new Frame("Action Listener Example");
f.setSize(300, 300);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
f.dispose();
}
});
f.setLayout(null);
b.setBounds(100, 100, 60, 20);
t1.setBounds(100, 200, 80, 30);
f.add(b);
f.add(t1);
f.setVisible(true);
b.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
count++;
t1.setText("Button Clicked" + count + "Times");
}
}
public static void main(String[] args) {
new AwtListenerDemo();
}
main
方法从不构造 AwtListenerDemo
,因此您看到的只是用该方法创建的标准空白框架。解决该问题后,需要将 main 方法中的一些语句移至构造函数中并应用于它创建的框架。
这是结果:
import java.awt.*;
import java.awt.event.*;
public class AwtListenerDemo implements ActionListener {
TextField t1 = new TextField(30);
Button b;
int count = 0;
Frame f;
AwtListenerDemo() {
f = new Frame("Action Listener Example");
b = new Button("Ok");
f.setLayout(null);
b.setBounds(100, 100, 60, 20);
t1.setBounds(100, 200, 80, 30);
f.add(b);
f.add(t1);
b.addActionListener(this);
// ADD this!
f.setSize(300,300);
// then set it VISIBLE
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
count++;
t1.setText("Button Clicked" + count + "Times");
}
}
public static void main(String[] args) {
// Change the main code to..
new AwtListenerDemo();
}
}
一般提示
- 请参阅 this answer 以了解放弃 AWT 组件以支持 Swing 的许多充分理由。除此之外,当今时代很少有人使用过 AWT(因此无法帮助解决问题),或者即使他们使用过,也忘记了更详细的信息(因此给出错误的建议)。如链接的答案中所述,Swing 是基于 AWT 构建的,但在进行转换时存在需要 unlearned 的差异。认真地说,从 Swing 开始或直接转到 Java-FX(一个完全不同的 GUI 工具包,既不基于 Swing 也不基于 AWT)。 警告:这是我唯一一次帮助您修复损坏的 AWT 代码。 (3)
- Java GUI 必须在不同的 OS'、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them along with layout padding and borders for white space。即使在那个简短的示例中也可以看到(这些问题的开始),因为:a)按钮,文本字段或 both 不是居中对齐的。如果框架稍微调整大小,它将是 both。 b) 文本字段的大小猜测也是错误的 - 显示文本的长度不够!
- 与(1)相关,这段代码还存在一些问题。我已经回答了问题所需的最低限度,不会再做更多了。
我正在使用 AWT 包编写一个程序,其中我还实现了 ActionListner,它显示了我在按钮上进行的点击次数。
package awtlistenerdemo;
import java.awt.*;
import java.awt.event.*;
public class AwtListenerDemo implements ActionListener {
TextField t1 = new TextField(30);
Button b;
int count = 0;
Frame f;
AwtListenerDemo() {
f = new Frame("Action Listener Example");
b = new Button("Ok");
f.setLayout(null);
b.setBounds(100, 100, 60, 20);
t1.setBounds(100, 200, 80, 30);
f.add(b);
f.add(t1);
b.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
count++;
t1.setText("Button Clicked" + count + "Times");
}
}
public static void main(String[] args) {
Frame f = new Frame("Action Listener Example");
f.setVisible(true);
f.setSize(300, 300);
}
}
TextField t1 = new TextField(30);
Button b;
int count = 0;
Frame f;
AwtListenerDemo() {
b = new Button("Ok");
f = new Frame("Action Listener Example");
f.setSize(300, 300);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
f.dispose();
}
});
f.setLayout(null);
b.setBounds(100, 100, 60, 20);
t1.setBounds(100, 200, 80, 30);
f.add(b);
f.add(t1);
f.setVisible(true);
b.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
count++;
t1.setText("Button Clicked" + count + "Times");
}
}
public static void main(String[] args) {
new AwtListenerDemo();
}
main
方法从不构造 AwtListenerDemo
,因此您看到的只是用该方法创建的标准空白框架。解决该问题后,需要将 main 方法中的一些语句移至构造函数中并应用于它创建的框架。
这是结果:
import java.awt.*;
import java.awt.event.*;
public class AwtListenerDemo implements ActionListener {
TextField t1 = new TextField(30);
Button b;
int count = 0;
Frame f;
AwtListenerDemo() {
f = new Frame("Action Listener Example");
b = new Button("Ok");
f.setLayout(null);
b.setBounds(100, 100, 60, 20);
t1.setBounds(100, 200, 80, 30);
f.add(b);
f.add(t1);
b.addActionListener(this);
// ADD this!
f.setSize(300,300);
// then set it VISIBLE
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
count++;
t1.setText("Button Clicked" + count + "Times");
}
}
public static void main(String[] args) {
// Change the main code to..
new AwtListenerDemo();
}
}
一般提示
- 请参阅 this answer 以了解放弃 AWT 组件以支持 Swing 的许多充分理由。除此之外,当今时代很少有人使用过 AWT(因此无法帮助解决问题),或者即使他们使用过,也忘记了更详细的信息(因此给出错误的建议)。如链接的答案中所述,Swing 是基于 AWT 构建的,但在进行转换时存在需要 unlearned 的差异。认真地说,从 Swing 开始或直接转到 Java-FX(一个完全不同的 GUI 工具包,既不基于 Swing 也不基于 AWT)。 警告:这是我唯一一次帮助您修复损坏的 AWT 代码。 (3)
- Java GUI 必须在不同的 OS'、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them along with layout padding and borders for white space。即使在那个简短的示例中也可以看到(这些问题的开始),因为:a)按钮,文本字段或 both 不是居中对齐的。如果框架稍微调整大小,它将是 both。 b) 文本字段的大小猜测也是错误的 - 显示文本的长度不够!
- 与(1)相关,这段代码还存在一些问题。我已经回答了问题所需的最低限度,不会再做更多了。