import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JButton btn1=new JButton("JButton 1");
static JTextField tbx1=new JTextField(" ");
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();
JButton buttons[]=new JButton[10];
String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8",};
//GridBagLayout border=new GridBagLayout ();
GridLayout border= new GridLayout(3,3);
GridLayout border2= new GridLayout(1,2);
JPanel p1 = new JPanel(border);
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i], border); // 在視窗內加入按鈕1
}
myfrm.add(p1);
JPanel p2 = new JPanel(border2);
myfrm.setLayout(border);
myfrm.setSize(500,500);
btn1.addActionListener(myfrm);
p2.add(tbx1);
p2.add(btn1);
myfrm.add(p2);
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
stringValue=tbx1.getText();
System.out.println(stringValue);
}
}
沒有留言:
張貼留言