標題:
[隨堂測驗] 點擊計數器
[打印本頁]
作者:
周政輝
時間:
2018-1-27 10:11
標題:
[隨堂測驗] 點擊計數器
本帖最後由 周政輝 於 2018-1-27 10:44 編輯
[attach]3254[/attach]
[attach]3255[/attach]
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.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main extends JFrame {
private JPanel contentPane;
private int count =0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setTitle("\u9EDE\u64CA\u8A08\u6578\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 389, 130);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel Count = new JLabel("0");
Count.setHorizontalAlignment(SwingConstants.CENTER);
Count.setBounds(37, 32, 67, 32);
contentPane.add(Count);
JButton btnNewButton = new JButton("\u9EDE\u64CA");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Count.setText(String.valueOf(++count));
}
});
btnNewButton.setBounds(129, 32, 93, 32);
contentPane.add(btnNewButton);
JButton button = new JButton("\u6B78\u96F6");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count=0;
Count.setText(String.valueOf(count));
}
});
button.setBounds(251, 32, 93, 32);
contentPane.add(button);
}
}
複製代碼
作者:
黃茂勛
時間:
2018-1-27 10:57
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);
}
}
複製代碼
作者:
陳泓瑜
時間:
2018-1-27 11:09
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 java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JTextField;
public class Click extends JFrame {
private JPanel contentPane;
private JTextField tf1;
public int click=0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Click frame = new Click();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Click() {
setTitle("\u9EDE\u64CA\u8A08\u6578\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 364, 93);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btn2 = new JButton("\u6B78\u96F6");
btn2.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
click=0;
tf1.setText(String.valueOf(click));
}
});
btn2.setBounds(250, 14, 88, 32);
contentPane.add(btn2);
JButton btn1 = new JButton("\u9EDE\u64CA");
btn1.setFont(new Font("微軟正黑體", Font.PLAIN, 16));
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
click++;
tf1.setText(String.valueOf(click));
}
});
btn1.setBounds(152, 13, 88, 32);
contentPane.add(btn1);
tf1 = new JTextField();
tf1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
tf1.setText(String.valueOf(click));
}
});
tf1.setFont(new Font("Consolas", Font.PLAIN, 14));
tf1.setBounds(10, 14, 132, 28);
contentPane.add(tf1);
tf1.setColumns(10);
}
}
複製代碼
作者:
蔡季樺
時間:
2018-1-27 11:18
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);
}}
複製代碼
作者:
蔡庭豪
時間:
2018-1-27 11:19
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.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main extends JFrame {
private JPanel contentPane;
private int count =0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 326, 171);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("0");
lblNewLabel.setBounds(10, 10, 98, 100);
contentPane.add(lblNewLabel);
JButton btnNewButton = new JButton("\u9EDE\u64CA");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblNewLabel.setText(String.valueOf(++count));
}
});
btnNewButton.setBounds(118, 49, 87, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u6B78\u96F6");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblNewLabel.setText("0");
count = 0;
}
});
btnNewButton_1.setBounds(213, 49, 87, 23);
contentPane.add(btnNewButton_1);
}
}
複製代碼
作者:
張健勳
時間:
2018-1-27 11:20
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.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class main extends JFrame {
private JPanel contentPane;
private int count =0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
main frame = new main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public main() {
setTitle("\u9EDE\u64CA\u8A08\u6578\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 322, 118);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel Count = new JLabel("0");
Count.setHorizontalAlignment(SwingConstants.CENTER);
Count.setBounds(14, 22, 67, 32);
contentPane.add(Count);
JButton btnNewButton = new JButton("\u9EDE\u64CA");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Count.setText(String.valueOf(++count));
}
});
btnNewButton.setBounds(88, 13, 93, 51);
contentPane.add(btnNewButton);
JButton button = new JButton("\u6B78\u96F6");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count=0;
Count.setText(String.valueOf(count));
}
});
button.setBounds(195, 13, 93, 51);
contentPane.add(button);
}
}
複製代碼
作者:
林侑成
時間:
2018-2-1 19:21
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.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main extends JFrame {
private JPanel contentPane;
private int count =0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setTitle("\u9EDE\u64CA\u8A08\u6578\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 389, 130);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel Count = new JLabel("0");
Count.setHorizontalAlignment(SwingConstants.CENTER);
Count.setBounds(37, 32, 67, 32);
contentPane.add(Count);
JButton btnNewButton = new JButton("\u9EDE\u64CA");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Count.setText(String.valueOf(++count));
}
});
btnNewButton.setBounds(129, 32, 93, 32);
contentPane.add(btnNewButton);
JButton button = new JButton("\u6B78\u96F6");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count=0;
Count.setText(String.valueOf(count));
}
});
button.setBounds(251, 32, 93, 32);
contentPane.add(button);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2