📄 computerframe.java~275~
字号:
package computer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.PaneLayout;
import com.borland.jbcl.layout.*;
import javax.swing.border.TitledBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import java.util.Calendar;
/**
* <p>Title:zhangwenbo</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: ppworkplace</p>
*
* @author not attributable
* @version 1.0 for pp
*/
public class computerframe extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
PaneLayout paneLayout1 = new PaneLayout();
GridLayout gridLayout1 = new GridLayout();
JLabel labelinput = new JLabel();
JLabel labelresult = new JLabel();
TitledBorder titledBorder1 = new TitledBorder("");
TitledBorder titledBorder2 = new TitledBorder("");
Border border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
Color.white, Color.white, new Color(116, 118, 122),
new Color(166, 170, 175));
PaneLayout paneLayout2 = new PaneLayout();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JTextField textfield = new JTextField();
BorderLayout borderLayout1 = new BorderLayout();
XYLayout xYLayout1 = new XYLayout();
JButton btbackspace = new JButton();
JButton btce = new JButton();
JButton bt7 = new JButton();
JButton bt8 = new JButton();
JButton bt9 = new JButton();
JButton btdivide = new JButton();
JButton btsqrt = new JButton();
JButton bt4 = new JButton();
JButton bt5 = new JButton();
JButton bt6 = new JButton();
JButton btmulti = new JButton();
JButton btpercent = new JButton();
JButton bt1 = new JButton();
JButton bt2 = new JButton();
JButton bt3 = new JButton();
JButton btsub = new JButton();
JButton btreciprocal = new JButton();
JButton bt0 = new JButton();
JButton btplus = new JButton();
JButton btdot = new JButton();
JButton btadd = new JButton();
JButton btequal = new JButton();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenu jMenu2 = new JMenu();
JMenu jMenu3 = new JMenu();
String operaterone = null;
String operatertwo = null;
int computecase = 0;
boolean fuhaoresult=false;
boolean fuhaomath=false;
boolean havedotsymbol=false;
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
JMenuItem jMenuItem5 = new JMenuItem();
JMenuItem jMenuItem6 = new JMenuItem();
JMenuItem jMenuItem7 = new JMenuItem();
public computerframe() {
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(paneLayout1);
this.setResizable(false);
setSize(new Dimension(355, 370));
setTitle("computer");
jMenuFile.setText("文件");
jMenuFileExit.setText("退出");
jMenuFileExit.addActionListener(new
computerframe_jMenuFileExit_actionAdapter(this));
//jMenuFileExit.addActionListener(new
// computerframe_jMenuFileExit_ActionAdapter(this));
jPanel1.setLayout(gridLayout1);
gridLayout1.setColumns(2);
gridLayout1.setHgap(1);
gridLayout1.setVgap(1);
labelinput.setBackground(UIManager.getColor("ToolBar.light"));
labelinput.setBorder(titledBorder2);
labelinput.setOpaque(true);
labelinput.setText("Input:");
labelresult.setBackground(UIManager.getColor("ToolBar.light"));
labelresult.setBorder(titledBorder1);
labelresult.setOpaque(true);
labelresult.setText("Result:");
jPanel1.setBackground(SystemColor.window);
jPanel1.setBorder(BorderFactory.createLoweredBevelBorder());
jPanel2.setLayout(paneLayout2);
textfield.setText("0");
textfield.setHorizontalAlignment(SwingConstants.RIGHT);
jPanel3.setLayout(borderLayout1);
borderLayout1.setVgap(5);
jPanel4.setLayout(xYLayout1);
btbackspace.setText("BackSpace");
btbackspace.addActionListener(new
computerframe_btbackspace_actionAdapter(this));
btce.setText("C E");
btce.addActionListener(new computerframe_btce_actionAdapter(this));
bt7.setText("7");
bt7.addActionListener(new computerframe_bt7_actionAdapter(this));
bt8.setText("8");
bt8.addActionListener(new computerframe_bt8_actionAdapter(this));
bt9.setText("9");
btdivide.setText("/");
btsqrt.setText("sqrt");
btsqrt.addActionListener(new computerframe_btsqrt_actionAdapter(this));
bt4.setText("4");
bt4.addActionListener(new computerframe_bt4_actionAdapter(this));
bt5.setText("5");
bt5.addActionListener(new computerframe_bt5_actionAdapter(this));
bt6.setText("6");
bt6.addActionListener(new computerframe_bt6_actionAdapter(this));
btmulti.setText("*");
btmulti.addActionListener(new computerframe_btmulti_actionAdapter(this));
btpercent.setText("%");
bt1.setText("1");
bt1.addActionListener(new computerframe_bt1_actionAdapter(this));
bt2.setText("2");
bt2.addActionListener(new computerframe_bt2_actionAdapter(this));
bt3.setText("3");
bt3.addActionListener(new computerframe_bt3_actionAdapter(this));
btsub.setText("-");
btreciprocal.setText("1/x");
btreciprocal.addActionListener(new
computerframe_btreciprocal_actionAdapter(this));
bt0.setText("0");
bt0.addActionListener(new computerframe_bt0_actionAdapter(this));
btplus.setText("+/-");
btplus.addActionListener(new computerframe_btplus_actionAdapter(this));
btdot.setText(".");
btadd.setText("+");
btadd.addActionListener(new computerframe_btadd_actionAdapter(this));
btequal.setText("=");
btequal.addActionListener(new computerframe_btequal_actionAdapter(this));
jPanel4.setBackground(UIManager.getColor("ToolBar.light"));
jMenu1.setText("符号");
jMenuItem1.setText("加");
jMenuItem1.addActionListener(new computerframe_jMenuItem1_actionAdapter(this));
jMenu2.setText("计算");
jMenu2.addMouseListener(new computerframe_jMenu2_mouseAdapter(this));
jMenu3.setText("退出"); //jMenu3.addActionListener(new computerframe_jMenu3_actionAdapter(this));
jMenu3.addMouseListener(new computerframe_jMenu3_mouseAdapter(this));
bt9.addActionListener(new computerframe_bt9_actionAdapter(this));
btsub.addActionListener(new computerframe_btsub_actionAdapter(this));
btdivide.addActionListener(new computerframe_btdivide_actionAdapter(this));
//btreciprocal.addActionListener(new
// computerframe_btreciprocal_actionAdapter(this));
btpercent.addActionListener(new computerframe_btpercent_actionAdapter(this));
btdot.addActionListener(new computerframe_btdot_actionAdapter(this));
jMenuItem2.setText("减");
jMenuItem2.addActionListener(new computerframe_jMenuItem2_actionAdapter(this));
jMenuItem3.setText("乘");
jMenuItem3.addActionListener(new computerframe_jMenuItem3_actionAdapter(this));
jMenuItem5.setText("1/x");
jMenuItem5.addActionListener(new computerframe_jMenuItem5_actionAdapter(this));
jMenuItem6.setText("%");
jMenuItem6.addActionListener(new computerframe_jMenuItem6_actionAdapter(this));
jMenuItem7.setText("sqrt");
jMenuItem7.addActionListener(new computerframe_jMenuItem7_actionAdapter(this));
jMenuItem4.addActionListener(new computerframe_jMenuItem4_actionAdapter(this));
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
jMenuFile.add(jMenuFileExit);
jPanel1.add(labelinput);
jPanel1.add(labelresult);
jPanel3.add(textfield, java.awt.BorderLayout.CENTER);
jPanel2.add(jPanel3, new PaneConstraints("jPanel3", "jPanel3",
PaneConstraints.ROOT, 0.5F));
jPanel2.add(jPanel4, new PaneConstraints("jPanel4", "jPanel3",
PaneConstraints.BOTTOM,
0.8850174F));
jPanel4.add(btsqrt, new XYConstraints(275, 69, 52, -1));
jPanel4.add(btpercent, new XYConstraints(275, 116, 52, -1));
jPanel4.add(btreciprocal, new XYConstraints(275, 163, 52, -1));
jPanel4.add(btequal, new XYConstraints(275, 208, 52, -1));
// jPanel4.add(btdivide,w XYConstraints(215,69,52,-1));
jPanel4.add(btdivide, new XYConstraints(215,69,52,-1));
jPanel4.add(bt9, new XYConstraints(150, 69, 52, -1));
jPanel4.add(bt7, new XYConstraints(20, 69, 52, -1));
jPanel4.add(btsub, new XYConstraints(215, 163, 52, -1));
jPanel4.add(bt6, new XYConstraints(150, 116, 52, -1));
jPanel4.add(bt3, new XYConstraints(150, 163, 52, -1));
jPanel4.add(btdot, new XYConstraints(150, 208, 52, -1));
jPanel4.add(bt2, new XYConstraints(85, 163, 52, -1));
jPanel4.add(btplus, new XYConstraints(85, 208, 52, -1));
jPanel4.add(bt4, new XYConstraints(20, 116, 52, -1));
jPanel4.add(bt1, new XYConstraints(20, 163, 52, -1));
jPanel4.add(bt0, new XYConstraints(20, 208, 52, -1));
jPanel4.add(btce, new XYConstraints(215, 19, 111, 26));
jPanel4.add(bt8, new XYConstraints(85, 69, 52, -1));
jPanel4.add(btbackspace, new XYConstraints(85, 19, 115, 26));
contentPane.add(jPanel2,
new PaneConstraints("jPanel2", "jPanel2",
PaneConstraints.ROOT, 0.5F));
contentPane.add(jPanel1,
new PaneConstraints("jPanel1", "jPanel2",
PaneConstraints.BOTTOM, 0.081009F));
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu1.add(jMenuItem3);
jMenu1.add(jMenuItem4);
jMenu1.addSeparator();
jMenu1.add(jMenuItem5);
jMenu1.add(jMenuItem6);
jMenu1.add(jMenuItem7);
jPanel4.add(bt2, new XYConstraints(85, 163, 52, 24));
jPanel4.add(bt5, new XYConstraints(85, 116, 52, 24));
jPanel4.add(btmulti, new XYConstraints(215, 116, 52, -1));
jPanel4.add(btadd, new XYConstraints(217, 208, 52, -1));
setJMenuBar(jMenuBar1);
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
Calendar c=Calendar.getInstance();
labelinput.setText("今天是 "+sdf.format(c.getTime()));
}
public void btce_actionPerformed(ActionEvent e) {
textfield.setText("0");
}
public void bt1_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("1");
}
public void bt2_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("2");
}
public void bt3_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("3");
}
public void bt4_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("4");
}
public void bt5_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("5");
}
public void bt6_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("6");
}
public void bt7_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("7");
}
public void bt8_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("8");
}
public void bt9_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("9");
}
public void bt0_actionPerformed(ActionEvent e) {
onetonine onenine=new onetonine();
onenine.onenine("0");
}
////加法computecase 1
////减法computecase 2
////乘法computecase 3
////除法computecase 4
////倒数computecase 5
////百分比computecase 6
////平方根computecase 7
public void btadd_actionPerformed(ActionEvent e) {
mathsymbol symbol=new mathsymbol();
symbol.mathsetup(1);
}
public void btsub_actionPerformed(ActionEvent e) {
mathsymbol symbol=new mathsymbol();
symbol.mathsetup(2);
}
public void btequal_actionPerformed(ActionEvent e) {
float a, b, result=0;
a = Float.parseFloat(operaterone);
operatertwo = textfield.getText();
b = Float.parseFloat(operatertwo);
switch (computecase) {
case 1:
result = a + b;
textfield.setText("" + result);
break;
case 2:
result = a - b;
textfield.setText("" + result);
break;
case 3:
result = a * b;
textfield.setText("" + result);
break;
case 4:
result = a / b;
textfield.setText("" + result);
break;
case 6:
result = a % b;
textfield.setText("" + result);
break;
}
this.labelresult.setText("Result: "+result);
if(textfield.getText().matches("0"))
textfield.setText("0");
fuhaoresult = true;
}
public void btmulti_actionPerformed(ActionEvent e) {
mathsymbol symbol=new mathsymbol();
symbol.mathsetup(3);
}
public void btdivide_actionPerformed(ActionEvent e) {
mathsymbol symbol=new mathsymbol();
symbol.mathsetup(4);
}
public void btreciprocal_actionPerformed(ActionEvent e) {
float daoshu,jieguo=0;
daoshu=Float.parseFloat(textfield.getText());
if(daoshu==0){
JOptionPane.showConfirmDialog(this," 0没有倒数","错误",JOptionPane.CLOSED_OPTION,JOptionPane.ERROR_MESSAGE);
textfield.setText("0");
}else{
jieguo=1/daoshu;
}
textfield.setText(""+jieguo);
fuhaomath = true;
}
public void btsqrt_actionPerformed(ActionEvent e) {
float sqrtnum;
sqrtnum=Float.parseFloat(textfield.getText());
if(sqrtnum<0){
JOptionPane.showConfirmDialog(this," 负数不能开根方","错误",JOptionPane.CLOSED_OPTION,JOptionPane.ERROR_MESSAGE);
}else{
textfield.setText(""+java.lang.Math.sqrt(sqrtnum));
}
fuhaomath = true;
}
public void btpercent_actionPerformed(ActionEvent e) {
mathsymbol symbol=new mathsymbol();
symbol.mathsetup(6);
}
public void btdot_actionPerformed(ActionEvent e) {
String beforetext=textfield.getText();
if(beforetext.matches("0")||beforetext.matches("0.")||beforetext.matches("0.0")||fuhaomath||fuhaoresult){
//if(havesymbol){
textfield.setText("0.");
fuhaomath=false;
fuhaoresult=false;
havedotsymbol=true;
// }
}else{
if(!havedotsymbol){
textfield.setText(beforetext + ".");
havedotsymbol=true;
}else{
textfield.setText(beforetext);
}
}
}
public void btplus_actionPerformed(ActionEvent e) {
String str=textfield.getText();
float tempnum=Float.parseFloat(str);
if(tempnum>0){
textfield.setText("-"+str);
//str.
}
if(tempnum<0){
textfield.setText(""+str.substring(1));
}
}
public void btbackspace_actionPerformed(ActionEvent e) {
String stri=textfield.getText();
float strii=Float.parseFloat(stri);
int striii=(int)strii/10;
textfield.setText(""+striii);
}
public void jMenuItem1_actionPerformed(ActionEvent e) {
btadd_actionPerformed(e);
}
public void jMenu3_mouseClicked(MouseEvent e) {
System.exit(0);
}
/*
public void jMenu2_menuSelected(MenuEvent e) {
//btequal_actionPerformed(e);
float a, b, result;
a = Float.parseFloat(operaterone);
operatertwo = textfield.getText();
b = Float.parseFloat(operatertwo);
switch (computecase) {
case 1:
result = a + b;
textfield.setText("" + result);
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -