import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Square extends JFrame implements ActionListener {
JLabel jl1, jl2;
JTextField jtf1, jtf2;
JButton jb1, jb2;
public Square() {
jl1 = new JLabel("X", JLabel.CENTER);
jl2 = new JLabel("X^2", JLabel.CENTER);
jtf1 = new JTextField();
jtf2 = new JTextField();
jtf2.setEditable(false);
jb1 = new JButton("计算");
jb2 = new JButton("取消");
this.setLayout(new GridLayout(3, 2));
this.add(jl1);
this.add(jtf1);
this.add(jl2);
this.add(jtf2);
this.add(jb1);
jb1.addActionListener(this);
this.add(jb2);
jb2.addActionListener(this);
this.setSize(400, 300);
this.setTitle("平方计算");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Square();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == this.jb1) {
jtf2.setText(String.valueOf(Math.pow(
Double.valueOf(jtf1.getText()), 2)));
jtf2.setVisible(true);
} else if (e.getSource() == this.jb2) {
System.exit(0);
}
}
}