java实现一个简单的计算器界面,加上适当的事件处理,完成计算功能。
2个回答
展开全部
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
// public static final int EXIT_ON_CLOSE = 3;
private Button plus=new Button("+"); //运算符
private Button minus=new Button("-");
private Button multip=new Button("*");
private Button divise=new Button("/");
private Panel pan=new Panel();
private JLabel opl1 = new JLabel("操作数1");
private JLabel opl2 = new JLabel("操作数2");
private JLabel resl = new JLabel("结果:");
private TextField op1=new TextField(10);
private TextField op2=new TextField(10);
private TextField result=new TextField(10);
public Calculator ()
{
super("Calculator");
initialize();
}
public void initialize()
{
this.setLayout(null);
this.setSize(360,300);
this.setResizable(false);
this.add(pan);
pan.setBounds(10,10,330,250);
pan.setLayout(new GridLayout(5,1));
pan.add(opl1);
// opl1.setBounds(10,10, 100, 40);
pan.add(op1);
// op1.setBounds(120,10,100,40);
pan.add(opl2);
// opl2.setBounds(21,0,100,40);
pan.add(op2);
// op2.setBounds(21,110 , 100, 40);
pan.add(resl);
// resl.setBounds(42,0 , 100, 40);
pan.add(result);
// result.setBounds(42,100,100,40);
plus.addActionListener(this);
minus.addActionListener(this);
multip.addActionListener(this);
divise.addActionListener(this);
pan.add(plus);
pan.add(minus);
pan.add(multip);
pan.add(divise);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==plus)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作数不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1+oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()==minus)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作数不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1-oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()== multip)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作数不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1*oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()==divise)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作数不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1/oprt2;
result.setText(temp.toString());
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Calculator();
}
}
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
// public static final int EXIT_ON_CLOSE = 3;
private Button plus=new Button("+"); //运算符
private Button minus=new Button("-");
private Button multip=new Button("*");
private Button divise=new Button("/");
private Panel pan=new Panel();
private JLabel opl1 = new JLabel("操作数1");
private JLabel opl2 = new JLabel("操作数2");
private JLabel resl = new JLabel("结果:");
private TextField op1=new TextField(10);
private TextField op2=new TextField(10);
private TextField result=new TextField(10);
public Calculator ()
{
super("Calculator");
initialize();
}
public void initialize()
{
this.setLayout(null);
this.setSize(360,300);
this.setResizable(false);
this.add(pan);
pan.setBounds(10,10,330,250);
pan.setLayout(new GridLayout(5,1));
pan.add(opl1);
// opl1.setBounds(10,10, 100, 40);
pan.add(op1);
// op1.setBounds(120,10,100,40);
pan.add(opl2);
// opl2.setBounds(21,0,100,40);
pan.add(op2);
// op2.setBounds(21,110 , 100, 40);
pan.add(resl);
// resl.setBounds(42,0 , 100, 40);
pan.add(result);
// result.setBounds(42,100,100,40);
plus.addActionListener(this);
minus.addActionListener(this);
multip.addActionListener(this);
divise.addActionListener(this);
pan.add(plus);
pan.add(minus);
pan.add(multip);
pan.add(divise);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==plus)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作数不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1+oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()==minus)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作数不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1-oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()== multip)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作数不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1*oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()==divise)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作数不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1/oprt2;
result.setText(temp.toString());
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Calculator();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询