GUI程序设计(java)
使用JavaAWT或JavaSwing实现如下实验题目:模仿Windows计算器,用Java实现图形界面的计算器。...
使用Java AWT或Java Swing实现如下实验题目:
模仿Windows计算器,用Java实现图形界面的计算器。 展开
模仿Windows计算器,用Java实现图形界面的计算器。 展开
3个回答
展开全部
=====================第一个类=============================
/**
*
* 实现了系统计算器连续按"="和按"+","-","*","/"进行累记运算的模式
*
* */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalculatorFrame extends JFrame {
private JTextField txtDis = new JTextField("0", 16); //显示文本框
private JButton[] btnGrp = new JButton[17]; //17个按钮
private JPanel jpnMain = new JPanel(); //主面板
private JPanel jpnNorth = new JPanel(); //上面板
private JPanel jpnSouth = new JPanel(); //下面板
private double numSaved; //保存的前一个数
private String operator = ""; //保存的运算符号
private String lastPress = ""; //上一次按的按钮("数字","运算符" 或 "等号")
/*构造函数*/
public CalculatorFrame() {
//窗口设置
setTitle("计算器");
setSize(210, 250);
buttonInit(); //按钮初始化
//主面板设置
add(jpnMain);
jpnMain.setLayout(null);
jpnMain.add(jpnNorth);
jpnMain.add(jpnSouth);
jpnMain.add(btnGrp[15]);
btnGrp[15].setBounds(8, 180, 188, 30); //添加“=”按钮
//上面板设置
jpnNorth.setBounds(8, 10, 190, 30);
jpnNorth.add(txtDis);
txtDis.setHorizontalAlignment(JTextField.RIGHT); //设置文本右对齐
txtDis.setEditable(false);
//下面板设置
jpnSouth.setBounds(8, 60, 190, 120);
jpnSouth.setLayout(new GridLayout(4, 4));
addSouthJpn(); //添加17个按钮
//添加监听器
addListener();
}
/*17个按钮显示值初始化*/
public void buttonInit() {
for (int i = 0; i < 10; i++) {
btnGrp[i] = new JButton("" + i);
}
btnGrp[10] = new JButton(".");
btnGrp[11] = new JButton("+");
btnGrp[12] = new JButton("-");
btnGrp[13] = new JButton("*");
btnGrp[14] = new JButton("/");
btnGrp[15] = new JButton("=");
btnGrp[16] = new JButton("C");
for (int i = 0; i < 17; i++) {
btnGrp[i].setFont(new Font("Dialog", Font.PLAIN, 18));
}
}
/*按网格顺序添加除“=”外16个按钮*/
public void addSouthJpn() {
int[] index={7,8,9,14,4,5,6,13,1,2,3,12,0,16,10,11};
for(int i=0;i<index.length;i++){
jpnSouth.add(btnGrp[index[i]]);
}
}
/*给按钮添加监听器*/
public void addListener() {
//给数字按钮添加监听器,btnGrp[10]是"."号
for (int i = 0; i <= 10; i++) {
btnGrp[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String button = ( (JButton) e.getSource()).getText();
//如果有运算操作符或文本框数字为0并且按钮不为“。”
if (lastPress == "运算符" || lastPress == "等号"
|| txtDis.getText().equals("0") && button != ".") {
txtDis.setText(""); //清空文本框
}
if (lastPress == "等号") {
operator = ""; //上次按的是=号,这次按数字,清空运算符
}
if (button == "." && txtDis.getText().indexOf(".") != -1) {
} //这次按的是.文本框内已经包含.这种情况什么都不做,防止.重复
else {
txtDis.setText(txtDis.getText() + button); //文本框添加内容
}
lastPress = "数字";
}
});
}
//给运算操作符按钮添加监听器
for (int i = 11; i <= 14; i++) {
btnGrp[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != "" && lastPress == "数字") { //运算符不为空并且之前按的是数字
(btnGrp[15].getActionListeners())[0].actionPerformed(e); //手动调用=号的监听响应实现=号功能
}
numSaved = Double.parseDouble(txtDis.getText()); //保存当前文本框的数字
operator = ( (JButton) e.getSource()).getText(); //保存运算操作符
lastPress = "运算符"; //添加运算符标记,下次文本框清空
}
});
}
//给等号按钮添加监听器
btnGrp[15].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double numNow = Double.parseDouble(txtDis.getText()); //当前文本框的数字
//运算符匹配操作
if (operator.equals("+")) {
txtDis.setText( (numSaved + numNow) + "");
}
else if (operator.equals("-")) {
if (lastPress == "等号") { //累记运算时的算法(一直按等号)
txtDis.setText( (numNow - numSaved) + "");
}
else {
txtDis.setText( (numSaved - numNow) + "");
}
}
else if (operator.equals("*")) {
txtDis.setText( (numSaved * numNow) + "");
}
else if (operator.equals("/")) {
if (lastPress == "等号") { //累记运算时的算法(一直按等号)
txtDis.setText( (numNow / numSaved) + "");
}
else {
txtDis.setText( (numSaved / numNow) + "");
}
}
if (lastPress != "等号") { //是第一次,以后按累记运算
numSaved = numNow; //保存数改为后一个数
}
lastPress = "等号"; //添加运算符标记,下次文本框清空
}
});
//给复位符“C”按钮添加监听器
btnGrp[16].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtDis.setText("0");
numSaved = 0;
operator = "";
lastPress = "";
}
});
}
}
=====================第二个类=============================
import javax.swing.*;
public class Test {
public Test() {
CalculatorFrame calframe = new CalculatorFrame(); //新窗口
calframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭
calframe.setLocationRelativeTo(null); //窗口置中
calframe.setVisible(true); //显示窗口
calframe.setDefaultLookAndFeelDecorated(false); //使用windows视感
calframe.setResizable(false); //窗口不可调大小
}
public static void main(String[] args) {
Test test = new Test();
}
}
/**
*
* 实现了系统计算器连续按"="和按"+","-","*","/"进行累记运算的模式
*
* */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalculatorFrame extends JFrame {
private JTextField txtDis = new JTextField("0", 16); //显示文本框
private JButton[] btnGrp = new JButton[17]; //17个按钮
private JPanel jpnMain = new JPanel(); //主面板
private JPanel jpnNorth = new JPanel(); //上面板
private JPanel jpnSouth = new JPanel(); //下面板
private double numSaved; //保存的前一个数
private String operator = ""; //保存的运算符号
private String lastPress = ""; //上一次按的按钮("数字","运算符" 或 "等号")
/*构造函数*/
public CalculatorFrame() {
//窗口设置
setTitle("计算器");
setSize(210, 250);
buttonInit(); //按钮初始化
//主面板设置
add(jpnMain);
jpnMain.setLayout(null);
jpnMain.add(jpnNorth);
jpnMain.add(jpnSouth);
jpnMain.add(btnGrp[15]);
btnGrp[15].setBounds(8, 180, 188, 30); //添加“=”按钮
//上面板设置
jpnNorth.setBounds(8, 10, 190, 30);
jpnNorth.add(txtDis);
txtDis.setHorizontalAlignment(JTextField.RIGHT); //设置文本右对齐
txtDis.setEditable(false);
//下面板设置
jpnSouth.setBounds(8, 60, 190, 120);
jpnSouth.setLayout(new GridLayout(4, 4));
addSouthJpn(); //添加17个按钮
//添加监听器
addListener();
}
/*17个按钮显示值初始化*/
public void buttonInit() {
for (int i = 0; i < 10; i++) {
btnGrp[i] = new JButton("" + i);
}
btnGrp[10] = new JButton(".");
btnGrp[11] = new JButton("+");
btnGrp[12] = new JButton("-");
btnGrp[13] = new JButton("*");
btnGrp[14] = new JButton("/");
btnGrp[15] = new JButton("=");
btnGrp[16] = new JButton("C");
for (int i = 0; i < 17; i++) {
btnGrp[i].setFont(new Font("Dialog", Font.PLAIN, 18));
}
}
/*按网格顺序添加除“=”外16个按钮*/
public void addSouthJpn() {
int[] index={7,8,9,14,4,5,6,13,1,2,3,12,0,16,10,11};
for(int i=0;i<index.length;i++){
jpnSouth.add(btnGrp[index[i]]);
}
}
/*给按钮添加监听器*/
public void addListener() {
//给数字按钮添加监听器,btnGrp[10]是"."号
for (int i = 0; i <= 10; i++) {
btnGrp[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String button = ( (JButton) e.getSource()).getText();
//如果有运算操作符或文本框数字为0并且按钮不为“。”
if (lastPress == "运算符" || lastPress == "等号"
|| txtDis.getText().equals("0") && button != ".") {
txtDis.setText(""); //清空文本框
}
if (lastPress == "等号") {
operator = ""; //上次按的是=号,这次按数字,清空运算符
}
if (button == "." && txtDis.getText().indexOf(".") != -1) {
} //这次按的是.文本框内已经包含.这种情况什么都不做,防止.重复
else {
txtDis.setText(txtDis.getText() + button); //文本框添加内容
}
lastPress = "数字";
}
});
}
//给运算操作符按钮添加监听器
for (int i = 11; i <= 14; i++) {
btnGrp[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != "" && lastPress == "数字") { //运算符不为空并且之前按的是数字
(btnGrp[15].getActionListeners())[0].actionPerformed(e); //手动调用=号的监听响应实现=号功能
}
numSaved = Double.parseDouble(txtDis.getText()); //保存当前文本框的数字
operator = ( (JButton) e.getSource()).getText(); //保存运算操作符
lastPress = "运算符"; //添加运算符标记,下次文本框清空
}
});
}
//给等号按钮添加监听器
btnGrp[15].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double numNow = Double.parseDouble(txtDis.getText()); //当前文本框的数字
//运算符匹配操作
if (operator.equals("+")) {
txtDis.setText( (numSaved + numNow) + "");
}
else if (operator.equals("-")) {
if (lastPress == "等号") { //累记运算时的算法(一直按等号)
txtDis.setText( (numNow - numSaved) + "");
}
else {
txtDis.setText( (numSaved - numNow) + "");
}
}
else if (operator.equals("*")) {
txtDis.setText( (numSaved * numNow) + "");
}
else if (operator.equals("/")) {
if (lastPress == "等号") { //累记运算时的算法(一直按等号)
txtDis.setText( (numNow / numSaved) + "");
}
else {
txtDis.setText( (numSaved / numNow) + "");
}
}
if (lastPress != "等号") { //是第一次,以后按累记运算
numSaved = numNow; //保存数改为后一个数
}
lastPress = "等号"; //添加运算符标记,下次文本框清空
}
});
//给复位符“C”按钮添加监听器
btnGrp[16].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtDis.setText("0");
numSaved = 0;
operator = "";
lastPress = "";
}
});
}
}
=====================第二个类=============================
import javax.swing.*;
public class Test {
public Test() {
CalculatorFrame calframe = new CalculatorFrame(); //新窗口
calframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭
calframe.setLocationRelativeTo(null); //窗口置中
calframe.setVisible(true); //显示窗口
calframe.setDefaultLookAndFeelDecorated(false); //使用windows视感
calframe.setResizable(false); //窗口不可调大小
}
public static void main(String[] args) {
Test test = new Test();
}
}
展开全部
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author fubin
* 标准计算器
* Download:http://www.codefans.net
*/
public class buatcalculator extends JFrame implements ActionListener
{
JPanel txtpanel,btnpanel,southpanel;
JTextField txtinput;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdes,kalibtn,bagibtn,tambahbtn,kurangbtn,clearbtn,equalbtn,aboutbtn;
GridLayout gl;
double simpan,hasil;
String operasi;
public buatcalculator()
{
super("Calculator Cabe");
txtpanel=new JPanel();
btnpanel=new JPanel();
southpanel=new JPanel();
txtinput=new JTextField(20);
txtinput.setEditable(false);
txtinput.setHorizontalAlignment(JTextField.RIGHT);
txtinput.setText("");
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bdes=new JButton(".");
kalibtn=new JButton("x");
kalibtn.setFont(new Font("Gothic",Font.BOLD,20));
kalibtn.setBackground(Color.black);
kalibtn.setForeground(Color.blue);
bagibtn=new JButton(":");
bagibtn.setFont(new Font("Gothic",Font.BOLD,20));
bagibtn.setBackground(Color.black);
bagibtn.setForeground(Color.blue);
tambahbtn=new JButton("+");
tambahbtn.setFont(new Font("Gothic",Font.BOLD,20));
tambahbtn.setBackground(Color.black);
tambahbtn.setForeground(Color.blue);
kurangbtn=new JButton("-");
kurangbtn.setFont(new Font("Gothic",Font.BOLD,20));
kurangbtn.setBackground(Color.black);
kurangbtn.setForeground(Color.blue);
aboutbtn=new JButton("About");
aboutbtn.setFont(new Font("Arial",Font.ITALIC,12));
aboutbtn.setBackground(Color.black);
aboutbtn.setForeground(Color.red);
aboutbtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent argh)
{
btnOKActionPerformed();
}
});
clearbtn=new JButton("C");
clearbtn.setFont(new Font("Arial",Font.ITALIC,12));
clearbtn.setBackground(Color.black);
clearbtn.setForeground(Color.red);
clearbtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent argh)
{
btnCancelActionPerformed();
}
});
equalbtn=new JButton("=");
equalbtn.setFont(new Font("Arial",Font.ITALIC,20));
equalbtn.setBackground(Color.black);
equalbtn.setForeground(Color.blue);
//putting event to the button
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);
bdes.addActionListener(this);
kalibtn.addActionListener(this);
bagibtn.addActionListener(this);
tambahbtn.addActionListener(this);
kurangbtn.addActionListener(this);
equalbtn.addActionListener(this);
getContentPane().add(txtpanel,BorderLayout.NORTH);
txtpanel.add(txtinput);
gl=new GridLayout(4,5,5,5);
btnpanel.setLayout(gl);
getContentPane().add(btnpanel);
btnpanel.add(b7);
btnpanel.add(b8);
btnpanel.add(b9);
btnpanel.add(kalibtn);
btnpanel.add(b4);
btnpanel.add(b5);
btnpanel.add(b6);
btnpanel.add(bagibtn);
btnpanel.add(b1);
btnpanel.add(b2);
btnpanel.add(b3);
btnpanel.add(tambahbtn);
btnpanel.add(b0);
btnpanel.add(bdes);
btnpanel.add(equalbtn);
btnpanel.add(kurangbtn);
getContentPane().add(southpanel,BorderLayout.SOUTH);
southpanel.add(clearbtn);
southpanel.add(aboutbtn);
setSize(250,300);
setVisible(true);
setLocation(300,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void btnOKActionPerformed()
{
JOptionPane.showMessageDialog(null,"标准计算器"+"\nCreated By Fubin (yonghe169@163.com) Make in china, 海南"+"\n这是我做过所有计算器中最简洁,最实用的一个了!","About",JOptionPane.INFORMATION_MESSAGE);
}
public void btnCancelActionPerformed()
{
txtinput.setText("");
}
//make event when a button clicked
public void actionPerformed(ActionEvent e)
{
try
{
Object objkcal=e.getSource();
if(objkcal==b1)
txtinput.setText(txtinput.getText()+"1");
else
if(objkcal==b2)
txtinput.setText(txtinput.getText()+"2");
else
if(objkcal==b3)
txtinput.setText(txtinput.getText()+"3");
else
if(objkcal==b4)
txtinput.setText(txtinput.getText()+"4");
else
if(objkcal==b5)
txtinput.setText(txtinput.getText()+"5");
else
if(objkcal==b6)
txtinput.setText(txtinput.getText()+"6");
else
if(objkcal==b7)
txtinput.setText(txtinput.getText()+"7");
else
if(objkcal==b8)
txtinput.setText(txtinput.getText()+"8");
else
if(objkcal==b9)
txtinput.setText(txtinput.getText()+"9");
else
if(objkcal==b0)
txtinput.setText(txtinput.getText()+"0");
else
if(objkcal==bdes)
txtinput.setText(txtinput.getText()+".");
else
if(objkcal==tambahbtn)
{
//save the first number to the var simpan
simpan=Double.parseDouble(txtinput.getText());
operasi="plus";
txtinput.setText("");
}
else
if(objkcal==kurangbtn)
{
//save the first number to the var simpan
simpan=Double.parseDouble(txtinput.getText());
operasi="minus";
txtinput.setText("");
}
else
if(objkcal==bagibtn)
{
//save the first number to the var simpan
simpan=Double.parseDouble(txtinput.getText());
operasi="bagi";
txtinput.setText("");
}
else
if(objkcal==kalibtn)
{
//save the first number to the var simpan
simpan=Double.parseDouble(txtinput.getText());
operasi="kali";
txtinput.setText("");
}
else
if(objkcal==equalbtn)
{
//catching the "plus" operation
if(operasi=="plus")
{
hasil=simpan+Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
else
//catching the "minus" operation
if(operasi=="minus")
{
hasil=simpan-Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
else
if(txtinput.equals(txtinput.getText())&&objkcal==kurangbtn)
{
hasil=simpan-Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
else
//catching the "bagi" operation
if(operasi=="bagi")
{
hasil=simpan/Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
else
//catching the "kali" operation
if(operasi=="kali")
{
hasil=simpan*Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
}
}
catch(Exception ouch)
{
JOptionPane.showMessageDialog(null,"错误输入","error",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String args[])
{
try
{
//使用显示风格
UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e)
{}
new buatcalculator();
}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author fubin
* 标准计算器
* Download:http://www.codefans.net
*/
public class buatcalculator extends JFrame implements ActionListener
{
JPanel txtpanel,btnpanel,southpanel;
JTextField txtinput;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdes,kalibtn,bagibtn,tambahbtn,kurangbtn,clearbtn,equalbtn,aboutbtn;
GridLayout gl;
double simpan,hasil;
String operasi;
public buatcalculator()
{
super("Calculator Cabe");
txtpanel=new JPanel();
btnpanel=new JPanel();
southpanel=new JPanel();
txtinput=new JTextField(20);
txtinput.setEditable(false);
txtinput.setHorizontalAlignment(JTextField.RIGHT);
txtinput.setText("");
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bdes=new JButton(".");
kalibtn=new JButton("x");
kalibtn.setFont(new Font("Gothic",Font.BOLD,20));
kalibtn.setBackground(Color.black);
kalibtn.setForeground(Color.blue);
bagibtn=new JButton(":");
bagibtn.setFont(new Font("Gothic",Font.BOLD,20));
bagibtn.setBackground(Color.black);
bagibtn.setForeground(Color.blue);
tambahbtn=new JButton("+");
tambahbtn.setFont(new Font("Gothic",Font.BOLD,20));
tambahbtn.setBackground(Color.black);
tambahbtn.setForeground(Color.blue);
kurangbtn=new JButton("-");
kurangbtn.setFont(new Font("Gothic",Font.BOLD,20));
kurangbtn.setBackground(Color.black);
kurangbtn.setForeground(Color.blue);
aboutbtn=new JButton("About");
aboutbtn.setFont(new Font("Arial",Font.ITALIC,12));
aboutbtn.setBackground(Color.black);
aboutbtn.setForeground(Color.red);
aboutbtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent argh)
{
btnOKActionPerformed();
}
});
clearbtn=new JButton("C");
clearbtn.setFont(new Font("Arial",Font.ITALIC,12));
clearbtn.setBackground(Color.black);
clearbtn.setForeground(Color.red);
clearbtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent argh)
{
btnCancelActionPerformed();
}
});
equalbtn=new JButton("=");
equalbtn.setFont(new Font("Arial",Font.ITALIC,20));
equalbtn.setBackground(Color.black);
equalbtn.setForeground(Color.blue);
//putting event to the button
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);
bdes.addActionListener(this);
kalibtn.addActionListener(this);
bagibtn.addActionListener(this);
tambahbtn.addActionListener(this);
kurangbtn.addActionListener(this);
equalbtn.addActionListener(this);
getContentPane().add(txtpanel,BorderLayout.NORTH);
txtpanel.add(txtinput);
gl=new GridLayout(4,5,5,5);
btnpanel.setLayout(gl);
getContentPane().add(btnpanel);
btnpanel.add(b7);
btnpanel.add(b8);
btnpanel.add(b9);
btnpanel.add(kalibtn);
btnpanel.add(b4);
btnpanel.add(b5);
btnpanel.add(b6);
btnpanel.add(bagibtn);
btnpanel.add(b1);
btnpanel.add(b2);
btnpanel.add(b3);
btnpanel.add(tambahbtn);
btnpanel.add(b0);
btnpanel.add(bdes);
btnpanel.add(equalbtn);
btnpanel.add(kurangbtn);
getContentPane().add(southpanel,BorderLayout.SOUTH);
southpanel.add(clearbtn);
southpanel.add(aboutbtn);
setSize(250,300);
setVisible(true);
setLocation(300,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void btnOKActionPerformed()
{
JOptionPane.showMessageDialog(null,"标准计算器"+"\nCreated By Fubin (yonghe169@163.com) Make in china, 海南"+"\n这是我做过所有计算器中最简洁,最实用的一个了!","About",JOptionPane.INFORMATION_MESSAGE);
}
public void btnCancelActionPerformed()
{
txtinput.setText("");
}
//make event when a button clicked
public void actionPerformed(ActionEvent e)
{
try
{
Object objkcal=e.getSource();
if(objkcal==b1)
txtinput.setText(txtinput.getText()+"1");
else
if(objkcal==b2)
txtinput.setText(txtinput.getText()+"2");
else
if(objkcal==b3)
txtinput.setText(txtinput.getText()+"3");
else
if(objkcal==b4)
txtinput.setText(txtinput.getText()+"4");
else
if(objkcal==b5)
txtinput.setText(txtinput.getText()+"5");
else
if(objkcal==b6)
txtinput.setText(txtinput.getText()+"6");
else
if(objkcal==b7)
txtinput.setText(txtinput.getText()+"7");
else
if(objkcal==b8)
txtinput.setText(txtinput.getText()+"8");
else
if(objkcal==b9)
txtinput.setText(txtinput.getText()+"9");
else
if(objkcal==b0)
txtinput.setText(txtinput.getText()+"0");
else
if(objkcal==bdes)
txtinput.setText(txtinput.getText()+".");
else
if(objkcal==tambahbtn)
{
//save the first number to the var simpan
simpan=Double.parseDouble(txtinput.getText());
operasi="plus";
txtinput.setText("");
}
else
if(objkcal==kurangbtn)
{
//save the first number to the var simpan
simpan=Double.parseDouble(txtinput.getText());
operasi="minus";
txtinput.setText("");
}
else
if(objkcal==bagibtn)
{
//save the first number to the var simpan
simpan=Double.parseDouble(txtinput.getText());
operasi="bagi";
txtinput.setText("");
}
else
if(objkcal==kalibtn)
{
//save the first number to the var simpan
simpan=Double.parseDouble(txtinput.getText());
operasi="kali";
txtinput.setText("");
}
else
if(objkcal==equalbtn)
{
//catching the "plus" operation
if(operasi=="plus")
{
hasil=simpan+Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
else
//catching the "minus" operation
if(operasi=="minus")
{
hasil=simpan-Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
else
if(txtinput.equals(txtinput.getText())&&objkcal==kurangbtn)
{
hasil=simpan-Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
else
//catching the "bagi" operation
if(operasi=="bagi")
{
hasil=simpan/Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
else
//catching the "kali" operation
if(operasi=="kali")
{
hasil=simpan*Double.parseDouble(txtinput.getText());
txtinput.setText(Double.toString(hasil));
}
}
}
catch(Exception ouch)
{
JOptionPane.showMessageDialog(null,"错误输入","error",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String args[])
{
try
{
//使用显示风格
UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e)
{}
new buatcalculator();
}
}
参考资料: 自己找的保存在电脑上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
很麻烦,需要写基本的button frame jpanel 等等,算法我是通过集合把每次算出来的值取下然后最后进行运算,判断算数优先级,反正麻烦得很。还把算数符号也取出来用来判断是什么运算。最后按等号输出最终答案。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询