當前位置:編程學習大全網 - 編程語言 - 簡單計算器圖形編程代碼是什麽?

簡單計算器圖形編程代碼是什麽?

//java版本的,妳早晚能用到

package bag;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.SwingUtilities;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JTextField;

import java.awt.*;

import java.awt.event.*;

import javax.swing.JButton;

import java.awt.Dimension;

public class SCul extends JFrame {

private static final long serialVersionUID = 1L;

public JTextField jTextField1;

private static String s1,s2,s,s3,s4;

private static double result;

public Component createComponents(){

final JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20;

GridLayout gl1=new GridLayout(4,5);

JPanel panel1=new JPanel();

panel1.setLayout(gl1);

b1=new JButton("1"); b1.setText("1");b1.setForeground(Color.blue);

b2=new JButton("2"); b2.setText("2");b2.setForeground(Color.blue);

b3=new JButton("3"); b3.setText("3");b3.setForeground(Color.blue);

b6=new JButton("4"); b6.setText("4");b6.setForeground(Color.blue);

b7=new JButton("5"); b7.setText("5");b7.setForeground(Color.blue);

b8=new JButton("6"); b8.setText("6");b8.setForeground(Color.blue);

b11=new JButton("7"); b11.setText("7");b11.setForeground(Color.blue);

b12=new JButton("8"); b12.setText("8");b12.setForeground(Color.blue);

b13=new JButton("9"); b13.setText("9");b13.setForeground(Color.blue);

b16=new JButton("0"); b16.setText("0");b16.setForeground(Color.blue);

b4=new JButton("+"); b4.setText("+");b4.setForeground(Color.red);

b5=new JButton("C"); b5.setText("C");b5.setForeground(Color.red);

b9=new JButton("-"); b9.setText("-");b9.setForeground(Color.red);

b10=new JButton("退格"); b10.setText("退格");b10.setForeground(Color.red);

b14=new JButton("*"); b14.setText("*");b14.setForeground(Color.red);

b15=new JButton("1/x"); b15.setText("1/x");b15.setForeground(Color.blue);

b17=new JButton("+/-"); b17.setText("+/-");b17.setForeground(Color.blue);

b18=new JButton("."); b18.setText(".");b4.setForeground(Color.blue);

b19=new JButton("/"); b19.setText("/");b19.setForeground(Color.red);

b20=new JButton("="); b20.setText("=");b20.setForeground(Color.red);

//設置大小和顏色

b1.setSize(100, 100);

b2.setSize(100, 100);

b3.setSize(100, 100);

b4.setSize(100, 100);

b5.setSize(100, 100);

b6.setSize(100, 100);

b7.setSize(100, 100);

b8.setSize(100, 100);

b9.setSize(100, 100);

b10.setSize(100, 100);

b11.setSize(100, 100);

b12.setSize(100, 100);

b13.setSize(100, 100);

b14.setSize(100, 100);

b15.setSize(100, 100);

b16.setSize(100, 100);

b17.setSize(100, 100);

b18.setSize(100, 100);

b19.setSize(100, 100);

b20.setSize(100, 100);

panel1.add(b1);

panel1.add(b2);

panel1.add(b3);

panel1.add(b4);

panel1.add(b5);

panel1.add(b6);

panel1.add(b7);

panel1.add(b8);

panel1.add(b9);

panel1.add(b10);

panel1.add(b11);

panel1.add(b12);

panel1.add(b13);

panel1.add(b14);

panel1.add(b15);

panel1.add(b16);

panel1.add(b17);

panel1.add(b18);

panel1.add(b19);

panel1.add(b20);

panel1.setVisible(true);

b1.addActionListener(new ActionListener(){//按1

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b1.getText().toString()));

s=jTextField1.getText().toString();

}

});

b2.addActionListener(new ActionListener(){//按2

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b2.getText().toString()));

s=jTextField1.getText().toString();

}

});

b3.addActionListener(new ActionListener(){//按3

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b3.getText().toString()));

s=jTextField1.getText().toString();

}

});

b6.addActionListener(new ActionListener(){// 按4

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b6.getText().toString()));

s=jTextField1.getText().toString();

}

});

b7.addActionListener(new ActionListener(){//按5

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b7.getText().toString()));

s=jTextField1.getText().toString();

}

});

b8.addActionListener(new ActionListener(){//按6

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b8.getText().toString()));

s=jTextField1.getText().toString();

}

});

b11.addActionListener(new ActionListener(){//按7

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b11.getText().toString()));

s=jTextField1.getText().toString();

}

});

b12.addActionListener(new ActionListener(){//按8

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b12.getText().toString()));

s=jTextField1.getText().toString();

}

});

b13.addActionListener(new ActionListener(){//按9

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b13.getText().toString()));

s=jTextField1.getText().toString();

}

});

b16.addActionListener(new ActionListener(){//按0

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b16.getText().toString()));

s=jTextField1.getText().toString();

}

});

b4.addActionListener(new ActionListener(){//按+

public void actionPerformed(ActionEvent e){

if(s=="")

{

s="0";

jTextField1.setText(s);

}

else{

s1=s;//將第壹操作數保存至s1

s3="+";

jTextField1.setText(jTextField1.getText().concat(b4.getText().toString()));

s=jTextField1.getText().toString();

}

}

});

b9.addActionListener(new ActionListener(){//按-

public void actionPerformed(ActionEvent e){

if(s=="")

{

s="0";

jTextField1.setText(s);

}

else{

s1=s;//將第壹操作數保存至s1

s3="-";

jTextField1.setText(jTextField1.getText().concat(b9.getText().toString()));

s=jTextField1.getText().toString();

}

}

});

b14.addActionListener(new ActionListener(){//按*

public void actionPerformed(ActionEvent e){

if(s=="")

{

s="0";

jTextField1.setText(s);

}

else{

s1=s;//將第壹操作數保存至s1

s3="*";

jTextField1.setText(jTextField1.getText().concat(b14.getText().toString()));

s=jTextField1.getText().toString();

}

}

});

b19.addActionListener(new ActionListener(){//按/

public void actionPerformed(ActionEvent e){

if(s=="")

{

s="0";

jTextField1.setText(s);

}

else{

s1=s;//將第壹操作數保存至s1

s3="/";

jTextField1.setText(jTextField1.getText().concat(b19.getText().toString()));

s=jTextField1.getText().toString();

}

}

});

b5.addActionListener(new ActionListener(){//按C清零

public void actionPerformed(ActionEvent e){

s="0";

jTextField1.setText(s);

}

});

b10.addActionListener(new ActionListener(){//按退格

public void actionPerformed(ActionEvent e){

int l=s.length();

if(l!=0)

s=s.substring(0, l-1);//刪除壹個字符

else

{

s="0";

s4="+";

}

jTextField1.setText(s);//將刪除後的字符串顯示到單行編輯框中

}

});

b15.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){//求倒數

if(s=="" || s=="0")

{

s="0";

jTextField1.setText("0");

}

else{

result=1/Double.parseDouble(s);

s=String.valueOf(result);

jTextField1.setText(s4+s);

}

}

});

b17.addActionListener(new ActionListener(){//按+/-

public void actionPerformed(ActionEvent e){

if(s=="")

{

jTextField1.setText("0");

}else{

if(s4=="+")

{ s4="-";

jTextField1.setText(s4+s);

}

else{

s4="+";

jTextField1.setText(s4+s);

}

}

}

});

b18.addActionListener(new ActionListener(){//按.

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b18.getText().toString()));

s=jTextField1.getText().toString();

}

});

b20.addActionListener(new ActionListener(){//按=求結果

public void actionPerformed(ActionEvent e){//求結果

if(s=="")

{

s="0";

s4="+";

s3="";

jTextField1.setText(s);

}

else if(s!=""){

int m1=s.indexOf(s3);

s1=s.substring(0, m1);

s2=s.substring(m1+1, s.length());

if(s4=="+"){

if(s3=="+")

result=Double.parseDouble(s1)+Double.parseDouble(s2);

if(s3=="-")

result=Double.parseDouble(s1)-Double.parseDouble(s2);

if(s3=="*")

result=Double.parseDouble(s1)*Double.parseDouble(s2);

if(s3=="/" && s2!="0" && s2!="")

result=Double.parseDouble(s1)/Double.parseDouble(s2);

if(s3=="/" && (s2=="0" || s2==""))

result=00.00;

s=String.valueOf(result);

}

else if(s4=="-"){

if(s3=="+"){

result=Double.parseDouble(s2)-Double.parseDouble(s1);

s=String.valueOf(result);

}

if(s3=="-"){

result=Double.parseDouble(s1)+Double.parseDouble(s2);

s="-"+String.valueOf(result);

}

if(s3=="*"){

result=Double.parseDouble(s1)*Double.parseDouble(s2);

s="-"+String.valueOf(result);

}

if(s3=="/" && (s2!="0" || s2!="")){

result=Double.parseDouble(s1)/Double.parseDouble(s2);

s="-"+String.valueOf(result);

}

if(s3=="/" && s2=="0" && s2=="")

{

s="00.00";

}

}

jTextField1.setText(s);

}

}

});

return panel1;

}

public static void main(String[] args) {

SCul cul1=new SCul();

JFrame frame1=new JFrame("計算器");

frame1.setBackground(Color.lightGray);

frame1.add(cul1.jTextField1,BorderLayout.NORTH);

frame1.setResizable(false);

Component contents=cul1.createComponents();

frame1.getContentPane().add(contents, BorderLayout.SOUTH);

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame1.pack();

frame1.setVisible(true);

}

/**

* This is the default constructor

*/

public SCul() {

super();

jTextField1=new JTextField();

s="";

s1="";

s2="";

s3="";

s4="+";

jTextField1.setText(s);

jTextField1.setHorizontalAlignment(JTextField.RIGHT);

}

}

壹個簡單的計算器如圖所示,使用“計算器”可以完成任意的的標準運算,“計算器”可用於基本的算術運算,比如加減運算等。

  • 上一篇:IT培訓分享程序員需要註意哪些軟件編程
  • 下一篇:linux下安裝opencv必需要先安裝ffmpeg嗎
  • copyright 2024編程學習大全網