初学JAVA,笔记:
package windows;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class WindowFlow extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JTextField text1,text2;
WindowFlow(String s){
super(s);
FlowLayout flow=new FlowLayout();
setLayout(flow);
text1=new JTextField(10);
text2=new JTextField(10);
text2.setEditable(false);
add(text1);
add(text2);
text1.addActionListener(this);
setBounds(100,100,330,70);
validate();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==text1){
int n=0,m=0;
try{
n=Integer.parseInt(text1.getText());
m=n*n;
text2.setText(n+"的平方是:"+m);
}
catch(Exception ee){
text2.setText("请输入数字字符!");
text1.setText(null);
}
}
}
}
public class JButtonE {
public static void main(String args[]){
new WindowFlow("JAVA控件示例程序");
}
}