创建如下要求的一个窗口: (1)创建一个标题为“计算”的窗口,窗口中有两个文本框和一个按钮;
2013-01-16
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class BaiDuZhiDao_2310 extends JFrame {
private JTextField firstTextField = new JTextField();
private JTextField secondTextField = new JTextField();
private JButton btn = new JButton("计算");
public BaiDuZhiDao_2310(){
this.setTitle("计算");
this.setSize(200, 100);
this.setLayout(new GridLayout(3,1,5,5));
this.init();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
private void init(){
this.add(firstTextField);
this.add(secondTextField);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Integer num = 0 ;
try{
num = Integer.parseInt(firstTextField.getText());
}catch(NumberFormatException ex){
//输入非法
return;
}
secondTextField.setText(String.valueOf(num*num));
}
});
this.add(btn);
}
public static void main(String[] args) {
new BaiDuZhiDao_2310();
}
}
广告 您可能关注的内容 |