import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test
{
public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(450, 150);
JButton jbnButton1 = new JButton("Button 1");
JButton jbnButton2 = new JButton("Button 2");/*9個按鈕*/
JButton jbnButton3 = new JButton("Button 3");
JButton jbnButton4 = new JButton("Button 4");
JButton jbnButton5 = new JButton("Button 5");
JButton jbnButton6 = new JButton("Button 6");
JButton jbnButton7 = new JButton("Button 7");
JButton jbnButton8 = new JButton("Button 8");
JButton jbnButton9 = new JButton("Button 9");
jbnButton1.addActionListener(new ActionListener() { /*控制按扭動作*/
public void actionPerformed(ActionEvent e) {
System.out.print("button1"); /*按了會在CMD顯示*/
}
});
jbnButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("button2");
}
});
jbnButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("button3");
}
});
jbnButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("button4");
}
});
jbnButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("button5");
}
});
jbnButton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("button6");
}
});
jbnButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("button7");
}
});
jbnButton8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("button8");
}
});
jbnButton9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("button9");
}
});
JPanel jplPanel = new JPanel();
jplPanel.add(jbnButton1);
jplPanel.add(jbnButton2);
jplPanel.add(jbnButton3);
jplPanel.add(jbnButton4);
jplPanel.add(jbnButton5);
jplPanel.add(jbnButton6);
jplPanel.add(jbnButton7);
jplPanel.add(jbnButton8);
jplPanel.add(jbnButton9);
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
System.out.print("press ctrl+c to reset!");
}
}
沒有留言:
張貼留言