diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..35410cacdc5e87f985c93a96520f5e11a5c822e4 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/artifacts/java2022spring_jar.xml b/.idea/artifacts/java2022spring_jar.xml new file mode 100644 index 0000000000000000000000000000000000000000..7c40a8e90fe979b965854302ed995f442077b709 --- /dev/null +++ b/.idea/artifacts/java2022spring_jar.xml @@ -0,0 +1,9 @@ + + + $PROJECT_DIR$/out/artifacts/java2022spring_jar + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lib.xml b/.idea/libraries/lib.xml new file mode 100644 index 0000000000000000000000000000000000000000..43793b00437a6fddb8c0e41d0ec29563b530ac1d --- /dev/null +++ b/.idea/libraries/lib.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..e208459b8afde5f7980720efd6bbb97f7ae24541 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..ea25cb6475b9409a6cd5868ef50ea0cdc7ce9a84 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000000000000000000000000000000000000..e96534fb27b68192f27f985d3879e173ec77adb8 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/java2022spring.iml b/java2022spring.iml new file mode 100644 index 0000000000000000000000000000000000000000..e826ecca400b30b654092997a03090f8e1c917c8 --- /dev/null +++ b/java2022spring.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/beautyeye_lnf.jar b/lib/beautyeye_lnf.jar new file mode 100644 index 0000000000000000000000000000000000000000..cf1770e157db393faa8e99896411a1426cad1602 Binary files /dev/null and b/lib/beautyeye_lnf.jar differ diff --git a/out/artifacts/java2022spring_jar/java2022spring.jar b/out/artifacts/java2022spring_jar/java2022spring.jar new file mode 100644 index 0000000000000000000000000000000000000000..645da94a3159e86a228825b962eca62ea069e40c Binary files /dev/null and b/out/artifacts/java2022spring_jar/java2022spring.jar differ diff --git a/readme.md b/readme.md index 4af16add33b576d095308a1888db35774618715d..b5f44024dc57da36231b813cd49309df72f92a47 100644 --- a/readme.md +++ b/readme.md @@ -1,2 +1,12 @@ -# ѧ -0103 \ No newline at end of file +# !!!要使用Java8环境来打开此程序,否则会报错(Java17绝对不行,其他版本未进行尝试) +*** +[github]:(https://github.com/) +1. 此程序为计算器,是大一下的设计性实验的作业 +2. 此程序的界面灵感来源于[github]中[Houari ZEGAI](https://github.com/HouariZegai)的计算器(此外算法思路也有参考) +3. 此计算器功能仿win10计算器~~阉割版~~(包括相反数、根号、e的x次幂、绝对值、倒数、对数以及三角函数) +4. 此外还引用了github中[Jack Jiang](https://github.com/JackJiang2011)的[beautyeye](https://github.com/JackJiang2011/beautyeye)美化包,来优化UI(虽然就是因为引用了这个jar包,才导致此程序要用Java8的环境打开,不过大佬就是大佬) +5. 最后,Thank you for your watching. +*** +#### 一些碎碎念: +* ~~本来想着用不了大佬的包就算了~~,随便改改UI得了,但还是用了。不过包内的颜色有点太少了就几个颜色 ~~连黑色都没有~~ 为了加几个颜色我花了2~3天时间才把源码读懂并把颜色加到jar包中(重点是后者,走了不少弯路,还是我太菜了QAQ) +* 其实还是感觉有许多功能没有加上的,像是程序员模式、日期计算还有各种换算器(单位、货币、容量之类的),甚至科学模式也阉割了不少东西,要是加上这些东西的话,估计还得再写 ~~几个月~~ 几个星期,不过要考四级没时间再加点东西,挺可惜的。 \ No newline at end of file diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000000000000000000000000000000000000..5191bb868575ddfe311d9ad76fefd307eb4b9339 --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: java2022spring.Main + diff --git a/src/java2022spring/Button.java b/src/java2022spring/Button.java new file mode 100644 index 0000000000000000000000000000000000000000..226aed2f4678a12b30130509f749c263652e226c --- /dev/null +++ b/src/java2022spring/Button.java @@ -0,0 +1,20 @@ +package java2022spring; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionListener; + +public class Button extends JButton { + + private static final int Button_Width=115; + private static final int Button_Height=100; + + public Button(String name,ActionListener event){ + setText(name); + setSize(Button_Width,Button_Height); + addActionListener(event); + setFocusable(false);//取消焦点 + setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,28));//设置字体 + setCursor(new Cursor(Cursor.HAND_CURSOR));//设置鼠标样式 + } +} diff --git a/src/java2022spring/Images/title.png b/src/java2022spring/Images/title.png new file mode 100644 index 0000000000000000000000000000000000000000..bf6fb8c51c97e43c125ac237412c282ad2ac626d Binary files /dev/null and b/src/java2022spring/Images/title.png differ diff --git a/src/java2022spring/Main.java b/src/java2022spring/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..fb7feceed648603b9290adfe71191d663c90e20b --- /dev/null +++ b/src/java2022spring/Main.java @@ -0,0 +1,18 @@ +package java2022spring; + +import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper; + +import javax.swing.*; + +public class Main { + public static void main(String[] args) { + try + { + BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.translucencyAppleLike; + UIManager.put("RootPane.setupButtonVisible", false); + org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF(); + } + catch(Exception e) {} + Window window=new Window(); + } +} diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java deleted file mode 100644 index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..0000000000000000000000000000000000000000 --- a/src/java2022spring/Test.java +++ /dev/null @@ -1,7 +0,0 @@ -package java2022spring; - -public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} diff --git a/src/java2022spring/Window.java b/src/java2022spring/Window.java new file mode 100644 index 0000000000000000000000000000000000000000..387bb702055cbc6b467ada184dbd5ee5a9182d93 --- /dev/null +++ b/src/java2022spring/Window.java @@ -0,0 +1,1046 @@ +package java2022spring; + +import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI; + +import javax.swing.*; +import java.awt.*; +import java.util.regex.Pattern; + +public class Window extends JFrame { + private static final int Window_Width=500; + private static final int Window_Height=750; + private GridBagConstraints constraints_panel,constraints_window;//网格包约束条件 + private GridBagLayout bagLayout;//网格包布局 + private JTextArea text,text_history;//文本域 + private JScrollPane jsHistory; + private JPanel pNorth,pCenter;//面板 + private char option=' ';//保存符号 + private double value=0;//保存值 + private boolean result=false;//判断是否为结果 + private boolean connectnNum=true;//连接字符 + private boolean go=true;//判断是否进行计算 + //按钮 + private JButton but1,but2,but3,but4,but5,but6,but7,but8,but9,but0, + butPlus,butSubtract,butMultiply,butExcept,butAmount,butPoint, + butOpposite,butClear,butBack,butPer, + butRoot,butPow,butLog, + butSin,butCos,butTan,butExp,butFraction, + butPI,butJuedui; + + JDialog dialog;//历史记录窗口 + + private Image title;//软件图标 + + //主题选项和类型选项 + private JMenuBar menu; + private JMenu theme,type; + private JMenuItem theme_Dark,theme_Color,theme_Simple,type_Normal,type_Scientific,history, + //空项目(为历史记录腾空间) + empty01,empty02; + + public Window(){ + //初始化 + super("计算器"); + pNorth=new JPanel(); + pCenter=new JPanel(); + bagLayout=new GridBagLayout(); + setLayout(bagLayout); + constraints_panel =new GridBagConstraints(); + constraints_panel.fill=GridBagConstraints.BOTH; + constraints_panel.weightx=0.5; + constraints_panel.weighty=0.3; + constraints_window=new GridBagConstraints(); + constraints_window.fill=GridBagConstraints.BOTH; + constraints_window.weightx=0.5; + constraints_window.weighty=0.3; + + //设置图标 + title=Toolkit.getDefaultToolkit().createImage(Main.class.getResource("Images/title.png")); + setIconImage(title); + + //菜单添加 + menu=new JMenuBar(); + theme=new JMenu("主题"); + theme.setCursor(new Cursor(Cursor.HAND_CURSOR)); + type=new JMenu("模式"); + type.setCursor(new Cursor(Cursor.HAND_CURSOR)); + theme_Simple=new JMenuItem("简单主题"); + theme_Simple.setCursor(new Cursor(Cursor.HAND_CURSOR)); + theme_Simple.addActionListener(e -> { + text.setBackground(Color.WHITE); + text.setForeground(Color.BLACK); + pCenter.setBackground(Color.WHITE); + pNorth.setBackground(Color.WHITE); + butRoot.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butLog.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butPow.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butAmount.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butClear.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butBack.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butPer.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butExcept.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butPlus.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butMultiply.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butSubtract.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but0.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but1.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but2.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but3.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but4.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but5.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but6.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but7.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but8.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but9.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butPoint.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butOpposite.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butSin.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butCos.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butTan.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butExp.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butFraction.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butPI.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butJuedui.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + }); + theme_Dark=new JMenuItem("黑色主题"); + theme_Dark.setCursor(new Cursor(Cursor.HAND_CURSOR)); + theme_Dark.addActionListener(e -> { + text.setBackground(Color.GRAY); + text.setForeground(Color.white); + pNorth.setBackground(Color.GRAY); + pCenter.setBackground(Color.GRAY); + but0.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + but1.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + but2.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + but3.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + but4.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + but5.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + but6.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + but7.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + but8.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + but9.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + butPoint.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + butOpposite.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + butAmount.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butPlus.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butSubtract.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butLog.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butPow.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butMultiply.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butRoot.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butBack.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butPer.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butClear.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butExcept.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butSin.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butCos.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butTan.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butExp.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butFraction.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butPI.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butJuedui.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + }); + theme_Color=new JMenuItem("彩色主题"); + theme_Color.setCursor(new Cursor(Cursor.HAND_CURSOR)); + theme_Color.addActionListener(e -> { + text.setBackground(Color.WHITE); + text.setForeground(Color.BLACK); + pCenter.setBackground(Color.WHITE); + pNorth.setBackground(Color.WHITE); + butClear.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red)); + butBack.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green)); + butPer.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue)); + butExcept.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butMultiply.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butPlus.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + butSubtract.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark)); + but0.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but1.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but2.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but3.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but4.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but5.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but6.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but7.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but8.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + but9.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butPoint.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butOpposite.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal)); + butAmount.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue)); + butPow.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green)); + butLog.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue)); + butRoot.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red)); + butSin.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + butCos.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + butTan.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + butFraction.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red)); + butExp.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark)); + butPI.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green)); + butJuedui.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue)); + }); + type_Normal=new JMenuItem("标准模式"); + type_Normal.setCursor(new Cursor(Cursor.HAND_CURSOR)); + type_Normal.addActionListener(e -> { + setSize(Window_Width,Window_Height); + butRoot.setVisible(false); + butPow.setVisible(false); + butLog.setVisible(false); + butSin.setVisible(false); + butCos.setVisible(false); + butTan.setVisible(false); + butExp.setVisible(false); + butFraction.setVisible(false); + butPI.setVisible(false); + butJuedui.setVisible(false); + }); + type_Scientific=new JMenuItem("科学模式"); + type_Scientific.setCursor(new Cursor(Cursor.HAND_CURSOR)); + type_Scientific.addActionListener(e -> { + setSize(Window_Width+120,Window_Height+80); + butRoot.setVisible(true); + butLog.setVisible(true); + butPow.setVisible(true); + butSin.setVisible(true); + butCos.setVisible(true); + butTan.setVisible(true); + butExp.setVisible(true); + butFraction.setVisible(true); + butPI.setVisible(true); + butJuedui.setVisible(true); + }); + history=new JMenuItem("历史记录"); + history.setCursor(new Cursor(Cursor.HAND_CURSOR)); + history.addActionListener(e -> { + showDialog(); + }); + empty01=new JMenuItem(); + empty02=new JMenuItem(); + theme.add(theme_Simple); + theme.add(theme_Color); + theme.add(theme_Dark); + menu.add(theme); + type.add(type_Scientific); + type.add(type_Normal); + menu.add(type); + menu.add(history); + menu.add(empty01); + menu.add(empty02); + + //设置文本框 + text=new JTextArea("0"); + text.setLineWrap(true);//自动换行 + text.setWrapStyleWord(true);//断行不断字 + text.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40)); + text.setSize(500,250); + text.setEditable(false); + text.setFocusable(false);//取消焦点 + text.setBackground(Color.WHITE); + text.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + text_history=new JTextArea(""); + text_history.setWrapStyleWord(true);//断行不断字 + text_history.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40)); + text_history.setEditable(false); + text_history.setFocusable(false);//取消焦点 + text_history.setBackground(Color.WHITE); + text_history.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + jsHistory=new JScrollPane(text_history); + jsHistory.setSize(750,500); + jsHistory.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + + //设置按钮 + //“C”清除符 + butClear=new Button("C",e -> { + repaintFont(); + text.setText("0"); + option=' '; + value=0; + if(Pattern.matches("[0]*",text.getText())) + if(!result) + text_history.setText(""); + result=false; + }); + //"<-"退格符 + butBack=new Button("<-",e -> { + repaintFont(); + String str =text.getText(); + StringBuffer strB=new StringBuffer(); + for(int i=0;i<(str.length()-1);i++) + strB.append(str.charAt(i)); + if(!result) { + if (strB.toString().equals("")) + text.setText("0"); + else + text.setText(strB.toString()); + } + }); + //"%"取余符 + butPer=new Button("%",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else{ + text.setText(String.valueOf(value)); + } + option='%'; + connectnNum=false; + go=false; + } + } + }); + //"/"除号 + butExcept=new Button("/",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(value==(Double.valueOf(text.getText()))) + text_history.append(""+value+"/"); + else + text_history.append(text.getText()+"/"); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='/'; + connectnNum=false; + go=false; + } + else { + option='/'; + } + } + }); + //"√"根号 + butRoot=new Button("√",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='√'; + connectnNum=false; + go=false; + } + else { + option='√'; + } + } + }); + butRoot.setVisible(false); + //数字"7" + but7=new Button("7",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("7"); + } + else{ + text.setText(text.getText()+"7"); + } + } + else{ + text.setText("7"); + connectnNum=true; + } + go=true; + }); + //数字"8" + but8=new Button("8",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("8"); + } + else{ + text.setText(text.getText()+"8"); + } + } + else{ + text.setText("8"); + connectnNum=true; + } + go=true; + }); + //数字”9“ + but9=new Button("9",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("9"); + } + else{ + text.setText(text.getText()+"9"); + } + } + else{ + text.setText("9"); + connectnNum=true; + } + go=true; + }); + //"*"乘号 + butMultiply=new Button("*",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(value==(Double.valueOf(text.getText()))) + text_history.append(""+value+"*"); + else + text_history.append(text.getText()+"*"); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='*'; + connectnNum=false; + go=false; + } + else { + option='*'; + } + } + }); + //"^"次幂 + butPow=new Button("^",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(value==(Double.valueOf(text.getText()))) + text_history.append(""+value+"^"); + else + text_history.append(text.getText()+"^"); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='^'; + connectnNum=false; + go=false; + } + else { + option='^'; + } + } + }); + butPow.setVisible(false); + //数字"4" + but4=new Button("4",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("4"); + } + else{ + text.setText(text.getText()+"4"); + } + } + else{ + text.setText("4"); + connectnNum=true; + } + go=true; + }); + //数字"5" + but5=new Button("5",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("5"); + } + else{ + text.setText(text.getText()+"5"); + } + } + else{ + text.setText("5"); + connectnNum=true; + } + go=true; + }); + //数字”6“ + but6=new Button("6",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("6"); + } + else{ + text.setText(text.getText()+"6"); + } + } + else{ + text.setText("6"); + connectnNum=true; + } + go=true; + }); + //"-"减号 + butSubtract=new Button("-",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(value==(Double.valueOf(text.getText()))) + text_history.append(""+value+"-"); + else + text_history.append(text.getText()+"-"); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='-'; + connectnNum=false; + go=false; + } + else { + option='-'; + } + } + }); + //"ln"对数 + butLog=new Button("ln",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='l'; + connectnNum=false; + go=false; + } + else { + option='l'; + } + } + }); + butLog.setVisible(false); + //数字”1“ + but1=new Button("1",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("1"); + } + else{ + text.setText(text.getText()+"1"); + } + } + else{ + text.setText("1"); + connectnNum=true; + } + go=true; + }); + //数字”2“ + but2=new Button("2",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("2"); + } + else{ + text.setText(text.getText()+"2"); + } + } + else{ + text.setText("2"); + connectnNum=true; + } + go=true; + }); + //数字”3“ + but3=new Button("3",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("3"); + } + else{ + text.setText(text.getText()+"3"); + } + } + else{ + text.setText("3"); + connectnNum=true; + } + go=true; + }); + //"+"加号 + butPlus=new Button("+",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(value==(Double.valueOf(text.getText()))) + text_history.append(""+value+"+"); + else + text_history.append(text.getText()+"+"); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='+'; + connectnNum=false; + go=false; + } + else { + option='+'; + } + } + }); + //"+/-"负号 + butOpposite=new Button("+/-",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("0"); + } + else{ + text.setText(""+(Double.parseDouble(text.getText())*(-1))); + } + } + else{ + text.setText(""+(Double.parseDouble(text.getText())*(-1))); + } + go=true; + }); + //数字”0“ + but0=new Button("0",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText("0"); + } + else{ + text.setText(text.getText()+"0"); + } + } + else{ + text.setText("0"); + connectnNum=true; + } + go=true; + }); + //"."小数点 + butPoint=new Button(".",e -> { + repaintFont(); + if(connectnNum){ + if(!text.getText().contains(".")){ + text.setText(text.getText()+"."); + } + } + else{ + text.setText("0."); + connectnNum=true; + } + go=true; + }); + //"="等于号 + butAmount=new Button("=",e -> { + if(Pattern.matches("([-]?\\d+[.]\\d*)|([-]?\\d+)",text.getText())) { + if (go|option=='l'|option=='√'|option=='S'|option=='C'|option=='T'|option=='e'|option=='F'|option=='J') { + if(option=='l'){ + text_history.append("ln"+value); + } + else if(option=='√'){ + text_history.append("√"+value); + } + else if(option=='S'){ + text_history.append("sin"+value); + } + else if(option=='C'){ + text_history.append("cos"+value); + } + else if(option=='T'){ + text_history.append("tan"+value); + } + else if(option=='e'){ + text_history.append("e^"+value); + } + else if(option=='F'){ + text_history.append("1/"+value); + } + else if(option=='J'){ + text_history.append("|"+value+"|"); + } + value = calculate(value, text.getText(), option); + if(option=='l'|option=='√'|option=='S'|option=='C'|option=='T'|option=='e'|option=='F'|option=='J') + text_history.append("="+value+"\n"); + else + text_history.append(""+text.getText()+"="+value+"\n"); + text.setText(String.valueOf(value)); + option = '='; + connectnNum = false; + result = true; + + } + } + }); + //"sin"正弦函数 + butSin=new Button("Sin",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='S'; + connectnNum=false; + go=false; + } + else { + option='S'; + } + } + }); + butSin.setVisible(false); + //"cos"余弦函数 + butCos=new Button("Cos",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='C'; + connectnNum=false; + go=false; + } + else { + option='C'; + } + } + }); + butCos.setVisible(false); + //"tan"正切函数 + butTan=new Button("Tan",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='T'; + connectnNum=false; + go=false; + } + else { + option='T'; + } + } + }); + butTan.setVisible(false); + //"e^x"对数 + butExp=new Button("e^x",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='e'; + connectnNum=false; + go=false; + } + else { + option='e'; + } + } + }); + butExp.setVisible(false); + //"1/x"倒数 + butFraction=new Button("1/x",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='F'; + connectnNum=false; + go=false; + } + else { + option='F'; + } + } + }); + butFraction.setVisible(false); + //"π"常数π + butPI=new Button("π",e -> { + repaintFont(); + if(connectnNum){ + if(Pattern.matches("[0]*",text.getText())){ + text.setText(String.valueOf(Math.PI)); + connectnNum=false; + } + } + else{ + text.setText(String.valueOf(Math.PI)); + } + go=true; + }); + butPI.setVisible(false); + //"|x|"绝对值 + butJuedui=new Button("|x|",e -> { + repaintFont(); + if(Pattern.matches("([-]?\\d+[.]\\d*)|([-]?\\d+)",text.getText())){ + if(go){ + value=calculate(value,text.getText(),option); + if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){ + text.setText(String.valueOf((int)value)); + } + else { + text.setText(String.valueOf(value)); + } + option='J'; + connectnNum=false; + go=false; + } + else { + option='J'; + } + } + }); + butJuedui.setVisible(false); + + //菜单与文本框的面板设置 + pNorth.setLayout(new BorderLayout()); + pNorth.add(menu,BorderLayout.NORTH); + pNorth.add(text,BorderLayout.CENTER); + + //按钮面板设置 + //第一行 + pCenter.setLayout(bagLayout); + constraints_panel.gridwidth=1; + bagLayout.setConstraints(butRoot, constraints_panel); + pCenter.add(butRoot); + constraints_panel.gridx=1; + bagLayout.setConstraints(butPow, constraints_panel); + pCenter.add(butPow); + constraints_panel.gridx=2; + bagLayout.setConstraints(butPI,constraints_panel); + pCenter.add(butPI); + constraints_panel.gridx=3; + bagLayout.setConstraints(butJuedui,constraints_panel); + pCenter.add(butJuedui); + constraints_panel.gridx=4; + bagLayout.setConstraints(butLog, constraints_panel); + pCenter.add(butLog); + //第二行 + constraints_panel.gridwidth=1; + constraints_panel.gridx=0; + bagLayout.setConstraints(butFraction,constraints_panel); + pCenter.add(butFraction); + constraints_panel.gridx=1; + bagLayout.setConstraints(butClear, constraints_panel); + pCenter.add(butClear); + constraints_panel.gridx=2; + bagLayout.setConstraints(butBack, constraints_panel); + pCenter.add(butBack); + constraints_panel.gridx=3; + bagLayout.setConstraints(butPer, constraints_panel); + pCenter.add(butPer); + constraints_panel.gridx=4; + bagLayout.setConstraints(butExcept, constraints_panel); + pCenter.add(butExcept); + //第三行 + constraints_panel.gridwidth=1; + constraints_panel.gridx=0; + bagLayout.setConstraints(butExp, constraints_panel); + pCenter.add(butExp); + constraints_panel.gridx=1; + bagLayout.setConstraints(but7, constraints_panel); + pCenter.add(but7); + constraints_panel.gridx=2; + bagLayout.setConstraints(but8, constraints_panel); + pCenter.add(but8); + constraints_panel.gridx=3; + bagLayout.setConstraints(but9, constraints_panel); + pCenter.add(but9); + constraints_panel.gridx=4; + bagLayout.setConstraints(butMultiply, constraints_panel); + pCenter.add(butMultiply); + //第四行 + constraints_panel.gridwidth=1; + constraints_panel.gridx=0; + bagLayout.setConstraints(butTan,constraints_panel); + pCenter.add(butTan); + constraints_panel.gridx=1; + bagLayout.setConstraints(but4, constraints_panel); + pCenter.add(but4); + constraints_panel.gridx=2; + bagLayout.setConstraints(but5, constraints_panel); + pCenter.add(but5); + constraints_panel.gridx=3; + bagLayout.setConstraints(but6, constraints_panel); + pCenter.add(but6); + constraints_panel.gridx=4; + bagLayout.setConstraints(butSubtract, constraints_panel); + pCenter.add(butSubtract); + //第五行 + constraints_panel.gridwidth=1; + constraints_panel.gridx=0; + bagLayout.setConstraints(butCos,constraints_panel); + pCenter.add(butCos); + constraints_panel.gridx=1; + bagLayout.setConstraints(but1,constraints_panel); + pCenter.add(but1); + constraints_panel.gridx=2; + bagLayout.setConstraints(but2,constraints_panel); + pCenter.add(but2); + constraints_panel.gridx=3; + bagLayout.setConstraints(but3,constraints_panel); + pCenter.add(but3); + constraints_panel.gridx=4; + bagLayout.setConstraints(butPlus,constraints_panel); + pCenter.add(butPlus); + //第六行 + constraints_panel.gridwidth=1; + constraints_panel.gridx=0; + bagLayout.setConstraints(butSin,constraints_panel); + pCenter.add(butSin); + constraints_panel.gridx=1; + bagLayout.setConstraints(butOpposite,constraints_panel); + pCenter.add(butOpposite); + constraints_panel.gridx=2; + bagLayout.setConstraints(but0,constraints_panel); + pCenter.add(but0); + constraints_panel.gridx=3; + bagLayout.setConstraints(butPoint,constraints_panel); + pCenter.add(butPoint); + constraints_panel.gridx=4; + bagLayout.setConstraints(butAmount,constraints_panel); + pCenter.add(butAmount); + + //窗口布局 + constraints_window.gridwidth=GridBagConstraints.REMAINDER; + bagLayout.setConstraints(pNorth, constraints_window); + add(pNorth); + bagLayout.setConstraints(pCenter, constraints_window); + add(pCenter); + + //窗口基础设置 + setSize(Window_Width,Window_Height); + setLocationRelativeTo(null);//window居中显示 + setResizable(false);//不可改变窗口大小 + setDefaultCloseOperation(EXIT_ON_CLOSE); + setVisible(true); + } + + //重新绘制字体 + public void repaintFont(){ + text.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40)); + } + + //计算方法 + public double calculate(double x,String input,char option){ + double y = Double.parseDouble(input); + + text.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40)); + + switch (option){ + case'+': + return x+y; + case'-': + return (x)-(y); + case'*': + return x*y; + case'/': + return x/y; + case'%': + return x%y; + case'^': + return Math.pow(x,y); + case'l': + return Math.log(x); + case'√': + return Math.sqrt(x); + case'O': + return x*-1; + case'S': { + double value=Math.toRadians(x); + return Math.sin(value); + } + case'C': { + double value = Math.toRadians(x); + return Math.cos(value); + } + case'T': { + double value=Math.toRadians(x); + return Math.tan(value); + } + case'e': + return Math.exp(x); + case'F': + return 1/x; + case'J':{ + if(Pattern.matches("([1-9]\\d*.\\d*|0\\.\\d*[1-9]\\d*)|([1-9]\\d*)",String.valueOf(x))) + return x; + else if(Pattern.matches("-([1-9]\\d*.\\d*|0\\.\\d*[1-9]\\d*)|(-[1-9]\\d*)",String.valueOf(x))) + return x*-1; + } + default: + text.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40)); + return y; + } + } + + //历史记录弹出 + public void showDialog(){ + dialog=new JDialog(this,"历史记录",true); + dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); + dialog.setSize(750,500); + dialog.setLocationRelativeTo(null); + dialog.add(jsHistory); + dialog.setVisible(true); + } + +} +