java作业:用GUI实现一个科学计算器的设计

问题描述
在这里插入图片描述

代码实现

import javax.swing.*;
import java.awt.*;

public class MyCalc extends JFrame
{

	private void initnumPanel1()
	{
		JPanel numpanel1 = new JPanel();//数字区域面板1
		JPanel numpanel2 = new JPanel();//数字区域面板2
		JPanel numpanel3 = new JPanel();//数字区域面板3
		
		//初始化面板一
		numpanel1.setBounds(400, 150, 290, 260);
		numpanel1.setLayout(new GridLayout(5,4,2,2));
		
		String[] titles1 = {"MC","MR","MS","M+","<-","CE","C","+-","7","8","9","/","4","5","6","*","1","2","3","-"};
		JButton[] buttons1 = new JButton[20];
		
		for(int i = 0;i<titles1.length ;i++)
		{
			buttons1[i] = new JButton(titles1[i]);
			buttons1[i].setBackground(Color.lightGray);
			
			numpanel1.add(buttons1[i]);
		}
		
		//初始化面板二
		numpanel2.setBounds(691, 150, 80, 206);
		numpanel2.setLayout(new GridLayout(4,1,2,2));
		
		String[] titles2={"M-","√","%","1/x"};
		JButton[] buttons2=new JButton[4];
		
		for(int i = 0;i<titles2.length ;i++)
		{
			buttons2[i] = new JButton(titles2[i]);
			buttons2[i].setBackground(Color.lightGray);
			
			numpanel2.add(buttons2[i]);
		}
		//初始化面板三
		
		numpanel3.setBounds(400,410,290,52);
		numpanel3.setLayout(new GridLayout(1,2));
		
		JButton buttons3=new JButton("0");
		buttons3.setBackground(Color.lightGray);
		
		JPanel numpanel4 = new JPanel();
		numpanel4.setLayout(new GridLayout(1,2));
		
		JButton[] buttons4 = new JButton[2];
		String[] titles3={".","+"};
		
		for(int i = 0; i<titles3.length;i++)
		{
			buttons4[i] = new JButton(titles3[i]);
			buttons4[i].setBackground(Color.lightGray);
			
			numpanel4.add(buttons4[i]);
		}
		
		numpanel3.add(buttons3);
		numpanel3.add(numpanel4);
		
		//将最特殊的等号按钮加进屏幕
		
		JButton equ = new JButton("+");
		
		equ.setBounds(691, 357, 80, 106);
		equ.setBackground(Color.lightGray);
		
		this.add(equ);
		this.add(numpanel1);
		this.add(numpanel2);
		this.add(numpanel3);
	}
	
	private void initalgPanel2()
	{
		//左边下方运算符按钮部分
		JPanel algpanel1 = new JPanel();
		
		algpanel1.setBounds(20, 203, 363, 260);
		algpanel1.setLayout(new GridLayout(5,5,2,2));
		
		String[] titles4 = {"","Inv","ln","(",")","Int","sinh","sin","x^2","n!","dms","cosh","cos","x^n","n√x","π","tanh","tan","x^3","3√x","F-E","Exp","Mod","log","10^n"};
		JButton[] buttons5 = new JButton[25];
		
		for(int i = 0;i<titles4.length;i++)
		{
			buttons5[i] = new JButton(titles4[i]);
			buttons5[i].setBackground(Color.lightGray);
			algpanel1.add(buttons5[i]);
		}
		//右边上方单选按钮选项集控件
		JPanel algpanel2 = new JPanel();
		
		algpanel2.setBounds(20, 160, 363, 52);
		algpanel2.setLayout(new GridLayout(1,3));
		
		JRadioButton angel = new JRadioButton("度");
		JRadioButton radian = new JRadioButton("弧度");
		JRadioButton grad = new JRadioButton("梯度");
		
		algpanel2.add(angel);
		algpanel2.add(radian);
		algpanel2.add(grad);
		
		this.add(algpanel2);
		this.add(algpanel1);
	}
	
	private void inittextPanel3()
	{
		TextField text=new TextField();
		
		text.setLocation(20, 10);
		text.setSize(750,135);
		
		this.add(text);
	}
	
	public MyCalc()
	{
		setSize(800,550);
		setTitle("科学计算器");
		setLayout(null);
		
		initnumPanel1();
		initalgPanel2();
		inittextPanel3();
		
		setVisible(true);
	}
	public static void main(String[] args)
	{
		MyCalc calc = new MyCalc();
	}
}


结果展示

在这里插入图片描述

过程中遇到的问题
1.在设置panel的位置时,可以选择三种不同的函数
setLocation();
setSize();
setBounds();
通过查看源码可以得知,setSzie()、setLocation()这两个函数在内部还是调用了setBound()函数,所以在所有面板定位时我都选择了setBound()函数来设置。
在这里插入图片描述2.面板使用GridLayout时可以进行嵌套使用。

3.在输入字符不合法时会出现:
Save could not be completed.
在这里插入图片描述
《解决办法请点击🗡》

  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值