2015年5月8日 星期五

顯示選取數字

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class button implements ActionListener
{
public static void main(String[] args)
{new button();}
public button(){
JButton jbnButton[]=new JButton[17];
JFrame jtfMainFrame = new JFrame("愛翹課");
jtfMainFrame.setSize(450, 450);
JPanel jplPanel = new JPanel();
//使用GridLayout在Panel裡面切出4x4的格子,使按鈕排整齊
jplPanel.setLayout(new GridLayout(4,4));
for(int i=1;i<17;i++){
jbnButton[i]=new JButton();
jbnButton[i].setSize(50, 50);
jbnButton[i].setText(Integer.toString(i));
jbnButton[i].setActionCommand(""+i);
jbnButton[i].addActionListener(this);
jplPanel.add(jbnButton[i]);
}
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
//把Panel加進去Fram
jtfMainFrame.setVisible(true); //讓Fram顯示出來
}

public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();JOptionPane.showMessageDialog(null,"此數字為"+cmd); }
}

沒有留言:

張貼留言