運行界面如下:
只有兩個文件,一個作框架支持頁面,一個為功能性頁面,很簡單,在參加省程序設計前期作為練習寫的,如果對你有用,你可以拿去看看,非常適合于初學者。
JSQ.java:
package jsq;
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
/**
*
Title: 計算器
Description: 計算器
Copyright: Copyright (c) 2005
Company: 西昌学院
/**
* Construct and show the application.
*/
public JSQ()
{
FrameJSQ frame = new FrameJSQ();
frame.setSize(330, 200);
frame.setResizable(false);
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
{
frame.pack();
}
else
{
frame.validate();
}
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
try
{
jbInit();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
/**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception exception)
{
exception.printStackTrace();
}
new JSQ();
}
});
}
private void jbInit()
throws Exception
{
}
}
FrameJSQ.java:
package jsq;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
Title: 计算器
Description: 计算器
Copyright: Copyright (c) 2005
Company: 西昌学院
public FrameJSQ()
{
try
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private
void jbInit()
throws Exception
{
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("计算器");
jBT3.setBounds(new Rectangle(140, 46, 46, 21));
jBT3.setText("3");
jBT3.addActionListener(new FrameJSP_jBT3_actionAdapter(this));
jBT1.setBounds(new Rectangle(27, 45, 45, 21));
jBT1.setText("1");
jBT1.addActionListener(new FrameJSP_jBT1_actionAdapter(this));
jBT4.setBounds(new Rectangle(27, 71, 45, 21));
jBT4.setText("4");
jBT4.addActionListener(new FrameJSP_jBT4_actionAdapter(this));
jBT5.setBounds(new Rectangle(82, 71, 45, 21));
jBT5.setText("5");
jBT5.addActionListener(new FrameJSP_jBT5_actionAdapter(this));
jBT6.setBounds(new Rectangle(140, 71, 44, 21));
jBT6.setText("6");
jBT6.addActionListener(new FrameJSP_jBT6_actionAdapter(this));
jBT7.setBounds(new Rectangle(27, 97, 45, 21));
jBT7.setText("7");
jBT7.addActionListener(new FrameJSP_jBT7_actionAdapter(this));
jBT9.setBounds(new Rectangle(140, 97, 44, 21));
jBT9.setText("9");
jBT9.addActionListener(new FrameJSP_jBT9_actionAdapter(this));
jBT0.setBounds(new Rectangle(27, 124, 44, 21));
jBT0.setText("0");
jBT0.addActionListener(new FrameJSP_jBT0_actionAdapter(this));
jBTDot.setBounds(new Rectangle(82, 124, 45, 21));
jBTDot.setText(".");
jBTDot.addActionListener(new FrameJSP_jBTDot_actionAdapter(this));
jBTZF.setBounds(new Rectangle(140, 124, 46, 21));
jBTZF.setHorizontalTextPosition(SwingConstants.CENTER);
jBTZF.setText("+/-");
jBTZF.addActionListener(new FrameJSP_jBTZF_actionAdapter(this));
jBT8.setBounds(new Rectangle(82, 97, 46, 21));
jBT8.setText("8");
jBT8.addActionListener(new FrameJSP_jBT8_actionAdapter(this));
jBT2.setBounds(new Rectangle(82, 46, 45, 21));
jBT2.setText("2");
jBT2.addActionListener(new FrameJSP_jBT2_actionAdapter(this));
jBTSub.setBounds(new Rectangle(196, 71, 43, 21));
jBTSub.setToolTipText("");
jBTSub.setText("-");
jBTSub.addActionListener(new FrameJSP_jBTSub_actionAdapter(this));
jBTMulti.setBounds(new Rectangle(196, 98, 43, 21));
jBTMulti.setText("*");
jBTMulti.addActionListener(new FrameJSP_jBTMulti_actionAdapter(this));
jBTDiv.setBounds(new Rectangle(197, 125, 42, 21));
jBTDiv.setText("/");
jBTDiv.addActionListener(new FrameJSP_jBTDiv_actionAdapter(this));
jBTDel.setBounds(new Rectangle(248, 46, 50, 21));
jBTDel.setText("C");
jBTDel.addActionListener(new FrameJSP_jBTDel_actionAdapter(this));
jBTCleanAll.setBounds(new Rectangle(248, 74, 50, 21));
jBTCleanAll.setText("CE");
jBTCleanAll.addActionListener(new FrameJSP_jBTCleanAll_actionAdapter(this));
jBTResult.setBounds(new Rectangle(248, 125, 51, 21));
jBTResult.setText("=");
jBTResult.addActionListener(new FrameJSP_jBTResult_actionAdapter(this));
jBTAdd.setBounds(new Rectangle(195, 46, 44, 21));
jBTAdd.setText("+");
jBTAdd.addActionListener(new FrameJSP_jBTAdd_actionAdapter(this));
jTxtDis.setActionMap(null);
jTxtDis.setEditable(false);
jTxtDis.setText("0");
jTxtDis.setHorizontalAlignment(SwingConstants.RIGHT);
jTxtDis.setBounds(new Rectangle(28, 19, 267, 24));
jBTExit.setBounds(new Rectangle(248, 99, 51, 21));
jBTExit.setText("Bye");
jBTExit.addActionListener(new FrameJSP_jBTExit_actionAdapter(this));
contentPane.add(jBT4);
contentPane.add(jBT7);
contentPane.add(jBT5);
contentPane.add(jBT3);
contentPane.add(jBT6);
contentPane.add(jBT9);
contentPane.add(jBT8);
contentPane.add(jBTZF);
contentPane.add(jBT0);
contentPane.add(jBT2);
contentPane.add(jBTDot);
contentPane.add(jBTSub);
contentPane.add(jBTMulti);
contentPane.add(jBTDiv);
contentPane.add(jBTDel);
contentPane.add(jBTCleanAll);
contentPane.add(jBTAdd);
contentPane.add(jBT1);
contentPane.add(jTxtDis);
contentPane.add(jBTExit);
contentPane.add(jBTResult);
}
//输入的字符串起来
String nowInputStr(String inStr)
{
inputStr1 = inputStr1.concat(inStr);
//这里就是确定当点击等号后马上就点数字,
//那么就表示,这是一个新数计算的开始,把原来保存的临时结果删除
if (isResultClicked == true && isFirstNumberClicked == false)
{
isResultClicked = false; //去掉已经点击等号的标记
inputStr2 = "";
}
return inputStr1;
}
//清楚一个字符
String nowInputStrAfterDel()
{
if (inputStr1.length() > 0 && !inputStr1.equals("0"))
{ //有且不为0
int num;
num = inputStr1.length();
inputStr1 = inputStr1.substring(0, num - 1);
if (inputStr1.equals("") || inputStr1.equals("0."))
{ //如果没有了就返回0
return "0";
}
return inputStr1;
}
else
{
return "0";
}
}
public void jBT1_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStr(jBT1.getText()));
/*
if (isResultClicked == true && isFirstNumberClicked==false) {
isResultClicked = false;
inputStr2="";
}*/
isFirstNumberClicked = true;
}
public void jBTDel_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStrAfterDel());
isFirstNumberClicked = true;
//isResultClicked = false;
}
public void jBT2_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStr(jBT2.getText()));
isFirstNumberClicked = true;
}
public void jBT3_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStr(jBT3.getText()));
isFirstNumberClicked = true;
}
public void jBT4_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStr(jBT4.getText()));
isFirstNumberClicked = true;
}
public void jBT5_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStr(jBT5.getText()));
isFirstNumberClicked = true;
}
public void jBT6_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStr(jBT6.getText()));
isFirstNumberClicked = true;
}
public void jBT7_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStr(jBT7.getText()));
isFirstNumberClicked = true;
}
public void jBT8_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStr(jBT8.getText()));
isFirstNumberClicked = true;
}
public void jBT9_actionPerformed(ActionEvent e)
{
jTxtDis.setText(nowInputStr(jBT9.getText()));
isFirstNumberClicked = true;
}
public void jBT0_actionPerformed(ActionEvent e)
{
if (isFirstNumberClicked == true)
{ //如果一开始就按0就不处理
jTxtDis.setText(nowInputStr(jBT0.getText()));
}
}
public void jBTDot_actionPerformed(ActionEvent e)
{
//如果刚开始没有点击数字或者是已经将输入转移或者是清空,那么点击点的时候,就作为小数的开始
if (isFirstNumberClicked == false || inputStr1.equals("0") ||
inputStr1.equals(""))
{
isFirstDotClicked = true;
inputStr1 = "0.";
}
//否则就是整数后面加小数的意思 ,并且字符串中没有点符
else if (isFirstDotClicked == false && isFirstNumberClicked == true)
{
inputStr1 = inputStr1 + ".";
}
isFirstDotClicked = true;
}
public void jBTZF_actionPerformed(ActionEvent e)
{
if (isFirstDotClicked | isFirstNumberClicked)
{ //如果已经点了数字或小数点
if (inputStr1.startsWith("-"))
{
int num;
num = inputStr1.length();
inputStr1 = inputStr1.substring(1, num);
}
else
{
inputStr1 = "-" + inputStr1;
}
jTxtDis.setText(inputStr1);
}
}
public void jBTCleanAll_actionPerformed(ActionEvent e)
{
isFirstNumberClicked = false;
isFirstDotClicked = false;
isThereAreSomethingTodo = false;
isResultClicked = false;
doWhat = 0;
inputStr1 = "";
inputStr2 = "";
resultAll = 0;
tempResult = 0;
jTxtDis.setText("0");
}
void fourActionPerformed(int DoWhat)
{
//函数说明:当前函数要依DoWhat的值(1,2,3,4)来执行的不同的操作(加、减、乘、除)
//
doWhat = DoWhat; //当前要执行加
//如果一个计算的开始是点击的数字或者是点击的点,那就说明用户开始不是点击的另外的键
if ((isFirstDotClicked | isFirstNumberClicked))
{
if (isThereAreSomethingTodo == true && !inputStr1.equals("") &&
!inputStr2.equals(""))
{ //保证有有动或者是两个要计算的数都已经输入
tempResult = Double.parseDouble(inputStr2);
tempResult2 = Double.parseDouble(inputStr1);
if (doWhat == 1)
{ //如果是加
resultAll = tempResult + tempResult2;
}
if (doWhat == 2)
{ //如果是减
resultAll = tempResult - tempResult2;
}
if (doWhat == 3)
{ //如果是乘
resultAll = tempResult * tempResult2;
}
if (doWhat == 4)
{ //如果是除
resultAll = tempResult / tempResult2;
}
inputStr1 = String.valueOf(resultAll);
jTxtDis.setText(inputStr1); //显示结果到文本框中
inputStr2 = inputStr1; //保存当前结果
inputStr1 = ""; //清空当前用于接收的字符串
//doWhat = 1; //当前要执行加
}
else
{
//如果只击了一个操作数,那么点击这个按钮就说明后面输入的数就和现在这个数进行当前以
//DoWhat的值相应的动作,所以下面将有动作要作的开关打开
isThereAreSomethingTodo = true;
//doWhat = 1;
if (isResultClicked == false)
{ //要这一句可以在点击了等于后可以继续相加
//如果没有点击等号,那么就原来输入的数保存在另一个字符串中
//保存第一个数
inputStr2 = inputStr1;
}
//清空当前字符串,便于第二个数的输入
inputStr1 = "";
}
}
//如果是刚点完了等号就来点击加号,那么第一要去掉点击等号标记,说明当明的计算是在原来的结果基础之上
else if (isResultClicked == true && isFirstNumberClicked == false)
{
isResultClicked = false;
isFirstNumberClicked = true;
}
}
public void jBTAdd_actionPerformed(ActionEvent e)
{ //加
fourActionPerformed(1);
}
public void jBTSub_actionPerformed(ActionEvent e)
{ //减
fourActionPerformed(2);
}
public void jBTExit_actionPerformed(ActionEvent e)
{ //退出系统
System.exit(0);
}
public void jBTResult_actionPerformed(ActionEvent e)
{
if (isFirstDotClicked | isFirstNumberClicked)
{ //已经输入有数
if (isThereAreSomethingTodo == true && !inputStr1.equals("") &&
!inputStr2.equals(""))
{ //两个都输入了
tempResult = Double.parseDouble(inputStr2);
tempResult2 = Double.parseDouble(inputStr1);
if (doWhat == 1)
{ //如果是加
resultAll = tempResult + tempResult2;
}
if (doWhat == 2)
{ //如果是减
resultAll = tempResult - tempResult2;
}
if (doWhat == 3)
{ //如果是乘
resultAll = tempResult * tempResult2;
}
if (doWhat == 4)
{ //如果是除
resultAll = tempResult / tempResult2;
}
inputStr1 = String.valueOf(resultAll);
jTxtDis.setText(inputStr1);
inputStr2 = inputStr1;
inputStr1 = ""; //现在这样就不能够执行连加操作
doWhat = 0; //还原当前操作状态
isResultClicked = true;
isFirstNumberClicked = false;
//isThereAreSomethingTodo = false;保留该状态,后面可能还会用
}
else if (isResultClicked == true)
{ //如果刚才执行了等于操作,如果又还续点击等于按钮
}
}
}
public void jBTMulti_actionPerformed(ActionEvent e)
{ //乘
fourActionPerformed(3);
}
public void jBTDiv_actionPerformed(ActionEvent e)
{ //除
fourActionPerformed(4);
}
}
class FrameJSP_jBTDiv_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTDiv_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTDiv_actionPerformed(e);
}
}
class FrameJSP_jBTMulti_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTMulti_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTMulti_actionPerformed(e);
}
}
class FrameJSP_jBTResult_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTResult_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTResult_actionPerformed(e);
}
}
class FrameJSP_jBTExit_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTExit_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTExit_actionPerformed(e);
}
}
class FrameJSP_jBTSub_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTSub_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTSub_actionPerformed(e);
}
}
class FrameJSP_jBTAdd_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTAdd_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTAdd_actionPerformed(e);
}
}
class FrameJSP_jBTCleanAll_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTCleanAll_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTCleanAll_actionPerformed(e);
}
}
class FrameJSP_jBTZF_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTZF_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTZF_actionPerformed(e);
}
}
class FrameJSP_jBTDot_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTDot_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTDot_actionPerformed(e);
}
}
class FrameJSP_jBT0_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT0_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT0_actionPerformed(e);
}
}
class FrameJSP_jBT9_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT9_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT9_actionPerformed(e);
}
}
class FrameJSP_jBT8_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT8_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT8_actionPerformed(e);
}
}
class FrameJSP_jBT7_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT7_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT7_actionPerformed(e);
}
}
class FrameJSP_jBT6_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT6_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT6_actionPerformed(e);
}
}
class FrameJSP_jBT5_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT5_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT5_actionPerformed(e);
}
}
class FrameJSP_jBT4_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT4_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT4_actionPerformed(e);
}
}
class FrameJSP_jBT3_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT3_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT3_actionPerformed(e);
}
}
class FrameJSP_jBT2_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT2_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT2_actionPerformed(e);
}
}
class FrameJSP_jBTDel_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBTDel_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBTDel_actionPerformed(e);
}
}
class FrameJSP_jBT1_actionAdapter
implements ActionListener
{
private FrameJSQ adaptee;
FrameJSP_jBT1_actionAdapter(FrameJSQ adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jBT1_actionPerformed(e);
}
}