diff --git a/src/java2022spring/CalculatorJFrame.java b/src/java2022spring/CalculatorJFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..967a123e521bbcdd1e7ea4b650e2ae27fa04fbf1 --- /dev/null +++ b/src/java2022spring/CalculatorJFrame.java @@ -0,0 +1,197 @@ +package java2022spring; +import java.awt.*; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import java.lang.*; +import java.rmi.AccessException; + +import javax.swing.*; +import java.awt.event.*; +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +public class CalculatorJFrame extends JFrame implements ActionListener { + private JTextField Text1=new JTextField(); + + private JPanel JP=new JPanel(); + CalculatorJFrame(){ + this.Init(); + } + public void Init() { + setTitle("计算器"); + JPanel centerPanel= new JPanel(); + JPanel WestJP=new JPanel(); + //设置好按钮 + Text1.setPreferredSize(new Dimension(70, 80)); + this.JP.setLayout(new GridLayout(4,4));//设置布局 + String btn_text = "1234567890+-*.=/"; + String regex = "[\\+\\-*/.=]"; + for (int i = 0; i < 16; i++) { + String temp = btn_text.substring(i, i + 1); + JButton btn = new JButton(); + btn.setText(temp); + btn.setBackground(Color.WHITE); + if (temp.matches(regex)) { + btn.setFont(new Font("粗体", Font.BOLD, 50)); + btn.setForeground(Color.RED); + btn.setBackground(Color.white); + } + + JButton clear=new JButton("清除"); + clear.setBounds(10, 20, 10, 10); + clear.setFont(new Font("粗体", Font.BOLD, 20)); + clear.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + Text1.setText(""); + + } + }); + + JButton Pi=new JButton("PI"); + Pi.setFont(new Font("粗体",Font.BOLD,20)); + Pi.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Text1.setText("3.141593"); + } + }); + JButton zhengfu=new JButton("+/-"); + zhengfu.setFont(new Font("粗体",Font.BOLD,20)); + zhengfu.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Text1.setText("-"); + } + }); + + + JButton genhao=new JButton("sqrt"); + genhao.setFont(new Font("粗体",Font.BOLD,20)); + genhao.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + double i=Double.parseDouble(Text1.getText()); + double c=Math.sqrt(i); + String iu=String.valueOf(c); + Text1.setText(iu); + } + }); + + JButton daoshu=new JButton("1/x"); + daoshu.setFont(new Font("粗体",Font.BOLD,20)); + daoshu.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + double z=Double .parseDouble(Text1.getText()); + double h=1/z; + String o=String.valueOf(h); + Text1.setText(o); + } + }); + btn.addActionListener(this); + + + JP.add(btn); + + + Text1.setSize(20,20); + Text1.setFont(new Font("宋体",Font.PLAIN,80)); + add(centerPanel,BorderLayout.SOUTH); + add(daoshu,BorderLayout.WEST); + add(clear,BorderLayout.SOUTH); + add(genhao,BorderLayout.EAST); + + + add(Text1,BorderLayout.NORTH); + add(JP,BorderLayout.CENTER); + + + setBounds(10,20,15,20); + setSize(550,530); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + validate(); + daoshu.addActionListener(new ActionListener(){ + + public void actionPerformed(ActionEvent e) { + int y=Text1.getText().length(); + if(y==0) { + JOptionPane.showMessageDialog(null, "你没有输入数字"); + } + + } + });//网上看的错误处理解决弹出窗口的方法 + genhao.addActionListener(new ActionListener(){ + + public void actionPerformed(ActionEvent e) { + int y=Text1.getText().length(); + if(y==0) { + JOptionPane.showMessageDialog(null, "你没有输入数字"); + } + + } + });//网上看的错误处理解决弹出窗口的方法 + clear.addActionListener(new ActionListener(){ + + public void actionPerformed(ActionEvent e) { + int y=Text1.getText().length(); + if(y==0) { + JOptionPane.showMessageDialog(null, "你没有输入数字"); + } + + } + });//网上看的错误处理解决弹出窗口的方法 + } + + + } + + + private String firstInput = null; + private String operator = null; + + + + +public void actionPerformed(ActionEvent m) { + + String click = m.getActionCommand(); + if (".0123456789".indexOf(click) != -1) { + this.Text1.setText(Text1.getText() + click); + this.Text1.setHorizontalAlignment(JTextField.RIGHT); + + } else if (click.matches("[\\+\\-*/]{1}")) { + + operator = click; + firstInput = this.Text1.getText(); + + this.Text1.setText(""); + } else if (click.equals("=")) { + if(Text1.getText().length()==0) {Text1.setText("你没有输入数字");} + Double a = Double.valueOf(firstInput); + Double b = Double.valueOf(this.Text1.getText()); + Double result = null; + switch (operator){ + case "+": + result = a + b; + break; + case "-": + result = a - b; + break; + case "*": + result = a * b; + break; + case "/": + if(b != 0){ + result = a / b; + } + break; + } + this.Text1.setText(result.toString()); + } + + + +} +} + diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..a2add6082f4f0b7562a9dd7b903aabeb92823a59 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -2,6 +2,7 @@ package java2022spring; public class Test { public static void main(String[] args) { - System.out.println("Hello world!"); + new CalculatorJFrame(); + } }