java初学者编译简单的计算机
Posted 星然
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java初学者编译简单的计算机相关的知识,希望对你有一定的参考价值。
package com.yj.test; import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class CaculateFrame extends JFrame{ private JPanel content,panel; private JTextField txtInput; //记录运算符操作 private String operation; private double v1,v2,result; // 一次计算的开关 private boolean flag = false; public CaculateFrame() { this.setTitle("计算器"); //设置位置和大小 this.setBounds(300, 200, 300, 300); txtInput = new JTextField(20); txtInput.setHorizontalAlignment(JTextField.RIGHT); Font f = new Font("微软雅黑",Font.BOLD,20); txtInput.setFont(f); content = new JPanel(); content.setLayout(new BorderLayout()); content.add(txtInput,BorderLayout.NORTH); panel = new JPanel(); panel.setLayout(new GridLayout(4,4,5,5)); panel.add(useButton("7")); panel.add(useButton("8")); panel.add(useButton("9")); panel.add(useButton("/")); panel.add(useButton("4")); panel.add(useButton("5")); panel.add(useButton("6")); panel.add(useButton("*")); panel.add(useButton("1")); panel.add(useButton("2")); panel.add(useButton("3")); panel.add(useButton("-")); panel.add(useButton("0")); panel.add(useButton(".")); panel.add(useButton("=")); panel.add(useButton("+")); content.add(panel,BorderLayout.CENTER); this.add(content); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private JButton useButton(String key) { JButton btn = new JButton(key); btn.addActionListener(new ActionListener() { //事件是由哪个控件发出的,通通都记录在ActionEvent这个对象中 public void actionPerformed(ActionEvent e) { //根据按钮上面不同的值,应该做不同的操作 myAction(key); } }); return btn; } private void myAction(String key) { if(flag) { txtInput.setText(""); flag = false; } switch(key) { case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": case ".": if(txtInput.getText() == null || txtInput.getText().equals("")) { if(key.equals("0")) { return; } if(key.equals(".")) { txtInput.setText("0."); return; } }if(txtInput.getText().indexOf(".") != -1 && key.equals(".")) { return; } txtInput.setText(txtInput.getText() + key); break; case "+": this.operation = "+"; if(txtInput.getText() == null || txtInput.getText().equals("")) { v1 = 0; }else { //转换为double类型 v1 = Double.parseDouble(txtInput.getText()); } txtInput.setText(""); break; case "-": this.operation = "-"; if(txtInput.getText() == null || txtInput.getText().equals("")) { v1 = 0; }else { //转换为double类型 v1 = Double.parseDouble(txtInput.getText()); } txtInput.setText(""); break; case "*": this.operation = "*"; if(txtInput.getText() == null || txtInput.getText().equals("")) { v1 = 0; }else { //转换为double类型 v1 = Double.parseDouble(txtInput.getText()); } txtInput.setText(""); break; case "/": this.operation = "/"; if(txtInput.getText() == null || txtInput.getText().equals("")) { v1 = 0; }else { //转换为double类型 v1 = Double.parseDouble(txtInput.getText()); } txtInput.setText(""); break; case "=": if(txtInput.getText() == null || txtInput.getText().equals("")) { v2 = 0; }else { //转换为double类型 v2 = Double.parseDouble(txtInput.getText()); } switch(this.operation) { case "+": result = v1 + v2; break; case "-": result = v1 - v2; break; case "*": result = v1 * v2; break; case "/": if (v2 == 0) { return; } result = v1 / v2; break; } txtInput.setText(String.valueOf(result)); v1 = 0; v2 = 0; result = 0; flag = true; } } }
计算机测试显示窗口 :
package com.yj.test; public class Test { public static void main(String[] args) { CaculateFrame cf = new CaculateFrame(); cf.setVisible(true); } }
以上是关于java初学者编译简单的计算机的主要内容,如果未能解决你的问题,请参考以下文章
偶是一名java初学者,请问一下,如何理解静态方法和动态方法,并且如何使用?
Java 初学 第一弹--编译并运行书上的简单程序(猜数字小游戏)
错误记录Android Studio 编译报错 ( Could not determine java version from ‘11.0.8‘. | Android Studio 降级 )(代码片段
错误记录Android Studio 编译报错 ( Could not determine java version from ‘11.0.8‘. | Android Studio 降级 )(代码片段