java图形界面计算器_java实现图形化界面计算器

本文实例为大家分享了java实现图形化界面计算器的具体代码,供大家参考,具体内容如下

最终效果图:

9c26ea0d8cf453fc23afe055ad5cd051.png

项目流程:

第一步:实现图形化界面(添加计算器的 Button 和 用于显示输入数字、输出结果的JTextField等)

565286f18f6af1e056fff23f1290ad0b.png

第二步:给按钮和文本框添加鼠标监听事件。

第三步:实现加减乘除、开方、平方、清零和退格功能。

开方运算:

5507ee647c6140df4b9e8376dc8d9683.png

平方运算:

dc477dd7d1437df88f8f986f0a073e42.png

加法运算:

793951299441368b941facdb5231a167.png

c891fb1384dc5dc516db16a641cd6637.png

减法运算:

8c19d533b59921546aa2f5a3d89d76af.png

c89c1575fb40fd8e76452499f5ef1243.png

乘法运算:

f2e3038ed7d9721934f70499a55bb88c.png

4debf0506d7f9414f1383360588537f1.png

除法运算:

9ff535b7443b7244bba51146c0a81b98.png

333325771c6e2c2b933db2cfda7eb3aa.png

完整项目代码:

package First_App;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Caculator extends JFrame{

/*

* 图形化界面设计

* */

private static final long serialVersionUID = 4907149509182425824L;

public Caculator(){

Container c = getContentPane(); //定义一个顶级容器c

setLayout(new GridLayout(2,1));//新建网格布局管理器,2行1列

JTextField jtf = new JTextField("0",40);//构造一个用指定文本和列初始化的新文本框--jtf

jtf.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式:居右对齐

JButton data0 = new JButton("0");

JButton data1 = new JButton("1");

JButton data2 = new JButton("2");

JButton data3 = new JButton("3");

JButton data4 = new JButton("4");

JButton data5 = new JButton("5");

JButton data6 = new JButton("6");

JButton data7 = new JButton("7");

JButton data8 = new JButton("8");

JButton data9 = new JButton("9");

JButton point = new JButton(".");

JButton equ = new JButton("=");

JButton plus = new JButton("+");

JButton minus = new JButton("-");

JButton mtp = new JButton("*");

JButton dvd = new JButton("/");

JButton sqr = new JButton("sqrt");

JButton root = new JButton("x^2");

JButton tg = new JButton("退格");

JButton ql = new JButton("清零");

JPanel jp = new JPanel(); //新建JPanel面板--jp

jp.setLayout(new GridLayout(4,5,5,5));//新建网格布局管理器(行数,列数,组件间的水平垂直间距)

jp.add(data7);

jp.add(data8);

jp.add(data9);

jp.add(plus);

jp.add(sqr);

jp.add(data4);

jp.add(data5);

jp.add(data6);

jp.add(minus);

jp.add(root);

jp.add(data1);

jp.add(data2);

jp.add(data3);

jp.add(mtp);

jp.add(ql);

jp.add(data0);

jp.add(point);

jp.add(equ);

jp.add(dvd);

jp.add(tg);

c.add(jtf);//将文本框jtf添加到顶级容器c中

c.add(jp);//将JPanel面板jp添加到顶级容器c中

setSize(400,300);

setTitle("计算器");

setVisible(true);

setResizable(false);//窗体大小由程序员决定,用户不能自由改变大小

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

/*

* *********************************************************

*

相关计算功能的实现

* *********************************************************

* */

data0.addActionListener(new ActionListener(){//数字0的输入

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){//将按钮值与0作比较

jtf.requestFocus();//把输入焦点放在调用这个方法的控件上(即把光标放在文本框jtf里)

}

else{

String str = jtf.getText();//取得当前按钮的按钮值

jtf.setText(str+"0"); //将文本内容后加上字符0

}

}

});

data1.addActionListener(new ActionListener(){//数字1的输入

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){//将按钮值与0作比较

jtf.setText("");//将文本框初始化为空

jtf.setText("1");//将文本框内容置为 1

jtf.requestFocus();//把输入焦点放在调用这个方法的控件上(即把光标放在文本框jtf里)

}

else{

String str = jtf.getText();//取得当前按钮的按钮值

jtf.setText(str+"1"); //将文本内容后加上字符1

}

}

});

data2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("2");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"2");

}

}

});

data3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("3");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"3");

}

}

});

data4.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("4");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"4");

}

}

});

data5.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("5");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"5");

}

}

});

data6.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("6");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"6");

}

}

});

data7.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("7");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"7");

}

}

});

data8.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("8");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"8");

}

}

});

data9.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("9");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"9");

}

}

});

point.addActionListener(new ActionListener(){ //点号的输入

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText(".");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+".");

}

}

});

plus.addActionListener(new ActionListener(){ //+号的输入

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("+");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"+");

}

}

});

minus.addActionListener(new ActionListener(){ //-号的输入

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("-");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"-");

}

}

});

mtp.addActionListener(new ActionListener(){ //*号的输入

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("*");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"*");

}

}

});

dvd.addActionListener(new ActionListener(){ //除号的输入

public void actionPerformed(ActionEvent arg0){

if(jtf.getText().equals("0")){

jtf.setText("");

jtf.setText("/");

jtf.requestFocus();

}

else{

String str = jtf.getText();

jtf.setText(str+"/");

}

}

});

//【**退格功能如下**】

tg.addActionListener(new ActionListener(){//监听退格键

public void actionPerformed(ActionEvent arg0){//处理退格键被按下的事件

String text = jtf.getText();

int i = text.length();

if(i>0){

text = text.substring(0,i-1);//去掉最后一个字符

if (text.length() == 0) {// 如果文本没有了内容,则初始化计算器的各种值

jtf.setText("0");

} else { // 显示新的文本

jtf.setText(text);

}

}

}

});

//【**清零功能如下**】

ql.addActionListener(new ActionListener(){//监听清零键

public void actionPerformed(ActionEvent e) {

jtf.setText("0");//将文本框置为0(清零功能)

}

});

//【**平方功能如下**】

root.addActionListener(new ActionListener(){//监听root键

public void actionPerformed(ActionEvent e){//root键被按事件

String i = jtf.getText();

Double j = Double.parseDouble(i);//将字符串i转换成对应的double类型的数值

double ans = j*j; //求平方

String answer =String.valueOf(ans);//将int型数据转换成String类型

jtf.setText(answer);//将文本框设置为平方后的结果

}

});

//【**开方功能如下**】

sqr.addActionListener(new ActionListener(){//监听sqrt键

public void actionPerformed(ActionEvent e){//sqrt键被按事件

String i = jtf.getText();

Double j = Double.parseDouble(i);//将字符串转换成对应的double类型的数值

double ans = (double)Math.sqrt(j);//求开方

String answer = String.valueOf(ans);//将double型数据转换成String类型

jtf.setText(answer);//将文本框设置为开方后的结果

}

});

//【等号实现 加减乘除 功能】

equ.addActionListener(new ActionListener(){ //监听 “等号” 按键

public void actionPerformed(ActionEvent arg0){//处理“等号” 按键被按下事件

//【**加法运算**】

if(jtf.getText().indexOf("+")!= -1){

//将字符串分割为子字符串,然后将结果作为字符串数组返回

String[] s = jtf.getText().split("[+]");//转义字符,要用"[+]"或者"\+"

Double d1 = Double.parseDouble(s[0]);//返回一个指定字符串表示的double值

Double d2 = Double.parseDouble(s[1]);

double ans = d1 + d2;

String answer = String.valueOf(ans);//将结果转换为字符串

jtf.setText(answer);//将加法运算的结果以字符串形式在文本框中显示

}

//【**减法运算**】

else if(jtf.getText().indexOf("-")!= -1){

String[] s = jtf.getText().split("-");

jtf.setText("");

Double d1 = Double.parseDouble(s[0]);

Double d2 = Double.parseDouble(s[1]);

double ans = d1-d2;

String answer =String.valueOf(ans);

jtf.setText(answer);

}

//【**乘法运算**】

else if(jtf.getText().indexOf("*")!= -1){

String[] s = jtf.getText().split("[*]");//*是转义字符,要用"[*]",或者"\*"

jtf.setText("");

Double d1 = Double.parseDouble(s[0]);

Double d2 = Double.parseDouble(s[1]);

double ans = d1*d2;

String answer =String.valueOf(ans);

jtf.setText(answer);

}

//【**除法运算**】

else if(jtf.getText().indexOf("/")!= -1){

String[] s = jtf.getText().split("/");

jtf.setText("");

Double d1 = Double.parseDouble(s[0]);

Double d2 = Double.parseDouble(s[1]);

double ans = d1/d2;

String answer =String.valueOf(ans);

jtf.setText(answer);

}

else{

jtf.setText("请选择要进行的运算");

}

}

});

}

public static void main(String[] args) {

new Caculator();

}

}

总结:

1.掌握基本的GUI添加按钮、文本框的方法

2.掌握字符串的处理,这里用到了indexOf()、split()等方法

3.注意Java中遇到的转义字符。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 8
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值