📄 frame1.java~94~
字号:
package calculator;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import javax.swing.UIManager;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
JButton jButton7 = new JButton();
JButton jButton8 = new JButton();
JButton jButton9 = new JButton();
JButton jButton10 = new JButton();
JButton jButton11 = new JButton();
JButton jButton12 = new JButton();
JButton jButton13 = new JButton();
JButton jButton14 = new JButton();
JButton jButton15 = new JButton();
JButton jButton16 = new JButton();
JButton jButton17 = new JButton();
JButton jButton18 = new JButton();
JButton jButton19 = new JButton();
JTextField readout = new JTextField();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
String Op1,Op2;
boolean Decimalflag;
int numops;
String lastinput;
int opflag;
String tempreadout;
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
keylisten stli=new keylisten(this);
contentPane.addKeyListener(stli);
jButton1.addKeyListener(stli);
jButton2.addKeyListener(stli);
jButton3.addKeyListener(stli);
jButton4.addKeyListener(stli);
jButton5.addKeyListener(stli);
jButton6.addKeyListener(stli);
jButton7.addKeyListener(stli);
jButton8.addKeyListener(stli);
jButton9.addKeyListener(stli);
jButton10.addKeyListener(stli);
jButton11.addKeyListener(stli);
jButton12.addKeyListener(stli);
jButton13.addKeyListener(stli);
jButton14.addKeyListener(stli);
jButton15.addKeyListener(stli);
jButton16.addKeyListener(stli);
jButton17.addKeyListener(stli);
jButton18.addKeyListener(stli);
jButton19.addKeyListener(stli);
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("简易计算器");
jButton1.setBounds(new Rectangle(23, 41, 41, 29));
jButton1.setText("7");
jButton2.setBounds(new Rectangle(94, 41, 39, 29));
jButton2.setText("8");
jButton3.setBounds(new Rectangle(163, 41, 40, 29));
jButton3.setText("9");
jButton4.setBounds(new Rectangle(250, 41, 44, 29));
jButton4.setText("C");
jButton5.setBounds(new Rectangle(309, 41, 47, 28));
jButton5.setText("CE");
jButton6.setBounds(new Rectangle(22, 86, 39, 30));
jButton6.setText("4");
jButton7.setBounds(new Rectangle(94, 86, 38, 29));
jButton7.setText("5");
jButton8.setBounds(new Rectangle(163, 86, 40, 28));
jButton8.setText("6");
jButton9.setBounds(new Rectangle(250, 86, 44, 28));
jButton9.setText("+");
jButton10.setBounds(new Rectangle(309, 86, 47, 27));
jButton10.setText("-");
jButton11.setBounds(new Rectangle(22, 132, 39, 30));
jButton11.setText("1");
jButton12.setBounds(new Rectangle(94, 131, 38, 29));
jButton12.setText("2");
jButton13.setBounds(new Rectangle(163, 132, 40, 28));
jButton13.setText("3");
jButton14.setBounds(new Rectangle(250, 132, 44, 28));
jButton14.setText("*");
jButton15.setBounds(new Rectangle(310, 132, 47, 28));
jButton15.setText("/");
jButton16.setBounds(new Rectangle(23, 175, 107, 31));
jButton16.setText("0");
jButton17.setBounds(new Rectangle(163, 175, 40, 31));
jButton17.setText(".");
jButton18.setBounds(new Rectangle(250, 176, 43, 31));
jButton18.setText("=");
jButton18.requestFocus(true) ;
jButton19.setText("%");
readout.setBorder(BorderFactory.createLineBorder(Color.black));
readout.setBounds(new Rectangle(23, 12, 332, 20));
readout.setHorizontalAlignment(JTextField.RIGHT);
// contentPane.setNextFocusableComponent(jButton18);
// contentPane.setRequestFocusEnabled(false);
//readout.setHorizontalAlignment(JTextField.RIGHT);
//contentPane.setNextFocusableComponent(null);
contentPane.setToolTipText("");
contentPane.addKeyListener(new Frame1_contentPane_keyAdapter(this));
jButton16.addActionListener(new Frame1_jButton16_actionAdapter(this));
jButton11.addActionListener(new Frame1_jButton11_actionAdapter(this));
jButton12.addActionListener(new Frame1_jButton12_actionAdapter(this));
jButton13.addActionListener(new Frame1_jButton13_actionAdapter(this));
jButton6.addActionListener(new Frame1_jButton6_actionAdapter(this));
jButton7.addActionListener(new Frame1_jButton7_actionAdapter(this));
jButton8.addActionListener(new Frame1_jButton8_actionAdapter(this));
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jButton3.addActionListener(new Frame1_jButton3_actionAdapter(this));
jButton9.addActionListener(new Frame1_jButton9_actionAdapter(this));
jButton10.addActionListener(new Frame1_jButton10_actionAdapter(this));
jButton14.addActionListener(new Frame1_jButton14_actionAdapter(this));
jButton15.addActionListener(new Frame1_jButton15_actionAdapter(this));
jButton18.addActionListener(new Frame1_jButton18_actionAdapter(this));
jButton19.addActionListener(new Frame1_jButton19_actionAdapter(this));
jButton4.addActionListener(new Frame1_jButton4_actionAdapter(this));
jButton5.addActionListener(new Frame1_jButton5_actionAdapter(this));
jButton17.addActionListener(new Frame1_jButton17_actionAdapter(this));
jButton19.setBounds(new Rectangle(313, 176, 47, 31));
readout.setText("123");
readout.setEditable(false);
//jButton18.setNextFocusableComponent(this); //jButton16.setSelected(true);
jButton18.setSelected(true); // jButton19.setNextFocusableComponent(jButton18);
readout.setForeground(UIManager.getColor("Button.focus")); //jButton18.setNextFocusableComponent(this);
contentPane.add(jButton13);
contentPane.add(jButton18);
contentPane.add(jButton16);
contentPane.add(jButton5);
contentPane.add(jButton9);
contentPane.add(jButton4);
contentPane.add(jButton17);
contentPane.add(jButton7);
contentPane.add(jButton2);
contentPane.add(jButton1, null);
contentPane.add(jButton3);
contentPane.add(jButton6);
contentPane.add(jButton8);
contentPane.add(jButton10);
contentPane.add(jButton14);
contentPane.add(jButton15);
contentPane.add(jButton12);
contentPane.add(jButton11);
contentPane.add(readout);
contentPane.add(jButton19);
Decimalflag=false;
numops=0;
lastinput="NONE";
opflag=0;
readout.setText("0.");
}
public void calculate(int Mynumber)
{
int index=Mynumber;
if(lastinput!="NUM")
{
readout.setText("0.");
Decimalflag=false;
}
String str="";
str=readout.getText();
if(Decimalflag==true)
{
readout.setText(readout.getText()+ Mynumber);
}
else
{
if (str.compareTo("0.")==0)
{
readout.setText("");
readout.setText(Mynumber + ".");
}
else
{
readout.setText(readout.getText().substring(0,
readout.getText().length() - 1) + Mynumber + ".");
}
}
if(lastinput == "NEG")
{
readout.setText("-" + readout.getText());
}
lastinput="NUM";
jButton18.requestFocus();
}
public void operator(int Myoperator)
{
int index = Myoperator;
tempreadout=readout.getText();
if(lastinput == "NUM")
{
numops=numops+1;
}
String myop;
//
switch(numops)
{
case 0:
if(Myoperator==3 && lastinput!= "NEG")
{
readout.setText("-"+readout.getText());
lastinput="NEG";
}
break;
case 1:
Op1=readout.getText() ;
if(Myoperator==3&&lastinput!="NUM"&&opflag!=4)
{
readout.setText("-");
lastinput="NEG";
}
break;
case 2:
Op2=tempreadout;
switch (opflag)
{
case 0:
if(Double.parseDouble(Op2)==0)
{
}
else
{
Op1=""+(Double.parseDouble(Op1)/Double.parseDouble(Op2));
}
break;
case 1:
Op1=""+(Double.parseDouble(Op1)+Double.parseDouble(Op2));
break;
case 2:
Op1=""+(Double.parseDouble(Op1)*Double.parseDouble(Op2));
break;
case 3:
Op1=""+(Double.parseDouble(Op1)-Double.parseDouble(Op2));
break;
case 4:
Op1=Op2;
}
readout.setText(Op1);
numops=1;
break;
}
if(lastinput!="NEG")
{
lastinput="OPS";
opflag=Myoperator;
}
jButton18.requestFocus();
}
public void jButton16_actionPerformed(ActionEvent e) {
this.calculate(0);
jButton18.requestFocus();
}
public void jButton11_actionPerformed(ActionEvent e) {
this.calculate(1);
jButton18.requestFocus();
}
public void jButton12_actionPerformed(ActionEvent e) {
this.calculate(2);
jButton18.requestFocus();
}
public void jButton13_actionPerformed(ActionEvent e) {
this.calculate(3);
jButton18.requestFocus();
}
public void jButton6_actionPerformed(ActionEvent e) {
this.calculate(4);
jButton18.requestFocus();
}
public void jButton7_actionPerformed(ActionEvent e) {
this.calculate(5);
jButton18.requestFocus();
}
public void jButton8_actionPerformed(ActionEvent e) {
this.calculate(6);
jButton18.requestFocus();
}
public void jButton1_actionPerformed(ActionEvent e) {
this.calculate(7);
jButton18.requestFocus();
}
public void jButton2_actionPerformed(ActionEvent e) {
this.calculate(8);
jButton18.requestFocus();
}
public void jButton3_actionPerformed(ActionEvent e) {
this.calculate(9);
}
public void jButton9_actionPerformed(ActionEvent e) {
this.operator(1);
jButton18.requestFocus();
}
public void jButton10_actionPerformed(ActionEvent e) {
this.operator(3);
jButton18.requestFocus();
}
public void jButton14_actionPerformed(ActionEvent e) {
this.operator(2);
jButton18.requestFocus();
}
public void jButton15_actionPerformed(ActionEvent e) {
this.operator(0);
jButton18.requestFocus();
}
public void jButton18_actionPerformed(ActionEvent e) {
this.operator(4);
jButton18.requestFocus();
}
public void percent()
{
readout.setText(""+Double.parseDouble(readout.getText())/100);
lastinput="OPS";
numops=numops+1;
Decimalflag=true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -