方案一(简易版)、
代码如下
- package com.liuyanzhao;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.WindowConstants;
- class GUI2 implements ActionListener {
- JPanel panel;
- JTextField textField1;
- JComboBox<String> comboBox;
- JTextField textField3;
- JButton button;
- JTextField textField4;
- public GUI2() {
- JFrame frame = new JFrame("简易计算器");
- frame.setVisible(true);
- frame.setBounds(100,100,300,100);
- frame.setLayout(new FlowLayout());
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- panel = new JPanel();
- comboBox = new JComboBox<String>();
- comboBox.addItem("+");
- comboBox.addItem("-");
- comboBox.addItem("*");
- comboBox.addItem("/");
- comboBox.addItem("%");
- textField1 = new JTextField(3);
- textField3 = new JTextField(3);
- textField4 = new JTextField(3);
- textField4.setEnabled(false);
- button = new JButton("=");
- panel.add(textField1);
- panel.add(comboBox);
- panel.add(textField3);
- panel.add(button);
- panel.add(textField4);
- button.addActionListener(this);
- frame.add(panel);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- int x1 = Integer.parseInt(textField1.getText());
- int x2 = Integer.parseInt(textField3.getText());
- int result = 0;
- switch (comboBox.getSelectedItem().toString()) {
- case "+":
- result = x1+x2;
- break;
- case "-":
- result = x1-x2;
- break;
- case "*":
- result = x1*x2;
- break;
- case "/":
- result = x1/x2;
- break;
- case "%":
- result = x1%x2;
- break;
- default:
- break;
- }
- textField4.setText(result+"");
- }
- }
- public class Demo2 {
- public static void main(String[] args) {
- new GUI2();
- }
- }
运行结果如下
方案二(升级版)、
代码如下
- package com.liuyanzhao;
- import java.awt.Button;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.text.DecimalFormat;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.WindowConstants;
- class GUI3 implements ActionListener {
- String arr[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "+", "-", "*", "/", "=" };
- JButton button[] = new JButton[arr.length];
- JTextField textField1;
- JLabel label1;
- JTextField textField2;
- JLabel label2;
- JTextField textField3;
- JButton button_c;
- boolean isOp1=true,isOp2;
- public GUI3() {
- JFrame frame = new JFrame("整数计算器");
- frame.setLayout(new FlowLayout());
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setBounds(100, 100, 300, 200);
- JPanel panel1 = new JPanel();
- JPanel panel2 = new JPanel();
- textField1 = new JTextField(3);
- label1 = new JLabel("?");
- textField2 = new JTextField(3);
- label2 = new JLabel("=");
- textField3 = new JTextField(6);
- button_c = new JButton("C");
- button_c.addActionListener(this);
- panel1.add(textField1);
- panel1.add(label1);
- panel1.add(textField2);
- panel1.add(label2);
- panel1.add(textField3);
- panel1.add(button_c);
- panel2.setLayout(new GridLayout(3, 5, 5, 5));// 3行5列,空隙为5
- for (int i = 0; i < arr.length; i++) {
- button[i] = new JButton(arr[i]);
- button[i].addActionListener(this);
- panel2.add(button[i]);
- }
- frame.add(panel1);
- frame.add(panel2);
- frame.setVisible(true);
- }
- public static String compute(int x,int y,String op) {
- int result=0;
- DecimalFormat df = new DecimalFormat("0.0000");
- if(y==0&&op.equals("/")) {
- return "除零了";
- }
- switch (op) {
- case "+":
- result = x + y;
- break;
- case "-":
- result = x - y;
- break;
- case "*":
- result = x * y;
- break;
- case "/":
- return df.format((x+0.0) / y);//格式化小数
- }
- return result+"";
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- //判断点击的是否为数字,s1.indexOf(s2) ,字符串s2是否为s1的字符串
- if("0123456789".indexOf(e.getActionCommand())>=0) {
- if(isOp1&&textField1.getText().length()<3) {
- textField1.setText(textField1.getText()+e.getActionCommand());
- isOp2 = false;
- }
- if(!isOp1&&isOp2&&textField2.getText().length()<3) {
- textField2.setText(textField2.getText()+e.getActionCommand());
- }
- }
- if("+-*/".indexOf(e.getActionCommand())>=0) {
- label1.setText(e.getActionCommand());
- isOp2 = true;
- isOp1 = false;
- }
- if(e.getActionCommand().equals("C")) {
- textField1.setText("");
- textField2.setText("");
- textField3.setText("");
- label1.setText("?");
- isOp1 = true;
- isOp2 = false;
- }
- if(e.getActionCommand().equals("=")) {
- isOp1 = false;
- isOp2 = false;
- textField3.setText(compute(Integer.parseInt(textField1.getText()), Integer.parseInt(textField2.getText()), label1.getText()));
- }
- }
- }
- public class Demo3 {
- public static void main(String[] args) {
- new GUI3();
- }
- }
运行结果如下
本文地址:https://liuyanzhao.com/4266.html
转载请注明
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏