教你用java编写计算器

Posted 王囧草

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了教你用java编写计算器相关的知识,希望对你有一定的参考价值。

教你用java编写计算器(三)

import java.awt.Color;

import java.awt.Dimension;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

 

public class Myclass  extends JFrame {

private JPanel jp1,jp2;

private JButton jb;

private JTextField display1, display2, display3;

private JMenuBar jmb;

private JMenu jm;

private JMenuItem jmi;

private final String[] firstmenu = { "查看(V)", "编辑(E)", "帮助(H)" };

private final String[][] secondMenu = {

{ "标准型", "科学型", "程序员", "统计信息", "历史信息", "数字分组", "基本", "单位转换",

"日期计算", "工作表" }, { "复制(C)", "粘贴(P)", "历史记录" },

{ "查看帮助(V)", "关于计算器(A)" } };

private final char[] Mnemonic = { ‘V‘, ‘E‘, ‘H‘ };

private String buttonsName[]={"MC","MR","MS","M+","M-",

"","CE","C","±","",

"7","8","9","/","%",

"4","5","6","*","1/x",

"1","2","3","-","=",

"0",".","+"};

 

public Myclass(){

for (int i = 0; i < buttonsName.length; i++) {

jb = new JButton(buttonsName[i]);

if (i == 24) {

jb.setBounds(i % 5 *(35 +7),65 + i /5 * (29 + 7), 35, 65);

} else if (i ==25) {

jb.setBounds(i % 5 *(35 +7),65 + i /5 * (29 + 7), 77, 29);

} else if (i > 25) {

jb.setBounds(i % 5 *(35 +7)+5+42,65 + i /5 * (29 + 7), 35,29);

} else {

                jb.setBounds(i % 5 *(35 +7),65 + i /5 * (29 + 7), 35, 29);//测量长宽和间距

}

 jb.setMarigin(new java.awt.Insets(0,0,0,0) );

thsi.add(jb);

{

             

}{

}

}

this.setTitle("计算器");display2 = new JTextField("0");

display2.setBounds(12, 12, 190, 50);

display2.setFont(new Font("宋体", Font.BOLD, 26));

display2.setHorizontalAlignment(JLabel.RIGHT);

display2.setEnabled(false);

display2.setDisabledTextColor(Color.BLACK);

display2.setBackground(new Color(247, 250, 254));

this.add(display2);

this.setLayout(null);

this.setTitle("计算器");

this.setLocation(500, 200);

this.setSize(230, 320);

this.setBackground(new Color(217, 228, 241));

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

 

 

 

 

    public static void main(String[] args) {

new Myclass();

 

 

    }

}

 

以上是关于教你用java编写计算器的主要内容,如果未能解决你的问题,请参考以下文章

90行代码写一个游戏?教你用90行HasKell代码实现2048游戏

教你用Python提取Chrome浏览器保存的密码

手把手教你用Java实现一套简单的鉴权服务(SpringBoot,SSM)(万字长文)

手把手教你用Java实现一套简单的鉴权服务(SpringBoot,SSM)(未完待续...)

今天大佬教你用Python3-OpenCV实现实时摄像头人脸检测

神级程序员教你用Python实现简单的导弹自动追踪!此乃装逼神技!