java计算器布局代码 java计算器的布局

求JAVA语言写的计算器的代码。用GridLayout布局。

package com.citi.Util;

创新互联-专业网站定制、快速模板网站建设、高性价比钟楼网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式钟楼网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖钟楼地区。费用合理售后完善,十余年实体公司更值得信赖。

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

/**

* @author yc80549

*

*/

public class Calculator extends JFrame implements ActionListener {

/**

*

*

*/

private static final long serialVersionUID = 1L;

private JTextField expression = new JTextField();

private JPanel buttonContainer = new JPanel();

private JPanel operators = new JPanel();

private JButton b1 = new JButton("1");

private JButton b2 = new JButton("2");

private JButton b3 = new JButton("3");

private JButton b4 = new JButton("4");

private JButton b5 = new JButton("5");

private JButton b6 = new JButton("6");

private JButton b7 = new JButton("7");

private JButton b8 = new JButton("8");

private JButton b9 = new JButton("9");

private JButton b0 = new JButton("0");

private JButton bpoint = new JButton(".");

private JButton bracket1 = new JButton("(");

private JButton bracket2 = new JButton(")");

private JButton plus = new JButton("+");

private JButton minus = new JButton("-");

private JButton multiply = new JButton("*");

private JButton divide = new JButton("/");

private JButton getResult = new JButton("=");

private JButton backSpace = new JButton("Backspace");

private JButton clear = new JButton("C");

private JButton OFF = new JButton("OFF");

private StringCalculate2 cacu = new StringCalculate2();

public Calculator() {

super();

setVisible(true);

this.setLayout(new BorderLayout());

this.setSize(400, 220);

Toolkit tk = Toolkit.getDefaultToolkit();

int width = (int) tk.getScreenSize().getWidth();

int height = (int) tk.getScreenSize().getHeight();

this.setLocation((int) ((width - getSize().getWidth()) / 2),

(int) ((height - getSize().getHeight()) / 2));

this.locate();

this.addAllListener();

}

public void locate() {

buttonContainer.setLayout(new GridLayout(4, 4));

buttonContainer.add(b1);

buttonContainer.add(b2);

buttonContainer.add(b3);

buttonContainer.add(plus);

buttonContainer.add(b4);

buttonContainer.add(b5);

buttonContainer.add(b6);

buttonContainer.add(minus);

buttonContainer.add(b7);

buttonContainer.add(b8);

buttonContainer.add(b9);

buttonContainer.add(multiply);

buttonContainer.add(b0);

buttonContainer.add(bpoint);

buttonContainer.add(getResult);

buttonContainer.add(divide);

operators.add(bracket1);

operators.add(bracket2);

operators.add(backSpace);

operators.add(clear);

operators.add(OFF);

this.add(expression, BorderLayout.NORTH);

this.add(buttonContainer, BorderLayout.CENTER);

this.add(operators, BorderLayout.SOUTH);

}

public void addAllListener() {

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

b0.addActionListener(this);

plus.addActionListener(this);

multiply.addActionListener(this);

divide.addActionListener(this);

minus.addActionListener(this);

bpoint.addActionListener(this);

bracket1.addActionListener(this);

bracket2.addActionListener(this);

getResult.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

double result = cacu.parse(expression.getText());

expression.setText("" + result);

} catch (Exception e1) {

System.err

.println("invaild expression or RuntimeException");

}

}

});

clear.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

expression.setText("");

}

});

OFF.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(1);

}

});

backSpace.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

expression.setText(expression.getText().substring(0,

expression.getText().length() - 1));

}

});

}

public void actionPerformed(ActionEvent e) {

JButton source = (JButton) e.getSource();

expression.setText(expression.getText() + source.getText());

}

public static void main(String[] args) {

Calculator cal = new Calculator();

cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

package com.citi.Util;

import java.util.Stack;

import javax.swing.JOptionPane;

public class StringCalculate2 {

private StackDouble numbers = new StackDouble();

private StackCharacter chs = new StackCharacter();

private boolean compare(char str) {

if (chs.empty()) {

return true;

}

char last = (char) chs.lastElement();

switch (str) {

case '*': {

if (last == '+' || last == '-')

return true;

else

return false;

}

case '/': {

if (last == '+' || last == '-')

return true;

else

return false;

}

case '+':

return false;

case '-':

return false;

}

return true;

}

public Double caculate(String str) {

StringBuffer sb = new StringBuffer(str);

StringBuffer num = new StringBuffer();

String tem = null;

char next;

while (sb.length() 0) {

tem = sb.substring(0, 1);

sb.delete(0, 1);

if (isNum(tem.trim())) {

num.append(tem);

} else {

if (num.length() 0 !"".equals(num.toString().trim())) {

Double d = new Double(num.toString().trim());

numbers.push(d);

num.delete(0, num.length());

}

if (!chs.isEmpty()) {

while (!compare(tem.charAt(0))) {

caculate();

}

}

if (numbers.isEmpty()) {

num.append(tem);

} else {

chs.push(new Character(tem.charAt(0)));

}

next = sb.charAt(0);

if (next == '-') {

num.append(next);

sb.delete(0, 1);

}

}

}

Double d = new Double(num.toString().trim());

numbers.push(d);

while (!chs.isEmpty()) {

caculate();

}

return numbers.pop();

}

public void caculate() {

Double b = numbers.pop();

Double a = null;

a = numbers.pop();

char ope = chs.pop();

Double result = null;

switch (ope) {

case '+':

result = a + b;

numbers.push(result);

break;

case '-':

result = a - b;

numbers.push(result);

break;

case '*':

result = a * b;

numbers.push(result);

break;

case '/':

result = a / b;

numbers.push(result);

break;

}

}

private boolean isNum(String num) {

return num.matches("[0-9.]");

}

public Double parse(String str) {

int start = 0;

StringBuffer sts = new StringBuffer(str);

int end = -1;

while ((end = sts.indexOf(")")) 0) {

String s = sts.substring(start, end + 1);

int first = s.lastIndexOf("(");

Double value = caculate(sts.substring(first + 1, end));

sts.replace(first, end + 1, value.toString());

}

return caculate(sts.toString());

}

public static void main(String[] args) {

String exp = JOptionPane

.showInputDialog("please input expression you want to caculate!");

if (exp.trim().equals("")) {

System.out.println("you have input nothing");

return;

}

StringCalculate2 sc = new StringCalculate2();

JOptionPane.showMessageDialog(null, "the result is: " + sc.parse(exp));

}

}

求Java计算器标准的布局代码

是用awt和swing做的,很好看比单单用AWT做好看

import java.awt.*;

import javax.swing.*;

public class Conputer extends JFrame {

protected Container con = getContentPane();// 指向内容面板

protected JMenuBar a = new JMenuBar();// 菜单条

protected JMenu a1 = new JMenu("编辑(E)");// 菜单1

protected JMenu a2 = new JMenu("查看(V)");// 菜单2

protected JMenu a3 = new JMenu("帮助(H)");// 菜单3

protected JMenuItem a11 = new JMenuItem("复制(C)", 'C');// 菜单1的菜单项

protected JMenuItem a12 = new JMenuItem("粘贴(P)", 'P');// 菜单1的菜单项

protected JMenuItem a21 = new JMenuItem("标准型(T)", 'T');// 菜单2的菜单项

protected JMenuItem a22 = new JMenuItem("科学型(S)", 'S');// 菜单2的菜单项

protected JMenuItem a31 = new JMenuItem("帮助主题(H)", 'H');// 菜单3的菜单项

protected JTextField jtf = new JTextField(30);// 文本框

protected JButton[] jb = new JButton[27];

protected String[] arr = { "Backspace", "CE", "C", "MC", "7", "8", "9", "/", "sqrt",

"MR", "4", "5", "6", "*", "%", "MS", "1", "2", "3", "-", "1/x",

"M+", "0", "+/-", ".", "+", "=" };

protected JPanel jp = new JPanel();// 主面板

protected JPanel m = new JPanel();// 次面板

protected JPanel m1 = new JPanel();// 次次面板

protected JPanel m2 = new JPanel();// 次次面板

protected JPanel m3 = new JPanel();// 次次面板

protected JPanel m4 = new JPanel();// 次次面板

protected JPanel m5 = new JPanel();// 次次面板

protected JPanel m6 = new JPanel();// 次次面板

protected GridLayout glo = new GridLayout(6, 1, 3, 3);// 主网格布局

protected GridLayout glo1 = new GridLayout(1, 3, 3, 3);// 次网格布局

protected GridLayout glo2 = new GridLayout(1, 6, 3, 3);// 次网格布局

protected Conputer(String s) {

super(s);

a1.setMnemonic('E');

a1.add(a11);

a1.add(a12);

a2.setMnemonic('V');

a2.add(a21);

a2.add(a22);

a3.setMnemonic('H');

a3.add(a31);

a.add(a1);

a.add(a2);

a.add(a3);

this.setJMenuBar(a);// 菜单条完成

for (int i = 0; i 27; i++) {

jb[i] = new JButton(arr[i]);

}

jp.setLayout(glo);

jp.add(m1);

m1.add(jtf);

jtf.setEditable(false); jtf.setText("0."); jtf.setForeground(Color.BLUE); jtf.setBackground(Color.WHITE);

jtf.setHorizontalAlignment(JTextField.RIGHT);//文本显示在右边

jp.add(m2);

m2.setLayout(glo1);

m2.add(jb[0]);

m2.add(jb[1]);

m2.add(jb[2]);

jp.add(m3);

m3.setLayout(glo2);

m3.add(jb[3]);

m3.add(jb[4]);

m3.add(jb[5]);

m3.add(jb[6]);

m3.add(jb[7]);

m3.add(jb[8]);

jp.add(m4);

m4.setLayout(glo2);

m4.add(jb[9]);

m4.add(jb[10]);

m4.add(jb[11]);

m4.add(jb[12]);

m4.add(jb[13]);

m4.add(jb[14]);

jp.add(m5);

m5.setLayout(glo2);

m5.add(jb[15]);

m5.add(jb[16]);

m5.add(jb[17]);

m5.add(jb[18]);

m5.add(jb[19]);

m5.add(jb[20]);

jp.add(m6);

m6.setLayout(glo2);

m6.add(jb[21]);

m6.add(jb[22]);

m6.add(jb[23]);

m6.add(jb[24]);

m6.add(jb[25]);

m6.add(jb[26]);

m.add(jp);

con.add(m);

this.setResizable(false);// 不能用鼠标拉伸窗体

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗体

this.setSize(380, 275);

this.setVisible(true);

}

protected Conputer() {

this("计算器");

}

public static void main(String[] args) {

new Conputer("计算器");

}

}

java计算器代码

import java.awt.*;

import java.awt.event.*;

public class lvhaiya{

int tmp,sum,sum1,sum2=1,sum3=1;

String a,b,c,d;String s="";

Frame f=new Frame("计算器");

private String[]name={

"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","空格"

};

public Button[] button=new Button[name.length];

TextField t=new TextField("",30);

Panel p=new Panel();

Panel p1=new Panel();

Color color=new Color(100,170,90);

public lvhaiya(){

p1.setLayout(new GridLayout(5,5));

for(int i=0;iname.length;i++){

button[i]=new Button(name[i]);

p1.add(button[i]);

}

p.setLayout(new FlowLayout(FlowLayout.LEFT));

p.setBackground(color);

p.add(t);

f.add(p,BorderLayout.NORTH);

f.add(p1,BorderLayout.SOUTH);

f.pack();

f.setVisible(true);

f.addWindowListener( new WindowClose());

t.setText("0.");

for(int i=0;iname.length;i++){

button[i].addActionListener(new ButtonEventl());

}

}

public static void main(String[]args){

new lvhaiya();

}

class WindowClose extends WindowAdapter{

public void windouClosing(WindowEvent e){

System.exit(0);

}

}

class ButtonEventl implements ActionListener{

public void actionPerformed(ActionEvent e)throws ArithmeticException{

String command=e.getActionCommand();

if(command.equals("+")){

sum=Integer.parseInt(t.getText())+sum;

t.setText(String.valueOf(sum));

a=t.getText();

s="";

}

else if(command.equals("-")){

sum1=Integer.parseInt(t.getText());

b=t.getText();

s="";

}

else if(command.equals("*")){

sum2=Integer.parseInt(t.getText())*sum2;

t.setText(String.valueOf(sum2));

c=t.getText();

s="";

}

else if(command.equals("/")){

sum3=Integer.parseInt(t.getText());

d=t.getText();

s="";

}

else if(command.equals("=")){

if(a!=null){

t.setText(String.valueOf(sum+tmp));

}

if(b!=null){

t.setText(String.valueOf(sum1-tmp));

}

if(c!=null){

t.setText(String.valueOf(sum2*tmp));

}

try{

if(d!=null){

t.setText(String.valueOf(sum3/tmp));

}

}

catch(ArithmeticException a){

t.setText("除数不能为零");

}

}

else if(command.equals("空格")){

sum=0;

a=null;

b=null;

c=null;

d=null;

sum1=0;

sum2=1;

sum3=1;

tmp=0;

t.setText("0");

s="";

}

else{

s=s+command;

t.setText(s);

tmp=Integer.parseInt(s);

}

}

}

}

//按钮可以自己美化一下 希望可以帮到你

JAVA计算器相关代码求大神{详解}

我给你找找

package com.bj.calcultor;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Calcultor extends Frame implements ActionListener {

public static void main(String[] args) {//定义主方

new Calcultor();//创建匿名对象,并调用test()方法;

}

//定义按钮名称

String[] arr={"1","2","3","4","5","6","7","8","9","0","+","-","*","/","=","."};

JButton [] button=new JButton[arr.length];

JButton reset = new JButton("CE");

JTextField display = new JTextField(20);

//创建窗口,定义组件

//执行窗口事件:关闭窗口

private class WindowCloser extends WindowAdapter {

public void windowClosing(WindowEvent we) {

System.exit(0);

}

}

public Calcultor(){

super("计算器");//定义标题

//定义面板容器,并布局

JPanel jpanel=new JPanel(new GridLayout(4,4));

//添加按钮,并给按钮添加名称

for (int i = 0; i arr.length; i++) {

button[i]= new JButton(arr[i]);

jpanel.add(button[i]);

}

JPanel panel2 = new JPanel();

panel2.add("Northr", display);

display.setEnabled(false);

panel2.add("East", reset);

this.add("North", panel2);

this.add("Center", jpanel);

for (int i = 0; i arr.length; i++){

addWindowListener(new WindowCloser());

setVisible(true);

setSize(400,400);

pack();

button[i].addActionListener(this);

reset.addActionListener(this);

display.addActionListener(this);

}

}

@Override

public void actionPerformed(ActionEvent e) {//定义事件

// TODO Auto-generated method stub

Object target=e.getSource();

String lable=e.getActionCommand();

if(target==reset){

handleReset();

}else if("0123456789.".indexOf(lable)0){

handleNumber(lable);

}else{

hadleOperator(lable);

}

}

boolean isFirstDigit=true;

private void hadleOperator(String key) {

if(operator.equals("+")){

number += Double.valueOf(display.getText());

}else if (operator.equals("-")){

number -= Double.valueOf(display.getText());

}else if (operator.equals("*")){

number *= Double.valueOf(display.getText());

}else if (operator.equals("/")){

number /= Double.valueOf(display.getText());

}else if(operator.equals("=")){

number =Double.valueOf(display.getText());

}

display.setText(String.valueOf(number));

operator=key;

isFirstDigit=true;

}

private void handleNumber(String key) {

if(true){

display.setText(key);//把键值设置为文本框内容

}else if(key.equals(".") display.getText().indexOf(".")0){

display.setText(display.getText()+".");//把文本框内容设置:display.getText()+"."

}else if(!key.equals(".")){

display.setText(display.getText() + key);//把文本框内容设置:display.getText()+key

isFirstDigit=false;

}

}

private void handleReset() {

display.setText("0");

isFirstDigit=true;

operator="=";

}

String operator="=";

Double number=0.0;

}

如何java设计计算器并实现功能

java设计计算器,主要是使用java swing,对每个按钮进行监听,实现相关功能,如下代码:

package com.qiu.lin.he;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

public class Jisuanqi extends JFrame implements ActionListener {

/**

 * 

 */

private static final long serialVersionUID = 1L;

Result result = new Result(); // 定义text的面板

Number_Key number_key = new Number_Key(); // 定义按钮面板

// 当点击按钮+、-、*、/时,com = true

boolean com = false;

// 当i=0时说明是我们第一次输入,字符串text不会累加

int i = 0;

// 存放text的内容

String text = "";

// 存放点击按钮+、-、*、/之前的数值

double defbutton = 0;

// +、-、*、/的代号分别为1,2,3,4

int symbol = 0;

// 构造函数

Jisuanqi() {

super("WangJiao"); // 设定标题

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定关闭窗体时退出程序

JPanel pane = new JPanel(); // 定义主面板

pane.setLayout(new BorderLayout());

setBounds(380, 220, 30, 80); // 前两个参数是在屏幕上显示的坐标,后两个是大小

// 替换图标

ImageIcon icon = new ImageIcon("F:1.GIF");

// Jisuanqi.class.getResource("APPLE.GIF")

// );

setIconImage(icon.getImage());

pane.add(result, BorderLayout.NORTH);

pane.add(number_key, BorderLayout.CENTER);

pane.add(number_key.equal, BorderLayout.SOUTH);

number_key.one.addActionListener(this); // 对1按钮添加监听事件

number_key.two.addActionListener(this); // 对2按钮添加监听事件

number_key.three.addActionListener(this); // 对3按钮添加监听事件

number_key.four.addActionListener(this); // 对4按钮添加监听事件

number_key.five.addActionListener(this); // 对5按钮添加监听事件

number_key.six.addActionListener(this); // 对6按钮添加监听事件

number_key.seven.addActionListener(this); // 对7按钮添加监听事件

number_key.eight.addActionListener(this); // 对8按钮添加监听事件

number_key.nine.addActionListener(this); // 对9按钮添加监听事件

number_key.zero.addActionListener(this); // 对0按钮添加监听事件

number_key.ce.addActionListener(this); // 对置零按钮添加监听事件

number_key.plus.addActionListener(this); // 对+按钮添加监听事件

number_key.equal.addActionListener(this); // 对=按钮添加监听事件

number_key.sub.addActionListener(this); // 对-按钮添加监听事件

number_key.mul.addActionListener(this); // 对*按钮添加监听事件

number_key.div.addActionListener(this); // 对/按钮添加监听事件

number_key.point.addActionListener(this); // 对.按钮添加监听事件

setContentPane(pane);

pack(); // 初始化窗体大小为正好盛放所有按钮

}

// 各个按钮触发的事件

public void actionPerformed(ActionEvent e) {

/*

 * 如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么com=true 如果没有com=

 * false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;

 */

if (e.getSource() == number_key.one) {

if (com || i == 0) {

result.text.setText("1");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "1");

}

} else if (e.getSource() == number_key.two) {

if (com || i == 0) {

result.text.setText("2");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "2");

}

} else if (e.getSource() == number_key.three) {

if (com || i == 0) {

result.text.setText("3");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "3");

}

} else if (e.getSource() == number_key.four) {

if (com || i == 0) {

result.text.setText("4");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "4");

}

} else if (e.getSource() == number_key.five) {

if (com || i == 0) {

result.text.setText("5");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "5");

}

} else if (e.getSource() == number_key.six) {

if (com || i == 0) {

result.text.setText("6");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "6");

}

} else if (e.getSource() == number_key.seven) {

if (com || i == 0) {

result.text.setText("7");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "7");

}

} else if (e.getSource() == number_key.eight) {

if (com || i == 0) {

result.text.setText("8");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "8");

}

} else if (e.getSource() == number_key.nine) {

if (com || i == 0) {

result.text.setText("9");

com = false;

i = 1;

} else {

text = result.text.getText();

result.text.setText(text + "9");

}

}

/*

 * 对于0这个按钮有一定的说法,在我的程序里不会出现如00000这样的情况,我加了判断条件就是

 * 如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添 加0;否则保持原来的数值不变

 */

else if (e.getSource() == number_key.zero) { // result.text.getText()是得到text里内容的意思

if (com || i == 0) {

result.text.setText("0");

com = false;

i = 1;

} else {

text = result.text.getText();

if (Float.parseFloat(text)  0 || Float.parseFloat(text)  0) { // Float.parseFloat(text)就是类型转换了,下面都是一样

result.text.setText(text + "0");

} else {

if (text.trim().indexOf(".") == -1) {

result.text.setText(text);

} else {

result.text.setText(text + "0");

}

}

}

} else if (e.getSource() == number_key.ce) {

result.text.setText("0");

i = 0;

com = true;

// text = "";

defbutton = 0;

}

/*

 * 本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加, 不存在就添加.

 */

else if (e.getSource() == number_key.point) {

if (com || i == 0) {

result.text.setText("0.");

com = false;

i = 1;

} else {

text = result.text.getText();

if (text.trim().indexOf(".") == -1) {

result.text.setText(text + ".");

} else {

result.text.setText(text);

}

}

} // 获得点击+之前的数值

else if (e.getSource() == number_key.plus) {

com = true;

i = 0;

defbutton = Double.parseDouble(result.text.getText());

symbol = 1;

} // 获得点击-之前的数值

else if (e.getSource() == number_key.sub) {

com = true;

i = 0;

defbutton = Double.parseDouble(result.text.getText());

symbol = 2;

} // 获得点击*之前的数值

else if (e.getSource() == number_key.mul) {

com = true;

i = 0;

defbutton = Double.parseDouble(result.text.getText());

System.out.println(defbutton);

symbol = 3;

} // 获得点击/之前的数值

else if (e.getSource() == number_key.div) {

com = true;

i = 0;

defbutton = Double.parseDouble(result.text.getText());

symbol = 4;

} else if (e.getSource() == number_key.equal) {

switch (symbol) {

case 1: { // 计算加法

double ad = defbutton

+ Double.parseDouble(result.text.getText());

result.text.setText(ad + "");

i = 0;

text = "";

break;

}

case 2: { // 计算减法

double ad = defbutton

- Double.parseDouble(result.text.getText());

result.text.setText(String.valueOf(ad));

i = 0;

text = "";

break;

}

case 3: { // 计算乘法

double ad = defbutton

* Double.parseDouble(result.text.getText());

result.text.setText(ad + "");

i = 0;

text = "";

break;

}

case 4: { // 计算除法

double ad = defbutton

/ Double.parseDouble(result.text.getText());

result.text.setText(ad + "");

i = 0;

text = "";

break;

}

}

System.out.println(com);

}

System.out.println(result.text.getText());

}

@SuppressWarnings("deprecation")

public static void main(String[] args) {

Jisuanqi loveyou = new Jisuanqi();

loveyou.show();

}

}

// 计算器数字按钮定义面板

class Number_Key extends JPanel {

/**

 * 

 */

private static final long serialVersionUID = 1L;

JButton zero = new JButton("0"); // 数字键0

JButton one = new JButton("1"); // 数字键1

JButton two = new JButton("2"); // 数字键2

JButton three = new JButton("3"); // 数字键3

JButton four = new JButton("4"); // 数字键4

JButton five = new JButton("5"); // 数字键5

JButton six = new JButton("6"); // 数字键6

JButton seven = new JButton("7"); // 数字键7

JButton eight = new JButton("8"); // 数字键8

JButton nine = new JButton("9"); // 数字键9

JButton plus = new JButton("+");

JButton sub = new JButton("-");

JButton mul = new JButton("*");

JButton div = new JButton("/");

JButton equal = new JButton("=");

JButton ce = new JButton("清零"); // 置零键

JButton point = new JButton(".");

Number_Key() {

setLayout(new GridLayout(4, 4, 1, 1)); // 定义布局管理器为网格布局

setBackground(Color.blue); // 设置背景颜色

// 添加按钮

add(one);

add(two);

add(three);

add(four);

add(five);

add(six);

add(seven);

add(eight);

add(nine);

add(zero);

add(plus);

add(sub);

add(mul);

add(div);

add(point);

add(equal);

add(ce);

}

}

// 计算器显示结果的窗体

class Result extends JPanel {

/**

 * 

 */

private static final long serialVersionUID = 1L;

// text先是输入和结果

JTextField text = new JTextField("0");

@SuppressWarnings("deprecation")

Result() { // 讲输入的数字或得到的结果在text的右边显示

text.setHorizontalAlignment(SwingConstants.RIGHT);

text.enable(false); // 文本框不能编辑

setLayout(new BorderLayout()); // 设定布局管理器边框布局

add(text, BorderLayout.CENTER); // text放置在窗体的中间

}

}

运行结果如下:


网页名称:java计算器布局代码 java计算器的布局
当前链接:http://scyanting.com/article/dossecg.html