java gui按钮点完后打印
时间: 2023-06-14 19:07:28 浏览: 56
要在Java GUI中打印按钮点击事件的信息,您需要完成以下步骤:
1. 创建一个JFrame窗口。
2. 在窗口中添加一个JButton。
3. 为按钮添加一个ActionListener,用于处理按钮点击事件。
4. 在ActionListener中,使用System.out.println()方法打印按钮点击事件的信息。
以下是一个示例代码,演示如何在Java GUI中打印按钮点击事件的信息:
```java
import javax.swing.*;
import java.awt.event.*;
public class ButtonExample extends JFrame implements ActionListener {
private JButton button;
public ButtonExample() {
// 创建窗口
super("Button Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建按钮
button = new JButton("Click me!");
button.addActionListener(this);
// 添加按钮到窗口
getContentPane().add(button);
// 设置窗口大小和位置
setSize(300, 200);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
ButtonExample example = new ButtonExample();
example.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// 打印按钮点击事件的信息
System.out.println("Button clicked!");
}
}
```
运行这个程序,点击按钮后,将会在控制台中看到“Button clicked!”的输出信息。