實現效果
友情提示:建議您把代碼複製到自己的編譯器裏去研究算法,博文看着實屬有些累,前提您需要一定的 java 基礎哦~
網格袋類
package jsj;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class LayoutUtil {
//參數含義介紹 1. Container container 容器對象
// 2. int fill 填充方式
// 3. int anchor 停靠位置(對齊方式)
// 4. int weightx 水平權值 與填充方式有內在關係
// 5. int weighty 垂直權值 與填充方式有內在關係
// 6. int x 水平起點
// 7. int y 垂直起點
// 8. int width 水平寬度(水平佔鋸的單元格數)
// 9. int height 垂直寬度(垂直佔鋸的單元格數)
// 10. Component comp 組件對象
public static void add(Container container,int fill,
int anchor,int weightx,int weighty,
int x,int y,int width,int height,Component comp){
GridBagConstraints constraints=new GridBagConstraints();
constraints.fill=fill;
constraints.anchor=anchor;
constraints.weightx=weightx;
constraints.weighty=weighty;
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=width;
constraints.gridheight=height;
container.add(comp,constraints);
} // 參數含義同上,只新增一個 外邊距參數
public static void add(Container container,int fill,
int anchor,int weightx,int weighty, // Insets insets 外邊距填充方式
int x,int y,int width,int height,Component comp,Insets insets){
GridBagConstraints constraints=new GridBagConstraints();
constraints.insets=insets;
constraints.fill=fill;
constraints.anchor=anchor;
constraints.weightx=weightx;
constraints.weighty=weighty;
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=width;
constraints.gridheight=height;
container.add(comp,constraints);
}
}
計算器的實現
package jsj;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import jsj.LayoutUtil;
public class Calculator extends JFrame {
static int count1=0;//計數第一位數點擊按鈕次數,用來判斷小數位數,除以多少個10
static int count2=0;//計數第二位數點擊按鈕次數,用來判斷小數位數,除以多少個10
static int k=0; //判斷是否點過運算符+-*/
static float a=0; //存第一位計算數
static float b=0; //存第二位計算數
static float c=0; //存運算結果
static int pointt=0;//判斷是否點過小數點 .
static int jiat=0;//判斷是否點過加號+
static int jiant=0;//判斷是否點過減號-
static int chengt=0;//判斷是否點過乘號*
static int chut=0;//判斷是否點過除號/
/*各個按鈕*/
JButton jia=new JButton("+");
JButton jian=new JButton("-");
JButton cheng=new JButton("*");
JButton chu=new JButton("/");
JButton one=new JButton("1");
JButton two=new JButton("2");
JButton three=new JButton("3");
JButton four=new JButton("4");
JButton five=new JButton("5");
JButton six=new JButton("6");
JButton seven=new JButton("7");
JButton eight=new JButton("8");
JButton nine=new JButton("9");
JButton zero=new JButton("0");
JButton clean=new JButton("CE");
JButton point=new JButton(".");
JButton result=new JButton("=");
/*顯示文字與運算結果面板*/
JPanel showJPanel=new JPanel();
/*顯示最上面文字標籤*/
JLabel showresult2=new JLabel("鳳兮鳳兮歸故鄉,遨遊四海求其凰");
/*顯示運算結果標籤*/
JLabel showresult=new JLabel("0");
/*存放各個按鈕面板*/
JPanel show=new JPanel();
public static void main(String[] args) {
Calculator calculator=new Calculator();
calculator.setVisible(true);
}
public Calculator(){
super();
setTitle("計算器");//設置框體標題
setBounds(300, 300, 300, 250);//設置框體默認大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
showresult2.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null,
null));//給最上方文字加邊框
showresult.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null,
null));//給運算結果加邊框
showresult2.setForeground(new Color(255, 0, 0));//給最上方文字設置紅色
showresult2.setFont(new Font("", Font.BOLD, 15));//給最上方文字設置大小
showJPanel.setLayout(new GridBagLayout());//對顯示文字與運算結果面板設置網格袋佈局
LayoutUtil.add(showJPanel,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,0,0,1,1,showresult2);
LayoutUtil.add(showJPanel,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,0,1,1,1,showresult);
getContentPane().add(showJPanel,BorderLayout.NORTH);//將面板放在內容窗格北方
show.setLayout(new GridBagLayout());//對存放各個按鈕面板設置網格袋佈局
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,3,0,1,1,clean);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,0,1,1,1,seven);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,1,1,1,1,eight);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,2,1,1,1,nine);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,3,1,1,1,jia);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,0,2,1,1,four);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,1,2,1,1,five);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,2,2,1,1,six);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,3,2,1,1,jian);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,0,3,1,1,one);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,1,3,1,1,two);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,2,3,1,1,three);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,3,3,1,1,cheng);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,0,4,1,1,point);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,1,4,1,1,zero);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,2,4,1,1,result);
LayoutUtil.add(show,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,3,4,1,1,chu);
getContentPane().add(show,BorderLayout.SOUTH);//將面板放在內容窗格南方
point.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
pointt=1;//如果點擊小數點,設置pointt=1計數
/*令計數開始計數小數位個數*/
count1=0;
count2=0;
}
});
clean.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//清空數值
a=0;
c=0;
b=0;
pointt=0;
count1=0;count2=0;
/*將float類型轉化成String類型,因爲setText()放String類型參數*/
String d=Float.toString(c);
showresult.setText(d);
}
});
/*請翻到nine,這裏以nine爲例解釋算法 ,zero,one,two,three,four,five,six,seven,eight原理均同nine*/
zero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {
a=a*10+0;
count1++;
if(pointt==1) {
a=(float) (a/Math.pow(10, count1));
String d=Float.toString(a);
showresult.setText(d);
a=(float) (a*Math.pow(10, count1));
}
else{
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){
b=b*10+0;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {
a=a*10+1;
count1++;
if(pointt==1) {
a=(float) (a/Math.pow(10, count1));
String d=Float.toString(a);
showresult.setText(d);
a=(float) (a*Math.pow(10, count1));
}
else{
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){
b=b*10+1;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
two.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {
a=a*10+2;
count1++;
if(pointt==1) {
a=(float) (a/Math.pow(10, count1));
String d=Float.toString(a);
showresult.setText(d);
a=(float) (a*Math.pow(10, count1));
}
else{
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){
b=b*10+2;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
three.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {
a=a*10+3;
count1++;
if(pointt==1) {
a=(float) (a/Math.pow(10, count1));
String d=Float.toString(a);
showresult.setText(d);
a=(float) (a*Math.pow(10, count1));
}
else{
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){
b=b*10+3;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
four.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {
a=a*10+4;
count1++;
if(pointt==1) {
a=(float) (a/Math.pow(10, count1));
String d=Float.toString(a);
showresult.setText(d);
a=(float) (a*Math.pow(10, count1));
}
else{
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){
b=b*10+4;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
five.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {
a=a*10+5;
count1++;
if(pointt==1) {
a=(float) (a/Math.pow(10, count1));
String d=Float.toString(a);
showresult.setText(d);
a=(float) (a*Math.pow(10, count1));
}
else{
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){
b=b*10+5;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {
a=a*10+6;
count1++;
if(pointt==1) {
a=(float) (a/Math.pow(10, count1));
String d=Float.toString(a);
showresult.setText(d);
a=(float) (a*Math.pow(10, count1));
}
else{
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){
b=b*10+6;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
seven.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {
a=a*10+7;
count1++;
if(pointt==1) {
a=(float) (a/Math.pow(10, count1));
String d=Float.toString(a);
showresult.setText(d);
a=(float) (a*Math.pow(10, count1));
}
else{
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){
b=b*10+7;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
eight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {
a=a*10+8;
count1++;
if(pointt==1) {
a=(float) (a/Math.pow(10, count1));
String d=Float.toString(a);
showresult.setText(d);
a=(float) (a*Math.pow(10, count1));
}
else{
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){
b=b*10+8;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
/*算法解釋:*/
nine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(k==0) {//輸入第一位數
a=a*10+9;//輸入爲999...一個個輸入,拼湊規則(初始化a=0) 9=0*10+9 -> 99=9*10+9->999=99*10+9...
count1++;//count1加一
if(pointt==1) {//如果點擊小數點後(point按鍵部分令count1=0,count1從頭開始即計數小數位數字個數)
a=(float) (a/Math.pow(10, count1));//Math.pow(10,count1)即10的count1次方,此次獲得a的小數形式如9.99僅爲了顯示
String d=Float.toString(a);
showresult.setText(d);//顯示a的小數形式
a=(float) (a*Math.pow(10, count1));//顯示後變回a的整數形式999(爲了按鍵下一個小數位此時,count1隨按鍵次數增加而增加一,因此小數點會根據按鍵次數“往前移”)
}
else{//爲涉及小數點的整數輸出
String d=Float.toString(a);
showresult.setText(d);
}
}
if(k==1){//輸入第二位數,下面原理同第一個數的輸入
b=b*10+9;
count2++;
if(pointt==1) {
b=(float) (b/Math.pow(10, count2));
pointt=0;
String d=Float.toString(b);
showresult.setText(d);
b=(float) (b*Math.pow(10, count2));
}
else{
String d=Float.toString(b);
showresult.setText(d);
}
}
}
});
jia.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
/*當點擊加號後,k=1即下面點擊數字輸入的是b的值,jiat用來在=輸出結果時計算方式判斷*/
k=1;
jiat=1;
if(pointt==1) {a=(float) (a/Math.pow(10, count1));pointt=0;}//涉及小數點,前面是爲了顯示,這次第一個數不在輸入,則這個a的值用於計算
}
});
/*減乘除號原理均同加號原理*/
jian.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
k=1;
jiant=1;
if(pointt==1) {a=(float) (a/Math.pow(10, count1));pointt=0;}
}
});
cheng.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
k=1;
chengt=1;
if(pointt==1) {a=(float) (a/Math.pow(10, count1));pointt=0;}
}
});
chu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
k=1;
chut=1;
if(pointt==1) {a=(float) (a/Math.pow(10, count1));pointt=0;}
}
});
result.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(pointt==1) {//涉及小數點,前面是爲了顯示,這次第二個數不在輸入,則這個b的值用於計算
pointt=0;
b=(float) (b/Math.pow(10, count2));
}
if(k==1) {//點擊過運算符
if(jiat==1) {c=a+b;a=c;jiat=0;}//最後把c的值給a,爲了銜接計算,即如2+3+4...
else if(jiant==1) {c=a-b;a=c;jiant=0;}
else if(chengt==1) {c=a*b;a=c;chengt=0;}
else if(chut==1) {c=a/b;chut=0;a=c;}
String d=Float.toString(c);
showresult.setText(d);
}
else {//未點過運算符,輸入a的值後直接點=號,則直接輸出a的值
String d=Float.toString(a);
showresult.setText(d);
}
/*計算完後重新初始化(a沒必要初始化,因爲要銜接計算)*/
count1=0;count2=0;
k=0;b=0;chengt=0;jiat=0;
// 思考:如果不銜接計算了,a的值又要爲0重新開始輸入,那麼a=0放在什麼位置,否則會把上一次的c結果值如22給a進行下一次a的輸入,我們再輸個3,會變成223,不符合實際情況,我們應該只需要3
}
});
}
}