2011年4月1日 星期五

Get text

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AWT extends JFrame implements ActionListener
{
static JFrame myfrm=new JFrame("Button class");
static JTextField tex1=new JTextField("TextField");
static JButton but1=new JButton("點擊"); 
//static AWT myfrm=new AWT("Frame 1 ");
//static JTextField tbx1=new JTextField("JTextField");

public static void main(String args[])
{
AWT myfrm=new AWT();
GridLayout f = new GridLayout(3,3);
GridLayout f1 = new GridLayout(1,2);
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
JButton buttons[]=new JButton[10];
JPanel p = new JPanel(f);
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p.add(buttons[i], f); // 在視窗內加入按鈕1
}

JPanel k = new JPanel(f1);
k.add(tex1);
k.add(but1);
myfrm.setLayout(f);
myfrm.setSize(500,300);
but1.addActionListener(myfrm);
myfrm.add(p,f);
myfrm.add(k,f); 
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
stringValue=tex1.getText();
int intValue = Integer.parseInt(stringValue);
System.out.println(stringValue);

}
}