java计算器代码带菜单 java计算器程序代码

求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; i27; 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计算器问题的菜单显示怎么做啊跟后面java计算器代码带菜单的计算器一样 前面用个选择控制下就行java计算器代码带菜单了~
选择计算器
1:老款计算器2:新款计算器
如果输入的1 就进入加减乘除的计算里 如果输入2就进带开根的计算里~
码字不易 求采纳~
编写java application程序实现一个简易计算器Java计算器 源代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**********************Java计算器 主类*********************/
public class SunnyCalculator implements ActionListener {
JFrame f;
JMenu mEdit;
JMenu mView;
JMenu mHelp;
JMenuItem mCopy;
JMenuItem mPaste;
JTextField tResult;
JButton bNumber;
JButton bOperator;
JButton bOther;
JButton bM;
boolean isDouble=false;//是否为实数
int opFlag=-1;
static double t1=0,t2=0,t3=0,result=0;
static int opflag1=-1,opflag2=-1,flag=0,resflag=1;
int preOp,currentOp=0;//标准位
double op1=0,op2=0;//操作数
double n3;
StringBuffer buf=new StringBuffer(20);
StringBuffer copyBoard=new StringBuffer(20);//剪贴板
StringBuffer memory=new StringBuffer(20);//M系列
StringBuffer str=new StringBuffer();
//Java计算器 构造器
public SunnyCalculator()
{
f = new JFrame("Sunny计算器_杨梅树的盔甲");
Container contentPane = f.getContentPane();
/**************************Java计算器 菜单的创建*****************************/
JMenuBar mBar = new JMenuBar();
mBar.setOpaque(true);
mEdit = new JMenu("编辑(E)");
mEdit.setMnemonic(KeyEvent.VK_E);
mCopy = new JMenuItem("复制(C)");
mEdit.add(mCopy);
mPaste = new JMenuItem("粘贴(P)");
mEdit.add(mPaste);
mView = new JMenu("查看(V)");
mView.setMnemonic(KeyEvent.VK_V);
mView.add(new JMenuItem("标准型"));
mView.add(new JMenuItem("科学型"));
mView.addSeparator();
mView.add(new JMenuItem("查看分组"));
mHelp = new JMenu("帮助(H)");
mHelp.setMnemonic(KeyEvent.VK_H);
mHelp.add(new JMenuItem("帮助主题"));
mHelp.addSeparator();
mHelp.add(new JMenuItem("关于计算器"));
mBar.add(mEdit);
mBar.add(mView);
mBar.add(mHelp);
f.setJMenuBar(mBar);
contentPane.setLayout(new BorderLayout());
JPanel pTop = new JPanel();
tResult = new JTextField("0.",26);
tResult.setHorizontalAlignment(JTextField.RIGHT);
tResult.setEditable(false);
pTop.add(tResult);
contentPane.add(pTop,BorderLayout.NORTH);
JPanel pBottom = new JPanel();
pBottom.setLayout(new BorderLayout());
JPanel pLeft = new JPanel();
pLeft.setLayout(new GridLayout(5,1,3,3));
bM = new JButton(" ");
bM.setEnabled(false);
【java计算器代码带菜单 java计算器程序代码】pLeft.add(bM);
/*************************Java计算器 功能键定义***************************/
bOther = new JButton("MC");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
bOther = new JButton("MR");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
bOther = new JButton("MS");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
bOther = new JButton("M ");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
pBottom.add(pLeft,BorderLayout.WEST);
JPanel pRight = new JPanel();
pRight.setLayout(new BorderLayout());
JPanel pUp = new JPanel();
pUp.setLayout(new GridLayout(1,3,3,0));
bOther = new JButton("BackSpace");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,0,3,5));
pUp.add(bOther);
bOther = new JButton("CE");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
pUp.add(bOther);
bOther = new JButton("C");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
pUp.add(bOther);
/***************************Java计算器 数字键盘区定义**************************/
JPanel pDown = new JPanel();
pDown.setLayout(new GridLayout(4,5,3,2));
bNumber = new JButton("7");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("8");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("9");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("/");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,0,3,0));
pDown.add(bOperator);
bOperator = new JButton("sqrt");
bOperator.addActionListener(this);
bOperator.setForeground(Color.red);
bOperator.setMargin(new Insets(3,0,3,0));
pDown.add(bOperator);
bNumber = new JButton("4");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
bNumber.setHorizontalTextPosition(JButton.LEFT);
pDown.add(bNumber);
bNumber = new JButton("5");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("6");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("*");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("%");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bNumber = new JButton("1");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("2");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("3");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("-");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("1/x");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
pDown.add(bOperator);
bNumber = new JButton("0");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton(" /-");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton(".");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton(" ");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("=");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
pRight.add(pUp,BorderLayout.NORTH);
pRight.add(pDown,BorderLayout.SOUTH);
pBottom.add(pRight,BorderLayout.EAST);
contentPane.add(pBottom,BorderLayout.SOUTH);
f.setSize(new Dimension(320,256));
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
/************************Java计算器 计算方法区***************************/
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("复制(C)"))
{
String temp = tResult.getText().trim();
copyBoard.replace(0, copyBoard.length(), temp);
mPaste.setEnabled(true);
}
else if(s.equals("粘贴(p)"))
{
tResult.setText(copyBoard.toString());
}
else if(s.equals("CE"))
{
//如果是CE则清除文本框
tResult.setText("0.");
}
else if(s.equals("BackSpace"))
{
if(!tResult.getText().trim().equals("0."))
{
//如果文本框中有内容
if(str.length()!=1str.length()!=0)
{
tResult.setText(str.delete(str.length()-1,str.length()).toString());
}
else
{
tResult.setText("0.");
str.setLength(0);
}
}
op2 = Double.parseDouble(tResult.getText().trim());
}
else if(s.equals("C"))
{
//如果是C删除当前计算
tResult.setText("0.");
op1 = op2 = 0;
str.replace(0, str.length(), " ");
preOp = currentOp = 0;
}
else if(s.equals("MC"))
{
//如果是MC则清除缓冲区
String temp = "";
memory.replace(0, memory.length(), temp);
bM.setText(" ");
}
else if(s.equals("MR"))
{
//如果按键为MR则恢复缓冲区的数到文本框
tResult.setText(memory.toString());
}
else if(s.equals("MS"))
{
//如果按键为MS则将文本框的数存入缓冲区
String s1 = tResult.getText().trim();
memory.replace(0, memory.length(), s1);
bM.setText("M");
}
else if(s.equals("M "))
{
//如果按键为MS则将文本框值与缓冲区的数相加但不显示结果
String temp1 = tResult.getText().trim();
double dtemp = Double.parseDouble(temp1);
String temp2 = memory.toString();
dtemp= Double.parseDouble(temp2);
temp1 = String.valueOf(dtemp);
memory.replace(0, memory.length(), temp1);
}
else if(s.equals("1/x"))
{
//如果按键为1/x则将文本框中的数据为它的倒数
String temp = tResult.getText().trim();
double dtemp = Double.parseDouble(temp);
tResult.setText("" 1/dtemp);
}
else if(s.equals("sqrt"))
{
//如果按键为sqrt则将文本框中的内容求平方根
String temp = tResult.getText().trim();
double dtemp = Double.parseDouble(temp);
tResult.setText("" Math.sqrt(dtemp));
}
else if(s.equals(" "))
{
str.setLength(0);
if(currentOp==0)
{
preOp = currentOp = 1;
op2 = 0;
tResult.setText("" op1);
}
else
{
currentOp = preOp;
preOp = 1;
switch(currentOp){
case 1:
op1= op2;
tResult.setText("" op1);
break;
case 2:
op1 -= op2;
tResult.setText("" op1);
break;
case 3:
op1 *= op2;
tResult.setText("" op1);
break;
case 4:
op1 /= op2;
tResult.setText("" op1);
break;
}
}
}
else if(s.equals("-")){
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=2;//op1=op2;op2=0;
tResult.setText("" op1);
}
else
{
currentOp =preOp;
preOp =2;
switch(currentOp){
case 1:op1=op1 op2;tResult.setText("" op1);break;
case 2:op1=op1-op2;tResult.setText("" op1);break;
case 3:op1=op1*op2;tResult.setText("" op1);break;
case 4:op1=op1/op2;tResult.setText("" op1);break;
}
}
}
else if(s.equals("*"))//*
{
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=3;//op1=op2;op2=1;
tResult.setText("" op1);//op1=op2;
}
else
{
currentOp =preOp;
preOp =3;
switch(currentOp){
case 1:op1=op1 op2;tResult.setText("" op1);break;
case 2:op1=op1-op2;tResult.setText("" op1);break;
case 3:op1=op1*op2;tResult.setText("" op1);break;
case 4:op1=op1/op2;tResult.setText("" op1);break;
}
}
}
else if(s.equals("/"))// /
{
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=4;//op2=1;
tResult.setText("" op1);//op1=op2;
}
else
{
currentOp =preOp;
preOp =4;
switch(currentOp){
case 1:op1=op1 op2;tResult.setText("" op1);break;
case 2:op1=op1-op2;tResult.setText("" op1);break;
case 3:op1=op1*op2;tResult.setText("" op1);break;
case 4:op1=op1/op2;tResult.setText("" op1);break;
}
}
}
else if(s.equals("="))// =
{
if(currentOp==0)
{
str.setLength(0);
tResult.setText("" op2);
}
else
{
str.setLength(0);
currentOp =preOp;
switch(currentOp){
case 1:op1=op1 op2;tResult.setText("" op1);break;
case 2:op1=op1-op2;tResult.setText("" op1);break;
case 3:op1=op1*op2;tResult.setText("qu
JAVA编程跪求一简易计算器带菜单给java计算器代码带菜单你现成java计算器代码带菜单的吧java计算器代码带菜单,不知道是不是你要的菜单效果:
import java.awt.*;
import java.awt.event.*;
public class JSQ extends WindowAdapter {
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
TextField txt;
private Button[] b = new Button[19];
private String ss[] = { "1", "2", "3", " ", "-", "4", "5", "6", "*", "/",
"7", "8", "9", "0", "=", "%", "清空", "sqrt", "关闭", };
static double a;
static String s = "", str;// 定义变量 创建对像
public static void main(String args[]) {
(new JSQ()).frame();
}
public void frame() {
Frame fm = new Frame("计算器");
for (int i = 0; i = 18; i) {
b[i] = new Button(ss[i]);
}
for (int i = 0; i = 17; i) {
p2.add(b[i]);
} // 创建按钮 并添加到P2
b[18].setBackground(Color.yellow);
txt = new TextField(16);
txt.setEditable(false);
for (int i = 0; i = 18; i) {
b[i].addActionListener(new buttonlistener());// 添加监听器
}
b[18].addActionListener(new close());
fm.addWindowListener(this);
fm.setBackground(Color.red);
p1.setLayout(new BorderLayout());
p1.add(txt, "North");
p2.setLayout(new GridLayout(4, 4));
p3.setLayout(new BorderLayout());
p3.add(b[18]);
fm.add(p1, "North");
fm.add(p2, "Center");
fm.add(p3, "South");
fm.pack();
fm.setVisible(true);// 都是些窗中设置 添加相关组件和监听器
}
public void windowClosing(WindowEvent e) {
System.exit(0);// 退出系统
}
class buttonlistener implements ActionListener {// 编写监听器事件 通过按键得出给果
public void actionPerformed(ActionEvent e) {
Button btn = (Button) e.getSource();
if (btn.getLabel().equals("=")) {
if (s.equals("sqrt")) {
str = String.valueOf(a);
txt.setText(str);
s = "";
} else {
jisuan();
str = String.valueOf(a);
txt.setText(str);
s = "";
}
} else if (btn.getLabel().equals(" ")) {
jisuan();
txt.setText("");
s = " ";
} else if (btn.getLabel().equals("-")) {
jisuan();
txt.setText("");
s = "-";
} else if (btn.getLabel().equals("/")) {
jisuan();
txt.setText("");
s = "/";
} else if (btn.getLabel().equals("*")) {
jisuan();
txt.setText("");
s = "*";
} else if (btn.getLabel().equals("%")) {
jisuan();
txt.setText("");
s = "%";
} else if (btn.getLabel().equals("sqrt")) {
s = "sqrt";
jisuan();
txt.setText("");
} else {
txt.setText(txt.getText()btn.getLabel());
if (btn.getLabel() == "清空")
txt.setText("");
}
}
public void jisuan() {// 编写具体计算方法
if (s.equals(" "))
a= Double.parseDouble(txt.getText());
else if (s.equals("-"))
a -= Double.parseDouble(txt.getText());
else if (s.equals("*"))
a *= Double.parseDouble(txt.getText());
else if (s.equals("/"))
a /= Double.parseDouble(txt.getText());
else if (s.equals("%"))
a %= Double.parseDouble(txt.getText());
else if (s.equals("sqrt"))
a = Math.sqrt(Double.parseDouble(txt.getText()));
else
a = Double.parseDouble(txt.getText());
}
}
}
class close implements ActionListener {// 退出
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
java计算器菜单栏上的“关于计算器”代码怎么写?。?/h2>“关于计算器”这个非常简单呀,首先在菜单栏上添加“关于计算器”这项MenuItem,然后添加监听,功能是点击后菜单之后new 一下这个“关于计算器”java计算器代码带菜单的新窗口不就可以了么?新窗口放一个单独java计算器代码带菜单的类写,至于那里面想写什么就写什么,随意了 , 简单介绍 , 作者什么的,既然都能写出计算器的界面了,这个应该不是什么问题 。
java计算器代码这段代码对java计算器代码带菜单你有帮助
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
/**
* java计算器代码带菜单我java计算器代码带菜单的计算器 。MyCalculator 继承于 JFramejava计算器代码带菜单,是计算器的界面
*/
public class MyCalculator extends JFrame {
private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);
private JTextField textbox = new JTextField("0");
private CalculatorCore core = new CalculatorCore();
private ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
String label = b.getText();
String result = core.process(label);
textbox.setText(result);
}
};
public MyCalculator(String title) throws HeadlessException {
super(title);// 调用父类构造方法
setupFrame();// 调整窗体属性
setupControls();// 创建控件
}
private void setupControls() {
setupDisplayPanel();// 创建文本面板
setupButtonsPanel();// 创建按钮面板
}
// 创建按钮面板并添加按钮
private void setupButtonsPanel() {
JPanel panel = new JPanel();
panel.setBorder(border);
panel.setLayout(new GridLayout(4, 5, 3, 3));
createButtons(panel, new String[]{
"7", "8", "9", " ", "C",
"4", "5", "6", "-", "CE",
"1", "2", "3", "*", "",// 空字符串表示这个位置没有按钮
"0", ".", "=", "/", ""
});
this.add(panel, BorderLayout.CENTER);
}
/**
* 在指定的面板上创建按钮
*
* @param panel要创建按钮的面板
* @param labels 按钮文字
*/
private void createButtons(JPanel panel, String[] labels) {
for (String label : labels) {
// 如果 label 为空java计算器代码带菜单 , 则表示创建一个空面板 。否则创建一个按钮 。
if (label.equals("")) {
panel.add(new JPanel());
} else {
JButton b = new JButton(label);
b.addActionListener(listener);// 为按钮添加侦听器
panel.add(b);
}
}
}
// 设置显示面板,用一个文本框来作为计算器的显示部分 。
private void setupDisplayPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(border);
setupTextbox();
panel.add(textbox, BorderLayout.CENTER);
this.add(panel, BorderLayout.NORTH);
}
// 调整文本框
private void setupTextbox() {
textbox.setHorizontalAlignment(JTextField.RIGHT);// 文本右对齐
textbox.setEditable(false);// 文本框只读
textbox.setBackground(Color.white);// 文本框背景色为白色
}
// 调整窗体
private void setupFrame() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);// 当窗体关闭时程序结束
this.setLocation(100, 50);// 设置窗体显示在桌面上的位置
this.setSize(300, 200);// 设置窗体大小
this.setResizable(false);// 窗体大小固定
}
// 程序入口
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
MyCalculator frame = new MyCalculator("我的计算器");
frame.setVisible(true);// 在桌面上显示窗体
}
}
/**
* 计算器核心逻辑 。这个逻辑只能处理 1~2 个数的运算 。
*/
class CalculatorCore {
private String displayText = "0";// 要显示的文本
boolean reset = true;
private BigDecimal number1, number2;
private String operator;
private HashMapString, Operator operators = new HashMapString, Operator();
private HashMapString, Processor processors = new HashMapString, Processor();
CalculatorCore() {
setupOperators();
setupProcessors();
}
// 为每种命令添加处理方式
private void setupProcessors() {
processors.put("[0-9]", new Processor() {
public void calculate(String command) {
numberClicked(command);
}
});
processors.put("\\.", new Processor() {
public void calculate(String command) {
dotClicked();
}
});
processors.put("=", new Processor() {
public void calculate(String command) {
equalsClicked();
}
});
processors.put("[ \\-*/]", new Processor() {
public void calculate(String command) {
operatorClicked(command);
}
});
processors.put("C", new Processor() {
public void calculate(String command) {
clearClicked();
}
});
processors.put("CE", new Processor() {
public void calculate(String command) {
clearErrorClicked();
}
});
}
// 为每种 operator 添加处理方式
private void setupOperators() {
operators.put(" ", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.add(number2);
}
});
operators.put("-", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.subtract(number2);
}
});
operators.put("*", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.multiply(number2);
}
});
operators.put("/", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.divide(number2, 30, RoundingMode.HALF_UP);
}
});
}
// 根据命令处理 。这里的命令实际上就是按钮文本 。
public String process(String command) {
for (String pattern : processors.keySet()) {
if (command.matches(pattern)) {
processors.get(pattern).calculate(command);
break;
}
}
return displayText;
}
// 当按下 CE 时
private void clearErrorClicked() {
if (operator == null) {
number1 = null;
} else {
number2 = null;
}
displayText = "0";
reset = true;
}
// 当按下 C 时,将计算器置为初始状态 。
private void clearClicked() {
number1 = null;
number2 = null;
operator = null;
displayText = "0";
reset = true;
}
// 当按下 = 时
private void equalsClicked() {
calculateResult();
number1 = null;
number2 = null;
operator = null;
reset = true;
}
// 计算结果
private void calculateResult() {
number2 = new BigDecimal(displayText);
Operator oper = operators.get(operator);
if (oper != null) {
BigDecimal result = oper.process(number1, number2);
displayText = result.toString();
}
}
// 当按下-*/ 时(这里也可以扩展成其他中间操作符)
private void operatorClicked(String command) {
if (operator != null) {
calculateResult();
}
number1 = new BigDecimal(displayText);
operator = command;
reset = true;
}
// 当按下 . 时
private void dotClicked() {
if (displayText.indexOf(".") == -1) {
displayText= ".";
} else if (reset) {
displayText = "0.";
}
reset = false;
}
// 当按下 0-9 时
private void numberClicked(String command) {
if (reset) {
displayText = command;
} else {
displayText= command;
}
reset = false;
}
// 运算符处理接口
interface Operator {
BigDecimal process(BigDecimal number1, BigDecimal number2);
}
// 按钮处理接口
interface Processor {
void calculate(String command);
}
}
java计算器代码带菜单的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java计算器程序代码、java计算器代码带菜单的信息别忘了在本站进行查找喔 。

    推荐阅读