Java程序设计之加法器的实现

问题描述:请使用Java编程语言设计一个普通加法器。

程序源码:

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

public class Adder implements ActionListener
{
	JFrame AdderFrame;
	JTextField TOprand1;
	JTextField TOprand2;
	JLabel LAdd,LSum;
	JButton BAdd,BClear;
	JPanel JP1,JP2;
	public Adder()
	{
		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)
	{
		Adder adder=new Adder();
	}
}

 

你可能感兴趣的:(Java编程,加法器,Java程序设计,Java编程技术馆)