java awt實現(xiàn)計算器功能
本文實例為大家分享了java awt實現(xiàn)計算器的具體代碼,供大家參考,具體內(nèi)容如下

上課老師演示了一遍他的寫法,由于沒給代碼,因此按著他的思路擼了一遍,感覺還很簡單。
代碼以及解釋如下:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Caculate1 {
static String s1="";
static String s2="";
static int f=9;
static int num1=0,num2=0;
public static void main(String[] args) {
int x=0,y=0,z=0;
JFrame jf=new JFrame( "我的計算器");
jf.setBounds(0,0,300,400);
// jf.setBackground(bgColor);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTextField show=new JTextField("0");
//此處為布局:也就是定義五個容器,(也可以理解為將定義的JFrame空間分為5個容器)
JPanel jp1=new JPanel(new GridLayout(1,3));//存儲:
JPanel jp2=new JPanel(new GridLayout(1,3));
JPanel jp3=new JPanel(new GridLayout(1,3));
JPanel jp4=new JPanel(new GridLayout(1,3));
JPanel jp5=new JPanel(new GridLayout(1,3));
JButton bt_add=new JButton("+");
JButton bt_sub=new JButton("-");
JButton bt_mul=new JButton("*");
JButton bt_div=new JButton("/");
JButton bt_7=new JButton("7");
JButton bt_8=new JButton("8");
JButton bt_9=new JButton("9");
JButton bt_4=new JButton("4");
JButton bt_5=new JButton("5");
JButton bt_6=new JButton("6");
JButton bt_1=new JButton("1");
JButton bt_2=new JButton("2");
JButton bt_3=new JButton("3");
JButton bt_0=new JButton("0");
JButton bt_c=new JButton("C");
JButton bt_equal=new JButton("=");
jf.setLayout(new GridLayout(6,1));
//容器1 :添加 '+','-','*','/'按鈕
jp1.add(bt_add);
jp1.add(bt_sub);
jp1.add(bt_mul);
jp1.add(bt_div);
//容器2:添加'7','8','9'按鈕
jp2.add(bt_7);
jp2.add(bt_8);
jp2.add(bt_9);
//容器3:添加'4','5','6'按鈕
jp3.add(bt_4);
jp3.add(bt_5);
jp3.add(bt_6);
//容器4:添加'1','2','3'按鈕
jp4.add(bt_1);
jp4.add(bt_2);
jp4.add(bt_3);
//容器5:添加'0','C','='
jp5.add(bt_0);
jp5.add(bt_c);
jp5.add(bt_equal);
jf.add(show);
jf.add(jp1);
jf.add(jp2);
jf.add(jp3);
jf.add(jp4);
jf.add(jp5);
//元素已經(jīng)定義好了,思路也很簡單:(1)JFrame定義一個大容器jf,jf= 1個顯示框(show)+5個容器,分別定義顯示框和容器,
//(2)類似樹形添加元素的數(shù)據(jù)結(jié)構(gòu),為jf添加元素
//下面便是時間監(jiān)聽了(又稱加載驅(qū)動)
bt_9.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+9;
show.setText(s1);
}
});
bt_8.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+8;
show.setText(s1);
}
});
bt_7.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+7;
show.setText(s1);
}
});
bt_6.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+6;
show.setText(s1);
}
});
bt_5.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+5;
show.setText(s1);
}
});
bt_4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+4;
show.setText(s1);
}
});
bt_3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+3;
show.setText(s1);
}
});
bt_0.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+0;
show.setText(s1);
}
});
bt_2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+2;
show.setText(s1);
}
});
bt_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+1;
show.setText(s1);
}
});
bt_0.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1=s1+0;
show.setText(s1);
}
});
bt_equal.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
num1=Integer.valueOf(s2);
num2=Integer.valueOf(s1);
int z=0;
char fg=' ';
switch(f){
case 0:
z=num1+num2;
fg='+';
break;
case 1:
z=num1-num2;
fg='-';
break;
case 2:
z=num1*num2;
fg='*';
break;
case 3:
fg='/';
if(num2==0)
num2=num2+1;
z=num1/num2;
break;
default:
break;
}
show.setText(s2+" "+fg+" "+s1+" = "+z);
}
});
bt_add.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s2=s1;
s1="";
f=0;
show.setText(s2+"+");
}
});
bt_sub.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
s2 = s1;
s1 = "";
f = 1;
show.setText(s2 + " - ");
}
});
bt_mul.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
s2 = s1;
s1 = "";
f = 2;
show.setText(s2 + " * ");
}
});
bt_div.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
s2 = s1;
s1 = "";
f = 3;
show.setText(s2 + " / ");
}
});
bt_c.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
s2 = "";
s1 = "";
num1 = 0;
num2 = 0;
f = 9;
show.setText("0");
}
});
}
}
關(guān)于計算器的精彩文章請查看《計算器專題》 ,更多精彩等你來發(fā)現(xiàn)!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決springboot接入springfox-swagger2遇到的一些問題
這篇文章主要介紹了解決springboot接入springfox-swagger2遇到的一些問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
MyBatis-Plus多數(shù)據(jù)源的示例代碼
本文主要介紹了MyBatis-Plus多數(shù)據(jù)源的示例代碼,包括依賴配置、數(shù)據(jù)源配置、Mapper 和 Service 的定義,具有一定的參考價值,感興趣的可以了解一下2024-05-05
多模塊項目引入SpringSecurity后一直報404的解決方案
這篇文章主要介紹了多模塊項目引入SpringSecurity后一直報404的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
java連接zookeeper實現(xiàn)zookeeper教程
這篇文章主要介紹了java連接zookeeper實現(xiàn)zookeeper教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
java中的Io(input與output)操作總結(jié)(一)
所謂IO,也就是Input與Output的縮寫。在java中,IO涉及的范圍比較大,這里主要討論針對文件內(nèi)容的讀寫,感興趣的朋友可以了解下2013-01-01
Java實現(xiàn)Excel數(shù)據(jù)驗證功能
在Java中,開發(fā)者可以使用一些開源的庫(如Apache POI)來添加、修改和處理Excel中的數(shù)據(jù),下面我們就來看看如何使用Java實現(xiàn)添加,修改和刪除Excel數(shù)據(jù)驗證吧2023-10-10

