- package bbs.istak.org.tw;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import java.awt.BorderLayout;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.KeyEvent;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- public class Main {
- public JFrame frame;
- private final JLabel lblNewLabel = new JLabel("0");
- int c=0;
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Main window = new Main();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
-
- public Main() {
- initialize();
- }
- private void initialize() {
- frame = new JFrame("點擊計數器");
- frame.setBounds(100, 100, 324, 92);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
-
-
- JButton btnNewButton = new JButton("點擊");
- btnNewButton.setBounds(97, 10, 94, 41);
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- lblNewLabel.setText(String.valueOf(++c));
- }
- });
-
- JButton btnNewButton_1 = new JButton("歸零");
- btnNewButton_1.setBounds(201, 10, 94, 42);
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- c=0;
- lblNewLabel.setText(String.valueOf(c));
- }
- });
- frame.getContentPane().setLayout(null);
- lblNewLabel.setBounds(34, 13, 24, 35);
- frame.getContentPane().add(lblNewLabel);
- frame.getContentPane().add(btnNewButton);
- frame.getContentPane().add(btnNewButton_1);
- }
- }
複製代碼 |