实现一个Java GUI计算器应用程序界面

package gui;

import java.awt.*;

import java.awt.event.*;

 

import javax.swing.*;

 

 

/**

 * Windows操作系统自带的计算器是个很方便的小工具,

 * 利用Java的GUI编程,

 * 实现一个Java GUI计算器应用程序界面,

 * 窗口标题为“计算器”,窗口布局如下图所示,在此计算器应用程序中实现“+、-、*、/”运算操作。

 * 

 * @since 2010-9-20

 * @start 1:02 

 * @end 2:27

 * 

 * 

 * */

public class Three_Caculator {

public static void main(String[] args){

Caculator calc=new Caculator();

calc.init();

calc.start();

}

}

 

 

class Caculator extends JFrame implements ActionListener{

JButton num0=new JButton("0"),

num1=new JButton("1"),

num2=new JButton("2"),

num3=new JButton("3"),

num4=new JButton("4"),

num5=new JButton("5"),

num6=new JButton("6"),

num7=new JButton("7"),

num8=new JButton("8"),

num9=new JButton("9"),

plus=new JButton("+"),

minus=new JButton("-"),

times=new JButton("*"),

devid=new JButton("/"),

calc=new JButton("="),

clear=new JButton("Clear");

JTextArea text=new JTextArea();

JPanel panel1=new JPanel(),

  panel2=new JPanel(),

  panel4=new JPanel(),

  panel3=new JPanel();

String str="";

double input=0;

double output=0;

double sum=0;

int mark=0;

public void init(){

this.getContentPane().setLayout(new GridLayout(5,1));

this.setBounds(100,100,300,400);

 

this.getContentPane().add(text);

 

GridLayout mgr=new GridLayout(1,4);

panel1.setLayout(mgr);

panel2.setLayout(mgr);

panel3.setLayout(mgr);

panel4.setLayout(mgr);

this.getContentPane().add(panel1);

this.getContentPane().add(panel2);

this.getContentPane().add(panel3);

this.getContentPane().add(panel4);

panel1.add(num7);

panel1.add(num8);

panel1.add(num9);

panel1.add(plus);

panel2.add(num4);

panel2.add(num5);

panel2.add(num6);

panel2.add(minus);

panel3.add(num1);

panel3.add(num2);

panel3.add(num3);

panel3.add(times);

panel4.add(num0);

panel4.add(clear);

panel4.add(calc);

panel4.add(devid);

num0.addActionListener(this);

num1.addActionListener(this);

num2.addActionListener(this);

num3.addActionListener(this);

num4.addActionListener(this);

num5.addActionListener(this);

num6.addActionListener(this);

num7.addActionListener(this);

num8.addActionListener(this);

num9.addActionListener(this);

plus.addActionListener(this);

minus.addActionListener(this);

times.addActionListener(this);

clear.addActionListener(this);

devid.addActionListener(this);

calc.addActionListener(this);

this.setVisible(true);

this.validate();

}

public void start(){

text.setEditable(false);

}

 

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource().equals(num0)){

str+=0;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num1)){

str+=1;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num2)){

str+=2;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num3)){

str+=3;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num4)){

str+=4;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num5)){

str+=5;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num6)){

str+=6;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num7)){

str+="7";

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num8)){

str+=8;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num9)){

str+=9;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(num0)){

str+=0;

try{

input=Double.parseDouble(str);

}

catch(Exception exp){

text.setText("Input error!");

return;

}

}

if(e.getSource().equals(plus)){

this.calc(); 

 

mark=1;

 

}

if(e.getSource().equals(minus)){

this.calc(); 

 

mark=2;

 

 

}

if(e.getSource().equals(times)){

this.calc();

 

mark=3;

 

}

if(e.getSource().equals(devid)){

this.calc(); 

 

mark=4;

 

}

if(e.getSource().equals(clear)){

mark=0;

input=0;

str="";

output=0;

}

if(e.getSource().equals(calc)){

this.calc();

}

this.display();

 

}

public void calc(){

if(mark==0){

output=input;

}

 

if(mark==1){

output=output+input;

}

if(mark==2){

output=output-input;

}

if(mark==3){

output=output*input;

}

if(mark==4){

if(input!=0){

output=output/input;

}

}

 

input=0;

str="";

}

public void display(){

 

StringBuffer sb=new StringBuffer();

sb.append("Input:");

sb.append(input);

sb.append("/r/n");

sb.append("State:");

if(mark==1){

sb.append("+");

}

if(mark==2){

sb.append("-");

}

if(mark==3){

sb.append("*");

}

if(mark==4){

sb.append("/");

}

sb.append("/r/n");

 

sb.append(output);

sb.append("/r/n");

text.setText(sb.toString());

}

}

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值