- package bbs.istak.org.tw;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JTextField;
- public class BMI extends JFrame {
- private JPanel contentPane;
- private int num=0;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- BMI frame = new BMI();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public BMI() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(50, 50, 200, 200);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
-
-
- JTextField textField = new JTextField();
- textField.setText(String.valueOf(num));
- textField.setBounds(0, 65, 55, 21);
- contentPane.add(textField);
- textField.setColumns(10);
-
- JButton btnNewButton = new JButton("New button");
- btnNewButton.setText("歸零");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- num=0;
- textField.setText(String.valueOf(num));
- }
- });
- btnNewButton.setBounds(122, 64, 62, 23);
- contentPane.add(btnNewButton);
-
- JButton btnNewButton_1 = new JButton("New button");
- btnNewButton_1.setText("點擊");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- num+=1;
- textField.setText(String.valueOf(num));
- }
- });
- btnNewButton_1.setBounds(58, 64, 62, 23);
- contentPane.add(btnNewButton_1);
-
-
- }}
複製代碼 |