diff --git a/src/java2022spring/CalculatorJFrame.java b/src/java2022spring/CalculatorJFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..c5053c7e0e8ba8fdf01a2bc763172fafeffa5ca1 --- /dev/null +++ b/src/java2022spring/CalculatorJFrame.java @@ -0,0 +1,126 @@ +package java2022spring; +import java.awt.*; +import java.lang.*; +import javax.swing.*; +import java.awt.event.*; +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +public class CalculatorJFrame extends JFrame implements ActionListener{ + private JTextField Text1=new JTextField(); + private JPanel northJP=new JPanel(); + private JPanel JP=new JPanel(); + CalculatorJFrame(){ + this.Init(); + } + public void Init() { + setTitle("计算器"); + JPanel centerPanel= 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.setFont(new Font("粗体", Font.BOLD, 50)); + clear.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + Text1.setText(""); + + } + }); + JButton Pi=new JButton("PI"); + Pi.setFont(new Font("粗体",Font.BOLD,50)); + Pi.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Text1.setText("3.141593"); + } + }); + JButton zhengfu=new JButton("+/-"); + zhengfu.setFont(new Font("粗体",Font.BOLD,50)); + zhengfu.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Text1.setText("-"); + } + }); + JButton meiyong=new JButton("没用"); + + btn.addActionListener(this); + JP.add(btn); + + + + Text1.setSize(50,20); + Text1.setFont(new Font("宋体",Font.PLAIN,80)); + add(centerPanel,BorderLayout.SOUTH); + add(clear,BorderLayout.EAST); + add(Text1,BorderLayout.NORTH); + add(JP); + + setBounds(5,5,15,20); + setSize(1050,930); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + validate(); + } + } + private String firstInput = null; + private String operator = null; + + + +public void actionPerformed(ActionEvent e) { + + String click = e.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("=")) { + 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(); + } }