惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
L
LangChain Blog
A
About on SuperTechFans
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
博客园 - 司徒正美
Scott Helme
Scott Helme
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
腾讯CDC
Recorded Future
Recorded Future
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
G
GRAHAM CLULEY
Security Latest
Security Latest
S
Securelist
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
T
The Blog of Author Tim Ferriss
D
Docker
S
Security Affairs
F
Full Disclosure
Know Your Adversary
Know Your Adversary
N
News and Events Feed by Topic
N
News and Events Feed by Topic
T
Tor Project blog
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Microsoft Security Blog
Microsoft Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
Recent Announcements
Recent Announcements
博客园_首页
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security @ Cisco Blogs

博客园 - soulsjie

一种在winfrom窗体中显示计算公式的解决方案 winform窗体DataGridView合并单元格处理 C#GDI+阴影笔刷样式HatchStyle探讨 C#代码混淆工具ConfuserEx的使用 Aspose.Words在指定位置插入图片、调整图片大小 C#获取对象实体的键值对信息 C#将Winform上的TextBox和ComBox的值导入和导出 C#使用Aspose.Words将Spread表格插入到Word中 C# Aspose.Words将word中自定义的标签进行替换 Python文件操作基础方法 C# 将项目资源文件保存到磁盘上 SqlSugar数据库辅助类 使用存储过程备份MS SQLServer数据库 案例3:JAVA GUI 随机点名程序 ArcGIS JS API 添加要素图层 点击时获取图层属性 ArcGIS JS API 将天地图设置为底图 HTML5PLUS实现类似右侧弹出菜单 SqlSugar各数据库连接串 案例1:JAVAGUI用户管理
案例2:JAVA GUI 简易计算器
soulsjie · 2022-11-04 · via 博客园 - soulsjie

使用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;
	}
}