我刚加入,很高兴来到这里~ 所以,今天早上(凌晨 2 点左右,但这不是重点:P(我用 JFrame 和其他 GUI 的东西做了一些 Java 测试。这是我第一次使用 GUI。我试图做一个小小的Java应用程序,作为一个梦想的日记员。但是,当我遇到无法解决的问题时,我的进度就冻结了。我的代码如下。
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class Display extends Canvas
{
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static String defaultEntry = "Dreams...";
public static final String TITLE = "Dream Journal Testing";
Button erase;
public static void main(String[] args)
{
Display d = new Display();
d.create();
}
public void create()
{
JFrame frame = new JFrame();
System.out.println("Running");
Panel cardOne = new Panel();
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel grid = new Panel();
cardOne.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,1,3,6));
TextArea textArea1 = new TextArea(defaultEntry);
/*Font f1 = new Font("Courier", Font.PLAIN, 16);
setFont(f1);*/
Label l1 = new Label("Welcome to the Dream Journal! :)");
Label l2 = new Label("Type your dream below:");
p1.add(l1);
p1.add(l2);
p2.add(textArea1);
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
Button ok = new Button("Save");
erase = new Button("Erase");
p3.add(erase);
p3.add(ok);
cardOne.add("North",p1);
cardOne.add("Center",p2);
cardOne.add("South",p3);
frame.add(cardOne);
//frame.add(cardOne);
//frame.setLocationRelativeTo(null);
frame.pack();
frame.setTitle(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(textArea1.getText());
}
/*public boolean handleEvent(Event evt)
{
if(evt.target == erase)
{
System.out.println("it works");
return true;
}
else return super.handleEvent(evt);
}
*/
public boolean action(Event evt, Object arg)
{
if("Erase".equals(arg))
{
System.out.println("hello");
//textArea1.setText("");
}
return true;
}
}
我遇到的问题是我无法弄清楚如何做到这一点,因此如果按下"擦除"AWT 按钮,系统将打印一行(作为测试(。我试过了 公共布尔操作(事件 evt,对象参数(和 公共布尔句柄事件,但两者都不起作用。有人对我这个Java菜鸟有什么建议吗?谢谢!!:)
一种方法是在按钮中添加一个操作侦听器(例如,用于保存(。 另一种方法是创建一个Action
(例如,用于擦除(。
除非必要,否则不要将 Swing 与 AWT 组件混合使用。 在这个时间点,甚至不值得学习如何使用 AWT 组件,只使用 Swing 以获得最佳结果和最佳帮助。
这是该应用程序的一个版本。 使用所有 Swing 组件。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Display
{
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static String defaultEntry = "Dreams...";
public static final String TITLE = "Dream Journal Testing";
JButton erase;
public static void main(String[] args)
{
Display d = new Display();
d.create();
}
public void create()
{
JFrame frame = new JFrame();
System.out.println("Running");
JPanel cardOne = new JPanel();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
cardOne.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,1,3,6));
JTextArea textArea1 = new JTextArea(defaultEntry);
JLabel l1 = new JLabel("Welcome to the Dream Journal! :)");
JLabel l2 = new JLabel("Type your dream below:");
p1.add(l1);
p1.add(l2);
p2.add(textArea1);
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton ok = new JButton("Save");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Do " + ae.getActionCommand());
}
});
erase = new JButton(new EraseAction());
p3.add(erase);
p3.add(ok);
// Use the constants
cardOne.add(BorderLayout.PAGE_START,p1);
cardOne.add(BorderLayout.CENTER,p2);
cardOne.add(BorderLayout.PAGE_END,p3);
frame.add(cardOne);
frame.pack();
frame.setTitle(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(textArea1.getText());
}
}
class EraseAction extends AbstractAction {
EraseAction() {
super("Erase");
}
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Do " + arg0.getActionCommand());
}
}
首先让我向您解释事件处理程序的基础......
- 首先有Event Source
,当事件源上发生任何操作时,Event Object
被抛出到call back
方法。
- Call Back
方法是Listener
(接口(内的方法,需要由实现此侦听器的Class
实现。
- 此回调方法中的语句将指示在事件源上执行操作时需要执行的操作。
例如:
假设
Event Source - Button
When Clicked - Event object is thrown at the call back method
Call back method - actionPerformed(ActionEvent e) inside ActionListener.
现在您的情况:
现在这可以通过两种方式完成.....
1.让你Display
类实现ActionListener
,然后注册button
ActionListener
,最后实现 ActionListener 的抽象方法actionPerformed()
。
例如:
public class Display extends Canvas implements ActionListener{
public Display(){
// Your code....
setComponent(); // Initializing the state of Components
}
public void setComponent(){
// Your code.........
Button b = new Button("Click");
b.addActionListener(this); // Registering the button.
// Your code..........
}
public void actionPerformed(ActionEvent event) {
// Do here whatever you want on the Button Click
}
}
2.使用Anonymous
类。
- 匿名类同时声明和初始化。
- 匿名类必须实现或扩展到一个interface
或class
您的 Display 类不会在此处实现 ActionListener...。
public class Display extends Canvas {
public Display(){
// Your code....
setComponent(); // Initializing the state of Components
}
public void setComponent(){
// Your code.........
Button b = new Button("Click");
// Registering the button and Implementing it
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
// Do here whatever you want on the Button Click
}
});
// Your code..........
}
}
你需要实现ActionListner
:
public class Display extends Canvas implements ActionListener
并将自己添加到您的按钮中,如下所示:
erase.addActionListener(this);
然后实现所需的方法:
public void actionPerformed(ActionEvent event) {
//do stuff
}
有关详细信息,请查看有关创建操作侦听器的教程。您会发现这种可观察的模式在Java GUI中被广泛使用。
几个高层次的批评:
- 当有类似但更新(阅读:更灵活(的 Swing 组件可用(即
JButton
(时,您正在使用许多较旧的 AWT 组件(即Button
(。看看这个,快速解释一下差异。 - 您实现的事件模型在 1997 年已修改为我上面建议的可观察模式。 如果您想了解更多信息,可以阅读此内容。