java组件写一个GUI 计算器

1.设计GUI

整体的页面布局可设为空布局,上下各一个面板,其中上面面板可放入文本输入域(jtf.setHorizontalAlignment(JTextField.RIGHT);)这句代码是将文本件右边往左边输出,下面则是放入button,界面具体实现

java组件写一个GUI 计算器_第1张图片

2.具体代码实现

package Simple_Calc;

import java.awt.BasicStroke;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class Calc extends JFrame{
	private void initFrame() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setSize(300, 400);                          //窗口大小
		this.setTitle("简单计算器1.0");                  //窗口的名称
		this.setLocationRelativeTo(null);               //窗口在屏幕的位置 默认中央
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);    //设置单击窗口右上角后,程序的处理
		this.setLayout(null);                         //布局格式 默认
		JTextField jtf = this.topSurfacee();      //调用方法
		this.bottomSurface(jtf);                       
		this.setVisible(true);                   //设置窗口是否可见  !放在前面窗口无内容
		
	}
	private JTextField topSurfacee(){                
		JPanel topPanel = new JPanel();         //建立面板
		topPanel.setBounds(-5,10,300,50);       //窗口的在屏幕的位置
		JTextField jtf = new JTextField(15);    //显示结果文本框长度15个字
		jtf.setHorizontalAlignment(JTextField.RIGHT);// 设置文件右边往左边输出
		jtf.setForeground(Color.red);     //设置组件的前景色
		jtf.setFont(new Font("SansSerif",Font.PLAIN,20)); //字体样式 字体格式 字体大小
		jtf.setEditable(false);       //设置选项不可用
		jtf.setText("0");             //文本框的文字设置为“确定”
		topPanel.add(jtf);            //加入组件jtf
		this.add(topPanel);           //加入组件topPanel
		return jtf;
	}
	private void bottomSurface(JTextField jtf){        
		JPanel bottomPanel = new JPanel();             //建立面板
		bottomPanel.setBounds(8,80,270,250);           //面板的大小
		bottomPanel.setLayout(new GridLayout(5,4,5,4)); //布局格式   5行4列    行距5 列距4
		
		String[] buttonValue = {"1","2","3","C",        //操作对应的字符串数组
								"4","5","6","CE",
								"7","8","9","←",
								"+","0","-",".",
								"*","/","%","="};
		
		CalcListener cl = new CalcListener(jtf);                // 监听器的创建 
		for (int i = 0; i < buttonValue.length; i++) {            //遍历操作字符
			JButton button = new JButton(buttonValue[i]);         //建立有文字的按钮
			button.setBorder(BorderFactory.createRaisedBevelBorder());
			button.setFont(new Font("楷体", 0, 25));              //字体样式 字体格式 字体大小
			button.setForeground(Color.black);                     //设置组件的前景色
			bottomPanel.add(button);                               //添加组件
			button.addActionListener(cl);                          
		}
		
		this.add(bottomPanel);                                    //添加组件
	}
	public static void main(String[] args) {
		Calc calc = new Calc();
		calc.initFrame();
	}
}
package Simple_Calc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

//处理逻辑运算
public class CalcListener implements ActionListener {
	private JTextField jtf;

	String content = "";                   
	
	StringBuffer sb = new StringBuffer();            	// 创建字符缓冲区域

	public CalcListener(JTextField jtf) {            //构造方法
		this.jtf = jtf;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();  // 返回与此动作相关的命令字符串
		switch (command) {
		// case同时写多个条件,数字的基本处理
		case "1":
		case "2":
		case "3":
		case "4":
		case "5":
		case "6":
		case "7":
		case "8":
		case "9":
		case "0":
		case ".":
		case "+":
		case "-":
		case "*":
		case "/":
		case "%":
			sb.append(command);         // command添加到sb中
			jtf.setText(sb.toString().trim());   //在文本框添加字符串,并且去掉首尾空格
			break;

		// 清空输入框中内容
		case "C":
		case "CE":
			// 清空StringBuffer
			sb.setLength(0);
			jtf.setText("0");
			break;

		// 退格
		case "←":              //只是个修饰  实际上无用  
			break;

		// 四则运算处理 
		case "=":
			String content = jtf.getText();      //得到字符串
			if (content.contains("+")) {         //字符串中包含 + 
				String[] string = content.split("\\+"); //分隔字符串
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)+Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			} else if (content.contains("-")) {
				String[] string = content.split("-");
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)-Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			} else if (content.contains("*")) {
				String[] string = content.split("\\*");
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)*Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			} else if (content.contains("/")) {
				String[] string = content.split("\\/");
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)/Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			} else if (content.contains("%")) {
				String[] string = content.split("\\%");
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)%Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			}
			break;
		}
	}
}

参考资料:

 利用java的swing组件制作简易计算器

https://blog.csdn.net/gp3056/article/details/70226655

Java Swing中的文本框(JTextField)与文本区(JTextArea)使用实例_java

https://yq.aliyun.com/ziliao/149164

文本左右对齐属性设置方法:setHorizontalAlignment

https://bbs.csdn.net/topics/239844

setfont()函数

https://www.cnblogs.com/tk55/p/6627517.html

java中setEditable(false)

https://zhidao.baidu.com/question/391917093.html

java中xxx.setText("")

https://zhidao.baidu.com/question/296096558.html

JAVA:事件监听器之Button类中的addActionListener(ActionListener l)方法

https://www.cnblogs.com/KeenLeung/archive/2012/05/27/2520636.html

JButton的使用:

https://blog.csdn.net/kewbblog/article/details/8627004

Swing系列之JTextField(单行文本框)

https://segmentfault.com/a/1190000008994524

java中actionPerformed

https://zhidao.baidu.com/question/76927739.html

StringBuffer之append方法

https://www.cnblogs.com/jianmang/articles/4906515.html

java中string.trim()函数的使用

https://blog.csdn.net/etjnety/article/details/7842618

String.split()用法的一点经验

https://blog.csdn.net/leeharry/article/details/2281325

【swing】UIManager样式的处理(参数详解)

https://blog.csdn.net/yjqyyjw/article/details/52301963

你可能感兴趣的:(Java初学,工具使用)