java设置文本框-按钮的一个简单例子(加法器)

package pag;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class helloworld implements ActionListener
{
JFrame AdderFrame;
JTextField TOprand1;
JTextField TOprand2;
JLabel LAdd,LSum;
JButton BAdd,BClear;
JPanel JP1,JP2;
public helloworld()
{
AdderFrame=new JFrame("AdderFrame");
TOprand1=new JTextField("0.0");
TOprand2=new JTextField("0.0");
LAdd=new JLabel("+");
LSum=new JLabel("=   ");
BAdd=new JButton("Add"); 
BClear=new JButton("Clear");
JP1=new JPanel();
JP2=new JPanel();
BAdd.addActionListener(this);
BClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{         
TOprand1.setText("0.0");
TOprand2.setText("0.0");
LSum.setText("=");
}
});
AdderFrame.add(JP1);
JP1.add(TOprand1);
JP1.add(LAdd);
JP1.add(TOprand2);
JP1.add(LSum);


AdderFrame.add(JP2);
JP2.add(BAdd);
JP2.add(BClear);
AdderFrame.getContentPane().setLayout(new BorderLayout());
AdderFrame.getContentPane().add(JP1,BorderLayout.NORTH);
AdderFrame.getContentPane().add(JP2,BorderLayout.SOUTH);

AdderFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
AdderFrame.pack();
AdderFrame.setVisible(true);
AdderFrame.setResizable(false);
AdderFrame.setSize(250,100);
}
public void actionPerformed(ActionEvent event)
{

double sum=(double)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand2.getText()).doubleValue());
LSum.setText("="+sum);
}

public static void main(String[] args)
{
helloworld adder=new helloworld();
}
}

你可能感兴趣的:(java)