求 用java编辑的计算器代码(易简)
- 提问者网友:情歌越听越心酸
- 2021-05-08 02:48
- 五星知识达人网友:你哪知我潦倒为你
- 2021-05-08 03:46
呵呵 我写了一个比较简单的计算器,不知道能不能满足楼主的要求
import java.awt.*;
import java.awt.event.*;
class MyWindow extends Frame implements ActionListener
{ Button b[]=new Button[10];//new 0至9的按钮
Button button01,button02,button03,button04,button05,button06,button07,button08;
TextField text1;
int i,z;//Z表示单击了那一个运算符.0表示"加",1表示"减",2表示"乘",3表示"除"
double x,y;//x,y都是文本框中要输入计算的数字
MyWindow(String s)
{
super(s);
button01=new Button("加");
button02=new Button("减");
button03=new Button("乘");
button04=new Button("除");
button05=new Button("等于");
button06=new Button("归零");
button07=new Button("开根号");
button08=new Button(".");
text1=new TextField(12);
setLayout(new FlowLayout());
add(text1);
for(i=0;i<10;i++)
{b[i]=new Button(Integer.toString(i));
b[i].addActionListener(this);
add(b[i]);
}//生成0至9的按钮
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
add(button01);
add(button02);
add(button03);
add(button04);
add(button05);
add(button06);
add(button07);
add(button08);
button01.addActionListener(this);
button02.addActionListener(this);
button03.addActionListener(this);
button04.addActionListener(this);
button05.addActionListener(this);
button06.addActionListener(this);
button07.addActionListener(this);
button08.addActionListener(this);
setBounds(100,100,150,300);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{ Object temp = e.getSource();
for(i=0;i<=9;i++)
{
if(temp==b[i])
{
text1.setText(text1.getText()+b[i].getLabel());
}
}
if(temp==button01)
{ x=Double.parseDouble(text1.getText());
text1.setText(null);
z=0;
} //加法运算
else if(temp==button02)
{ x=Double.parseDouble(text1.getText());
text1.setText(null);
z=1;
}//减法运算
else if(temp==button03)
{
x=Double.parseDouble(text1.getText());
text1.setText(null);
z=2;
}//乘法运算
else if(temp==button04)
{ x=Double.parseDouble(text1.getText());
text1.setText(null);
z=3;
}//除法运算
else if(temp==button07)
{ x=Double.parseDouble(text1.getText());
text1.setText(Double.toString(Math.sqrt(x)));
}//开根号运算
else if(temp==button08)
{
text1.setText(text1.getText()+button08.getLabel());
}//小数运算
else if(temp==button05)
{ y=Double.parseDouble(text1.getText());
switch(z)
{
case 0 : text1.setText(Double.toString((x+y)));break;
case 1 : text1.setText(Double.toString((x-y)));break;
case 2 : text1.setText(Double.toString((x*y)));break;
case 3 : text1.setText(Double.toString((x/y)));break;
}
}
else if(temp==button06)
{text1.setText(null);
} //归零,为下次计算做准备
}
}
public class JiShuangQi03
{
public static void main(String args[])
{new MyWindow("计算器");
}
}
- 1楼网友:北城痞子
- 2021-05-08 05:39
问问团队邯涵学友
随时帮助您
我下面这个更简单
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class Calculator extends JApplet { public static void main(String[] args) { CalculatorFrame frame = new CalculatorFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
public void init() { CalculatorPanel panel = new CalculatorPanel(); add(panel); } }
class CalculatorFrame extends JFrame { public CalculatorFrame() { setTitle("Calculator"); CalculatorPanel panel = new CalculatorPanel(); add(panel); pack(); } }
class CalculatorPanel extends JPanel { private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
public CalculatorPanel() { setLayout(new BorderLayout());
result = 0; lastCommand = "="; start = true;
// add the display
display = new JButton("0"); display.setEnabled(false); add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction(); ActionListener command = new CommandAction();
// add the buttons in a 4 x 4 grid
panel = new JPanel(); panel.setLayout(new GridLayout(4, 4));
addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", command);
addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", command);
addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", command);
addButton("0", insert); addButton(".", insert); addButton("=", command); addButton("+", command);
add(panel, BorderLayout.CENTER); }
private void addButton(String label, ActionListener listener) { JButton button = new JButton(label); button.addActionListener(listener); panel.add(button); }
private class InsertAction implements ActionListener { public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); if (start) { display.setText(""); start = false; } display.setText(display.getText() + input); } }
private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand();
if (start) { if (command.equals("-")) { display.setText(command); start = false; } else lastCommand = command; } else { display.setText("" + calculate(Double.parseDouble(display.getText()),lastCommand)); lastCommand = command; start = true; } } }
public double calculate(double x,String action) { if (action.equals("+")) result += x; else if (action.equals("-")) result -= x; else if (action.equals("*")) result *= x; else if (action.equals("/")) result /= x; else if (action.equals("=")) result = x; return result; }
}
- 2楼网友:神也偏爱
- 2021-05-08 04:04
import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; 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 CalculatorB implements ActionListener{ private JFrame jf ; private JButton clearButton; private JButton [] allButton; private JTextField field; private String[] str = new String[3]; public CalculatorB() { jf=new JFrame("计算机"); clearButton = new JButton("C"); allButton = new JButton[16] ; field = new JTextField(15); String str = "123+456-789*0.=/"; for (int i = 0; i < str.length(); i++) { allButton[i]=new JButton(str.substring(i,i+1)); } } public void init (){ jf.setLayout(new BorderLayout()); JPanel northPanel = new JPanel(); JPanel centerPanel = new JPanel(); northPanel.setLayout(new FlowLayout()); centerPanel.setLayout(new GridLayout(4,4)); northPanel.add(field); northPanel.add(clearButton); for (int i = 0; i < allButton.length; i++) { centerPanel.add(allButton[i]); } jf.add(northPanel,BorderLayout.NORTH); jf.add(centerPanel,BorderLayout.CENTER); } public void SetFontAndColor(){ Font f = new Font("宋体",Font.BOLD,15); field.setFont(f); for (int i = 0; i < allButton.length; i++) { allButton[i].setFont(f); allButton[i].setBackground(Color.BLACK); allButton[i].setForeground(Color.WHITE); } } public void showMe(){ init(); SetFontAndColor(); EventHandler(); jf.pack(); jf.setResizable(false); jf.setLocation(300,300); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { String comm = e.getActionCommand().trim(); if("0123456789.".indexOf(comm)!=-1){ if("+-*/".contains(field.getText())){ field.setText(""); } field.setText(field.getText()+ comm); str[2] = field.getText(); }else if("+-*/".indexOf(comm)!=-1){ field.setText(comm); str[0] =str[2]; str[1] = comm; }else{ double sum = 0; if(str[1].equals("*")){ sum = Double.valueOf(str[0])* Double.valueOf(str[2]); }else if(str[1].equals("/")){ sum = Double.valueOf(str[0])/ Double.valueOf(str[2]); }else if(str[1].equals("+")){ sum = Double.valueOf(str[0])+ Double.valueOf(str[2]); }else{ sum = Double.valueOf(str[0])- Double.valueOf(str[2]); } field.setText(String.valueOf(sum)); str[2] = field.getText(); } } public void EventHandler(){ clearButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { field.setText(""); } }); for (int i = 0; i < allButton.length; i++) { allButton[i].addActionListener(this); } } public static void main(String[] args) { new CalculatorB().showMe(); }
}