import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class button
{
public static void main(String[] args)
{
JButton jbnButton[]=new JButton[10];
JFrame jtfMainFrame = new JFrame("愛翹課");
jtfMainFrame.setSize(450, 150);
JPanel jplPanel = new JPanel();
for(int i=1;i<10;i++){
jbnButton[i]=new JButton();
jbnButton[i].setSize(50, 50);
jbnButton[i].setText(Integer.toString(i));
jplPanel.add(jbnButton[i]);
String s = "" + i;
//以下程式碼類似再按鈕裝上一個"耳朵"
jbnButton[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,s); //呼叫MsgBox
System.out.println(s); //讓按鈕數字顯示在黑畫面上
}
});
}
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
//把Panel加進去Fram
jtfMainFrame.setVisible(true); //讓Fram顯示出來
}
}
留言列表