package jsq;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jsq {
public static void main(String args[])
{
new AppFrame();
}
}
class AppFrame extends JFrame
{
JTextField in = new JTextField();
JButton btn = new JButton();
JLabel out = new JLabel();
public AppFrame()
{
setLayout(new FlowLayout());
getContentPane().add(in);
getContentPane().add(btn);
getContentPane().add(out);
btn.addActionListener(new BtnActionAdapter());
setSize(400,100);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
class BtnActionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = in.getText();
double d = Double.parseDouble(s);
double sq = d*d;
out.setText(d+"的平方是:"+sq);
}
}
}
我是用eclipse编辑的,学习视频中是用appletviewer编辑的(用ecilipse是因为之前学过一点,现在想自己自学一下)