返回列表 發帖
  1. package bbs.istak.org.tw;

  2. import java.awt.EventQueue;

  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;

  5. import java.awt.BorderLayout;

  6. import javax.swing.JButton;

  7. import java.awt.event.ActionListener;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.KeyEvent;

  10. import javax.swing.GroupLayout;
  11. import javax.swing.GroupLayout.Alignment;

  12. public class Main {

  13.         public JFrame frame;
  14.         private final JLabel lblNewLabel = new JLabel("0");
  15. int c=0;
  16.         public static void main(String[] args) {
  17.                 EventQueue.invokeLater(new Runnable() {
  18.                         public void run() {
  19.                                 try {
  20.                                         Main window = new Main();
  21.                                         window.frame.setVisible(true);
  22.                                 } catch (Exception e) {
  23.                                         e.printStackTrace();
  24.                                 }
  25.                         }
  26.                 });
  27.         }
  28.        

  29.    
  30.         public Main() {
  31.                 initialize();
  32.         }

  33.         private void initialize() {
  34.                 frame = new JFrame("點擊計數器");
  35.                 frame.setBounds(100, 100, 324, 92);
  36.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.                 frame.getContentPane().setLayout(null);
  38.                
  39.                
  40.                 JButton btnNewButton = new JButton("點擊");
  41.                 btnNewButton.setBounds(97, 10, 94, 41);
  42.                 btnNewButton.addActionListener(new ActionListener() {
  43.                         public void actionPerformed(ActionEvent e) {                                            
  44.                                 lblNewLabel.setText(String.valueOf(++c));                                          
  45.                         }
  46.                 });
  47.                
  48.                 JButton btnNewButton_1 = new JButton("歸零");
  49.                 btnNewButton_1.setBounds(201, 10, 94, 42);
  50.                 btnNewButton_1.addActionListener(new ActionListener() {
  51.                         public void actionPerformed(ActionEvent e) {
  52.                                 c=0;
  53.                                 lblNewLabel.setText(String.valueOf(c));
  54.                         }
  55.                 });
  56.                 frame.getContentPane().setLayout(null);
  57.                 lblNewLabel.setBounds(34, 13, 24, 35);
  58.                 frame.getContentPane().add(lblNewLabel);
  59.                 frame.getContentPane().add(btnNewButton);
  60.                 frame.getContentPane().add(btnNewButton_1);
  61.         }
  62. }
複製代碼

TOP

返回列表