//共两个类第一个calculator
//第二个MyFont
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;public class calculator implements
ActionListener,KeyListener,ItemListener{
MyFont myfont;
JFrame f;
JMenuBar mb;
JMenu mOption;
JCheckBoxMenuItem alwaysOnTop;
JTextField tf;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0; //数字按钮
JButton bA,bS,bM,bD,bDot,bE,bBack,bCE,bPN;//功能按钮
JButton bSqr,bSqrt,bRadical,bYx,bMod;
JPanel plC,plN,plE,plW,p; //按钮存放面板
char command,operator; //command存放操作命令执行码,操作符
String display,firstStr,secondStr; //存放显示表达式、第一个操作数、第二个操作数 //存储输入命令、操作符、屏幕显示和数的字符串
double result; //存储结果
boolean dotFlag; //数中是否已含有".",如果含有小数点则再按小数点
//不进行操作
public static void main(String[] args) {
calculator cp = new calculator();
cp.init();
}
//初始化,界面设置
public void init(){
myfont = new MyFont();
dotFlag = false;
command = 'c';
operator = 'c';
display = "";
firstStr = "";
secondStr = "";
result = 0.0;
mb = new JMenuBar();
mOption = new JMenu("选项");
alwaysOnTop = new JCheckBoxMenuItem("总在最前面",true);
mOption.add(alwaysOnTop);
mb.add(mOption);
alwaysOnTop.addItemListener(this);
tf = new JTextField("0");
tf.setFont(new Font("宋体",Font.PLAIN,18));
tf.setBackground(Color.ORANGE);
tf.setEnabled(false);
tf.setDisabledTextColor(Color.BLACK);
tf.setHorizontalAlignment(JTextField.RIGHT);
//new
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");
bA = new JButton("+");
bS = new JButton("-");
bM = new JButton("*");
bD = new JButton("/");
bDot = new JButton(".");
bE = new JButton("=");
bCE = new JButton("CE");
bBack = new JButton("<-");
bPN = new JButton("+/-");
bSqrt = new JButton("Sqrt");
bSqr = new JButton("x^2");
bRadical = new JButton("1/x");
bYx = new JButton("Y^x");
bMod = new JButton("%");//set
bS.setFont(new Font("Batang",Font.PLAIN,16));
bM.setFont(new Font("Batang",Font.PLAIN,16));
bA.setFont(new Font("Batang",Font.PLAIN,16));
bD.setFont(new Font("Batang",Font.PLAIN,12));
bPN.setFont(new Font("Batang",Font.PLAIN,16));
bMod.setFont(new Font("Batang",Font.PLAIN,12));
bS.setForeground(Color.RED);
bM.setForeground(Color.RED);
bA.setForeground(Color.RED);
bD.setForeground(Color.RED);
bE.setForeground(Color.RED);
bMod.setForeground(Color.RED);
//第二个MyFont
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;public class calculator implements
ActionListener,KeyListener,ItemListener{
MyFont myfont;
JFrame f;
JMenuBar mb;
JMenu mOption;
JCheckBoxMenuItem alwaysOnTop;
JTextField tf;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0; //数字按钮
JButton bA,bS,bM,bD,bDot,bE,bBack,bCE,bPN;//功能按钮
JButton bSqr,bSqrt,bRadical,bYx,bMod;
JPanel plC,plN,plE,plW,p; //按钮存放面板
char command,operator; //command存放操作命令执行码,操作符
String display,firstStr,secondStr; //存放显示表达式、第一个操作数、第二个操作数 //存储输入命令、操作符、屏幕显示和数的字符串
double result; //存储结果
boolean dotFlag; //数中是否已含有".",如果含有小数点则再按小数点
//不进行操作
public static void main(String[] args) {
calculator cp = new calculator();
cp.init();
}
//初始化,界面设置
public void init(){
myfont = new MyFont();
dotFlag = false;
command = 'c';
operator = 'c';
display = "";
firstStr = "";
secondStr = "";
result = 0.0;
mb = new JMenuBar();
mOption = new JMenu("选项");
alwaysOnTop = new JCheckBoxMenuItem("总在最前面",true);
mOption.add(alwaysOnTop);
mb.add(mOption);
alwaysOnTop.addItemListener(this);
tf = new JTextField("0");
tf.setFont(new Font("宋体",Font.PLAIN,18));
tf.setBackground(Color.ORANGE);
tf.setEnabled(false);
tf.setDisabledTextColor(Color.BLACK);
tf.setHorizontalAlignment(JTextField.RIGHT);
//new
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");
bA = new JButton("+");
bS = new JButton("-");
bM = new JButton("*");
bD = new JButton("/");
bDot = new JButton(".");
bE = new JButton("=");
bCE = new JButton("CE");
bBack = new JButton("<-");
bPN = new JButton("+/-");
bSqrt = new JButton("Sqrt");
bSqr = new JButton("x^2");
bRadical = new JButton("1/x");
bYx = new JButton("Y^x");
bMod = new JButton("%");//set
bS.setFont(new Font("Batang",Font.PLAIN,16));
bM.setFont(new Font("Batang",Font.PLAIN,16));
bA.setFont(new Font("Batang",Font.PLAIN,16));
bD.setFont(new Font("Batang",Font.PLAIN,12));
bPN.setFont(new Font("Batang",Font.PLAIN,16));
bMod.setFont(new Font("Batang",Font.PLAIN,12));
bS.setForeground(Color.RED);
bM.setForeground(Color.RED);
bA.setForeground(Color.RED);
bD.setForeground(Color.RED);
bE.setForeground(Color.RED);
bMod.setForeground(Color.RED);
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);
bA.addActionListener(this);
bS.addActionListener(this);
bM.addActionListener(this);
bD.addActionListener(this);
bDot.addActionListener(this);
bE.addActionListener(this);
bCE.addActionListener(this);
bBack.addActionListener(this);
bPN.addActionListener(this);
bMod.addActionListener(this);
bSqr.addActionListener(this);
bSqrt.addActionListener(this);
bRadical.addActionListener(this);
bYx.addActionListener(this);
//addKeyListener
b1.addKeyListener(this);
b2.addKeyListener(this);
b3.addKeyListener(this);
b4.addKeyListener(this);
b5.addKeyListener(this);
b6.addKeyListener(this);
b7.addKeyListener(this);
b8.addKeyListener(this);
b9.addKeyListener(this);
b0.addKeyListener(this);
bA.addKeyListener(this);
bS.addKeyListener(this);
bM.addKeyListener(this);
bD.addKeyListener(this);
bDot.addKeyListener(this);
bE.addKeyListener(this);
//panel
plC = new JPanel();
plC.setLayout(new GridLayout(4,5,4,4));
plN = new JPanel();
plE = new JPanel();
plN.setLayout(new GridLayout(1,1,4,4));
plE.setLayout(new GridLayout(4,1,4,4));
plN.setSize(400,100);
plN.add(tf);
plW = new JPanel();
plW.setLayout(new GridLayout(4,1,4,4));
p = new JPanel();
p.setBorder(new EmptyBorder(4,4,4,4));
p.setLayout(new BorderLayout(5,5));
//panel add
plC.requestFocus(true);
plC.add(b1);
plC.add(b2);
plC.add(b3);
plC.add(bA);
plC.add(b4);
plC.add(b5);
plC.add(b6);
plC.add(bS);
plC.add(b7);
plC.add(b8);
plC.add(b9);
plC.add(bM); plC.add(b0);
plC.add(bDot);
plC.add(bE);
plC.add(bD); plE.add(bCE);
plE.add(bBack);
plE.add(bPN);
plE.add(bMod);
plW.add(bSqrt);
plW.add(bSqr);
plW.add(bRadical);
plW.add(bYx);
p.add(plC,BorderLayout.CENTER);
p.add(plN,BorderLayout.NORTH);
p.add(plE,BorderLayout.EAST);
p.add(plW,BorderLayout.WEST);
//frame
f = new JFrame("计算器");
f.setLayout(new BorderLayout(6,6));
f.setAlwaysOnTop(true);
f.setLocation(400,200);
f.add(p); f.setJMenuBar(mb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
plC.requestFocus();
}
//calculate
void calculate(){ //双目运算符分支计算
switch (operator){
case '+':
result = Double.parseDouble(firstStr)+
Double.parseDouble(secondStr);
break;
case '-':
result = Double.parseDouble(firstStr)-
Double.parseDouble(secondStr);
break;
case '*':
result = Double.parseDouble(firstStr)*
Double.parseDouble(secondStr);
break;
case '/':
result = Double.parseDouble(firstStr)/
Double.parseDouble(secondStr);
break;
case '%':
result = Double.parseDouble(firstStr)%
Double.parseDouble(secondStr);
break;
case '^':
result = Math.pow(Double.parseDouble(firstStr),
Double.parseDouble(secondStr));
break;
}
}
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
if (str == "CE")
command = 'c';
else if (str == "<-")
command = 'b';
else if (str == "+/-")
command = 'i';
else if (str == "Y^x")
command = '^';
else if (str == "Sqrt")
command = 'S';
else if (str == "x^2")
command = 'q';
else if (str == "1/x")
command = 'r';
else
command = e.getActionCommand().charAt(0);//获取第一个字母
getResult(command);
}
void getResult(char command){ //获取计算结果
switch(command){
//双目运算
case '+' :;
case '-' :;
case '*' :;
case '/' :;
case '%' :;
case '^' :
//如果操作符,第一、二个运算数已输入完整,但用户没有使用“=”进行计算
//而是直接点击数学运算符,则先计算并把结果作为第一个操作数并显示
if (operator != 'c' && firstStr !="" && secondStr != ""){
calculate();
dotFlag = false;
secondStr = "";
firstStr = "" + result;
display = firstStr + operator;
tf.setText(display);
}
//如果显示字符串中含有'='表示存在上次计算结果
//把上次的计算结果作为第一个数进行计算
if (display.contains("=")){
firstStr = "" + result;
display = firstStr;
tf.setText(display);
}
//如果第一个数和操作符输入完毕则对操作符进行更改
if (firstStr.length()>0 ){
if (operator != 'c')
display = display.substring(0,display.length()-1);
operator = command;
display += operator;
tf.setText(display);
dotFlag = false;
}
break;
//数字输入
case '0':
if (firstStr == "0" || secondStr == "0") //如果操作数为零不进行操作
break;
case '1':;
case '2':;
case '3':;
case '4':;
case '5':;
case '6':;
case '7':;
case '8':;
case '9':
if (display.contains("=")) //如果表达式包含"=",清除显示表达式
display = "";
if(operator == 'c') //如果操作符还未输入,追加输入第一个数
firstStr += command; //否则追加第二个数
else
secondStr += command;
display += command; //把输入追加到显示表达式
tf.setText(display);
break;
case '.':
if(operator == 'c'&& dotFlag==false){//判断书中是否已存在小数点
firstStr += command;
dotFlag = true;
display += command;
tf.setText(display);
}
if(operator != 'c' && dotFlag == false){
secondStr += command;
dotFlag = true;
display += command;
tf.setText(display);
}
break;//回车、等号
case '\n':
case '=':
if (operator != 'c' && firstStr !="" && secondStr != ""){
calculate();
dotFlag = false;
command = 'c';
operator = 'c';
firstStr = "";
secondStr = "";
display = display + " = " +result;
tf.setText(display);
}
break;
//清除、回退
case 'c':
firstStr = "";
secondStr = "";
operator = 'c';
tf.setText("0");
display = "";
break;
case 'b':
if (secondStr.length()>0){
secondStr = secondStr.substring(0,secondStr.length() -1);
display = display.substring(0,display.length() - 1);
tf.setText(display);
}
if (firstStr.length()>0 && secondStr.length()==0 && operator == 'c'){
firstStr = firstStr.substring(0,firstStr.length() -1);
display = display.substring(0,display.length() - 1);
tf.setText(display);
}
if (display.length() == 0)
tf.setText("0");
break;
//正负号
case 'i':
if (firstStr.length()>0 && operator == 'c'){
if (firstStr.charAt(0) != '-')
firstStr = '-' + firstStr;
else
firstStr = firstStr.substring(1);
display = firstStr;
tf.setText(display);
}
break;
//单目运算
case 'S':
case 'q':
case 'r':
if (firstStr.length()==0 && result != 0) //如果第一个数未输入且结果不等于0,
firstStr = "" + result; //把结果赋予第一个数
if (operator == 'c'){ //如果操作符已输入则不进行操作
if (firstStr.length()>0 && command == 'S'){
result = Math.sqrt(Double.parseDouble(firstStr));
display = firstStr + " 方根= " + result;
}
if (firstStr.length()>0 &&command == 'q'){
result = Double.parseDouble(firstStr)*Double.parseDouble(firstStr);
display = firstStr + " 平方= " + result;
}
if (firstStr.length()>0 &&command == 'r'){
result = 1/Double.parseDouble(firstStr);
display = firstStr + " 倒数= " + result;
}
firstStr = ""; //把结果赋予第一个数
tf.setText(display);
}
}
}
public void keyPressed(KeyEvent ke){
}
public void keyReleased(KeyEvent ke){
}
public void keyTyped(KeyEvent ke){
command = ke.getKeyChar();
getResult(command);
}
public void itemStateChanged(ItemEvent ie){
if (ie.getItem() == alwaysOnTop){
if (ie.getStateChange()==ItemEvent.DESELECTED)
f.setAlwaysOnTop(false);
else
f.setAlwaysOnTop(true);
}
}
}
import java.awt.Font;
import javax.swing.UIManager;
import java.awt.*;
/**
* 设置界面中所有组件的字体
*/public class MyFont {
Font font = null;
public MyFont() {
font = new Font("宋体",0,12);
UIManager.put("Button.font",font);
UIManager.put("Button.foreground",Color.BLUE);
UIManager.put("TextField.font",font);
UIManager.put("CheckBoxMenuItem.font",font);
UIManager.put("Label.font",font);
UIManager.put("RadioButton.font",font);
UIManager.put("CheckBox.font",font);
UIManager.put("List.font",font);
UIManager.put("Menu.font",font);
UIManager.put("MenuItem.font",font);
UIManager.put("ComboBox.font",font);
UIManager.put("TabbedPane.font",font);
}}
2.变量定义不规范
先好好把JAVA规范看看,养成好的编程习惯
现在还有一个困扰着我——把按钮放在panel上,如何使按钮随时都可获得焦点,以便监听KeyEvent事件。(我找了好多地方都没找到答案)
还有什么样的界面才算是好看的?
谢谢!
confirmButton.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "CLICK");
confirmButton.getActionMap().put("CLICK", this);