首页 > 编程语言 >案例2:JAVA GUI 简易计算器

案例2:JAVA GUI 简易计算器

时间:2022-11-04 11:38:26浏览次数:45  
标签:25 setBounds JAVA GUI add 计算器 new JLabel public

使用javaGUI实现计算器的基本功能,包含一个帮助说明页面,提示用户如何使用。包含一个计算器主界面,要实现基本的加法、减法、乘法、除法运算。

1.帮助界面

 

 

 Help.java

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;

//计算器的实现
public class Help extends JFrame {
	JButton btnOK = new JButton("我知道了,开始使用");
	
	public Help() {
		super("使用说明");
		JLabel lab1 = new JLabel("1.输入第一个操作数");
		JLabel lab2= new JLabel("2.选择一个运算符号");
		JLabel lab3 = new JLabel("3.输入第二个操作数");
		JLabel lab4 = new JLabel("4.点击【计算】按钮进行计算");
		JLabel lab5 = new JLabel("5.点击【计算】按钮进行计算");
		JLabel lab6 = new JLabel("6.点击【计算】按钮进行计算");

		lab1.setBounds(10, 10, 300, 25);
		lab2.setBounds(10, 35, 300, 25);
		lab3.setBounds(10, 60, 300, 25);
		lab4.setBounds(10, 85, 300, 25);
		lab5.setBounds(10, 110, 300, 25);
		lab6.setBounds(10, 135, 300, 25);
		
		btnOK.setBounds(30, 160, 200, 25);
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ee) {
				Calculator c = new Calculator();
				c.CenterPanel();
			}
		});
		JPanel p = new JPanel();
		p.setLayout(null);
		p.add(lab1);
		p.add(lab2);
		p.add(lab3);
		p.add(lab4);
		p.add(lab5);
		p.add(lab6);
		p.add(btnOK);

		getContentPane().add(p);
		setSize(380, 250);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		Help s = new Help();
		s.CenterPanel();
	}

	public void CenterPanel() {
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		this.setLocation(width / 2, height / 4);
	}

	//判断字符串是不是数字
	public static boolean isNumeric(String str){ 
		for (int i = str.length();--i>=0;){	
			if (!Character.isDigit(str.charAt(i))){ 
				return false; 
			} 
		} 
	return true;
	}
}

 

2.计算界面

 

 Calculator.java

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;

//计算器的实现
public class Calculator extends JFrame {
	JTextField txtNum1 = new JTextField();
	JTextField txtNum2 = new JTextField();
	JTextField txtResult = new JTextField();
	JButton btnCalcul = new JButton("计算");
	JButton btnClear = new JButton("清屏");
	JButton btnExit = new JButton("退出");
	
	public Calculator() {
		super("计算器");
		JLabel lab1 = new JLabel("请输入操作数1:");
		JLabel lab2= new JLabel("请选择运算符:");
		JLabel lab3 = new JLabel("请输入操作数2:");
		
		final JRadioButton radioButton1=new JRadioButton("加");
        final JRadioButton radioButton2=new JRadioButton("减");
        final JRadioButton radioButton3=new JRadioButton("乘");
        final JRadioButton radioButton4=new JRadioButton("除");
        ButtonGroup bg=new ButtonGroup();
        bg.add(radioButton1);
        bg.add(radioButton2);
		bg.add(radioButton3);
		bg.add(radioButton4);
		JPanel jp1=new JPanel(new GridLayout(1,4));
        jp1.add(radioButton1);
        jp1.add(radioButton2);
        jp1.add(radioButton3);
        jp1.add(radioButton4);


		JLabel lab4= new JLabel("计算结果:");
		lab1.setBounds(10, 10, 100, 25);
		lab2.setBounds(10, 40, 100, 25);
		lab3.setBounds(10, 70, 100, 25);
		lab4.setBounds(10, 100, 100, 25);

		txtNum1.setBounds(110, 10, 200, 25);
		jp1.setBounds(110, 40, 200, 25);
		txtNum2.setBounds(110, 70, 200, 25);
		txtResult.setBounds(110, 100, 200, 25);

		btnCalcul.setBounds(30, 130, 90, 25);
		btnClear.setBounds(130, 130, 90, 25);
		btnExit.setBounds(230, 130, 90, 25);
		//计算
		btnCalcul.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ee) {
				String num1=txtNum1.getText().trim();
				String num2=txtNum2.getText().trim();
				String Result="";
				if (!isNumeric(num1)){
				JOptionPane.showMessageDialog(null, "操作数1只允许为数字");
				return;
				}
				if (!isNumeric(num2))
				{
				JOptionPane.showMessageDialog(null, "操作数2只允许为数字");
				return;
				}
				if(radioButton1.isSelected()){
				Result=num1+" + "+num2+" = "+(Integer.parseInt(num1)+Integer.parseInt(num2));
				}
				if(radioButton2.isSelected()){
					Result=num1+" - "+num2+" = "+(Integer.parseInt(num1)-Integer.parseInt(num2));
				}
				if(radioButton3.isSelected()){
					Result=num1+" * "+num2+" = "+(Integer.parseInt(num1)*Integer.parseInt(num2));
				}
				if(radioButton4.isSelected()){
					Result=num1+" / "+num2+" = "+(Integer.parseInt(num1)/Integer.parseInt(num2));
				}
				txtResult.setText(Result);
			}
		});
		//清屏
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ee) {
				txtNum1.setText("");
				txtNum2.setText("");
				txtResult.setText("");
			}
		});
		//退出程序
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ee) {
				System.exit(0);
			}
		});
		JPanel p = new JPanel();
		p.setLayout(null);
		p.add(lab1);
		p.add(lab2);
		p.add(lab3);
		p.add(lab4);

		p.add(txtNum1);
		p.add(txtNum2);
		p.add(jp1);
		p.add(txtResult);

		p.add(btnCalcul);
		p.add(btnClear);
		p.add(btnExit);

		getContentPane().add(p);
		setSize(380, 250);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		Calculator s = new Calculator();
		s.CenterPanel();
	}

	public void CenterPanel() {
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		this.setLocation(width / 2, height / 4);
	}

	//判断字符串是不是数字
	public static boolean isNumeric(String str){ 
		for (int i = str.length();--i>=0;){	
			if (!Character.isDigit(str.charAt(i))){ 
				return false; 
			} 
		} 
	return true;
	}
}

  

 

标签:25,setBounds,JAVA,GUI,add,计算器,new,JLabel,public
From: https://www.cnblogs.com/soulsjie/p/16857155.html

相关文章

  • 案例3:JAVA GUI 随机点名程序
    先开发一个姓名维护的界面,输入学生的姓名,每行录入一个学生姓名,点击保存的时候将学生的姓名保存到一个txt文件中。再开发一个点名的程序,从维护好的txt文件中,随机读取一个学......
  • JavaIo案例
    FIleFIle是文件对象,可以表示一个文件,也可以表示文件夹。研究其源码,没有什么意义,我们要做的,是研究怎么用。常用的文件操作方式一:根据路径创建一个文件(只能创建在磁盘根......
  • Java学习——11.04
    因为昨天学的有点少,上不了台面,所以和今天的一起写,当然还可能是自己太懒了,昨天的没记住,于是又看了一遍。1.变量:局部变量(和C一样的)实例变量(加new引用文件名创建函数......
  • 十堰网站建设用php、java、.net哪个编程语言好
    前言对于这个问题,我的回答是:在十堰网站建设时没有什么编程语言是最好的,只有适合自己的才是最好的!或许你对我的回复不是很满意,不过没关系,你可以看看我的分析就知道了。Php......
  • 死磕面试系列,Java到底是值传递还是引用传递?
    Java到底是值传递还是引用传递?这虽然是一个老生常谈的问题,但是对于没有深入研究过这块,或者Java基础不牢的同学,还是很难回答得让人满意。可能很多同学能够很轻松的背出JVM......
  • Java核心工具库Guava介绍以及Optional和Preconditions使用进行非空和数据校验
    场景GuavaGuava项目是Google公司开源的Java核心库,它主要是包含一些在Java开发中经常使用到的功能,如数据校验、不可变集合、计数集合,集合增强操作、I/O、缓存、字......
  • JavaScript异或运算
    相关性质任何数和自己做异或运算,结果为0,即a⊕a=0a⊕a=0。任何数和0做异或运算,结果还是自己,即a⊕0=⊕a⊕0=⊕。异或运算中,满足交换律和结合律,也就是a⊕b⊕a=b⊕a⊕......
  • Java(screw)生成数据库表结构
    数据库支持MySQLMariaDBTIDBOracleSqlServerPostgreSQLCacheDB(2016)文档生成支持htmlwordmarkdown方式一:代码生成<dependency><groupId>cn......
  • java.lang.StackOverflowError错误的解决方法
    对于java.lang.StackOverflowError认识如下图所示,报出来这种错误的话,很大概率是有以下几种原因:现在来看一看我的报错界面:不难看出,这是无限循环的那种情况,所以,我就去看......
  • Java四舍五入的常见方法(DecimalFormat 用法详解)
    对Java中DecimalFormat的所有基础用法进行了一个汇总。DecimalFormat类主要靠#和0两种占位符号来指定数字长度。0表示如果位数不足则以0填充,#表示只要有可能就......