周三 GUI

GUI  组件 事件 监听器

主要由两个包java.awt和java.Swing定义  javaFX最终将取代Swing

then这是一个简单的GUI

首先安排好面板类

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PushCounterPanel extends JPanel {
private int count;
private JButton incrementButton;
private JLabel countLabel;

public PushCounterPanel() {
count = 0;

incrementButton = new JButton("点击增加");
countLabel = new JLabel("次数: " + count);
setBackground(Color.red);
setPreferredSize(new Dimension(300,40));

incrementButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
count++;
countLabel.setText("次数: " + count);
}
});

add(incrementButton);
add(countLabel);
}
}
然后是主程序
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("hellowzy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PushCounterPanel panel = new PushCounterPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
运行就是这样

 我觉得循序渐进吧,这一章内容真的多,一次学太多我也记不住

posted @ 2023-07-05 16:46  菜鸟de博客  阅读(70)  评论(0)    收藏  举报