java计算器页面设计

今天学习了Java的GUI图形界面,并实现了如下图所示的复杂计算器界面的设计,在设计算器界面的整个过程中遇到了不少小问题,值得注意和总结。
由于计算器界面较复杂,按键较多,因此需要用到面板的嵌套,但是面板为二级容器,不能单独出现,必须依赖于窗口。
在这里插入图片描述

其中代码如下:

public class CalTest extends JFrame {
    
    public CalTest(){
        this.setSize(820,680);
        this.setTitle("计算器");
        this.setLayout(null);
        initPanel1();
        initPanel2();
        initPanel3();
        initPanel5();
        initPrintText();
        initHelp();
        this.setVisible(true);
    }
//面板1,25个按键
    private void initPanel1() {
        JPanel Panel1 = new JPanel();
        Panel1.setSize(350, 350);
        Panel1.setLocation(50, 230);
        Panel1.setLayout(new GridLayout(5,5,3,3));
        Panel1.setBackground(Color.WHITE);
        JButton[] btsl = new JButton[25];
        String[] strs = {" ", "Inv", "In", "(", ")", "Int", "sinh", "sin", "x^2", "n!", "dms", "cosh", "cos", "x^y", "y|x", "3.14", "tanh", "tan", "x^3", "3|x", "F-E","Exp","Mod","log","10^x"};
        for (int i = 0; i < strs.length; i++) {
            btsl[i] = new JButton(strs[i]);
            Panel1.add(btsl[i]);
        }
        btsl[0].setEnabled(false);
        this.add(Panel1);
    }
//面板2,20个按键
    private void initPanel2(){
        JPanel panel2 = new JPanel();
        panel2.setSize(350,280);
        panel2.setLocation(402,160);
        panel2.setLayout(new GridLayout(4,5,3,3));
        panel2.setBackgroun/d(Color.WHITE);
        JButton[] btsl2 = new JButton[20];
        String[] strs2 = {"MC","MR","M5","M+","M-","<——","CE","C","+-","根号","7","8","9","/","%","4","5","6","*","1/x"};
        for (int i = 0; i < strs2.length ; i++) {
            btsl2[i] = new JButton(strs2[i]);
            panel2.add(btsl2[i]);
        }
        this.add(panel2);
    }
    //对特殊位置的按键进行处理
    private void initPanel3(){
        initPanel4();

        JButton button1 = new JButton("1");
        button1.setSize(67,67);
        button1.setLocation(402,440);
        this.add(button1);

        JButton button2 = new JButton("2");
        button2.setSize(67,67);
        button2.setLocation(472,440);
        this.add(button2);

        JButton button3 = new JButton("0");
        button3.setSize(137,67);
        button3.setLocation(402,510);
        this.add(button3);

        JButton button4 = new JButton("=");
        button4.setSize(67,137);
        button4.setLocation(684,441);
        this.add(button4);
    }
    //面板4,4个按键
    private void initPanel4(){
        JPanel Panel4 = new JPanel();
        Panel4.setSize(140,140);
        Panel4.setLocation(542,440);
        Panel4.setLayout(new GridLayout(2,2,3,3));
        Panel4.setBackground(Color.WHITE);
        JButton[] btsl4 = new JButton[4];
        String[] strs4 = {"3","——",".","+"};
        for (int i = 0; i < strs4.length ; i++) {
            btsl4[i] = new JButton(strs4[i]);
            Panel4.add(btsl4[i]);
        }
        this.add(Panel4);
    }
//面板5,3个按键
    private void initPanel5(){
        JPanel panel5 = new JPanel();
        panel5.setSize(347,67);
        panel5.setLocation(50,160);
        panel5.setLayout(new GridLayout(1,3,10,10));
        JButton[] btls5 = new JButton[3];
        String[] strs = {"度","弧度","梯度"};
        for (int i = 0; i < strs.length ; i++) {
            btls5[i] = new JButton(strs[i]);
            panel5.add(btls5[i]);
        }
        this.add(panel5);
    }
//输出框
    private void initPrintText(){
        JTextField printText = new JTextField();
        printText.setSize(702,100);
        printText.setLocation(50,50);
        this.add(printText);
    }
//顶部的帮助按键
    private void initHelp(){
        JPanel panel6 = new JPanel();
        panel6.setSize(400,40);
        panel6.setLocation(50,0);
        panel6.setLayout(new GridLayout(1,3,10,10));
        JButton[] btls5 = new JButton[3];
        String[] strs = {"查看(v)","编辑(E)","帮助(H)"};
        for (int i = 0; i < strs.length ; i++) {
            btls5[i] = new JButton(strs[i]);
            panel6.add(btls5[i]);
        }
        this.add(panel6);
    }
}

在主方法中调用就可以运行成功
在编写代码中遇到了一些小问题需要注意
1.布局时坐标是按照此控件左上角到窗口的距离来确定x,y坐标的。
2.大窗口的尺寸要足够大,控件的尺寸大小和坐标中和不能超过大窗口的尺寸,否则运行时窗口不能显示完全。
3.在使用对象设置相应参数时要注意不能用this来代替当前对象,因为此时this指向调用当前方法的对象,不能搞混淆。
例如: panel6.setSize(400,40);就不能写成 this.setSize(400,40);
4.按钮一定要add到相应面板上,面板要add到窗口上
5.设置可见:this.setVisible(true);否则窗口不会显示相应控件

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目:简易计算器设计 一、设计概要 1、设计内容 计算器设计使用图形用户界面实现,能够进行简单的加、减、乘、除四则计算 ; 参与运算的数字通过点击按钮输入。 2、程序流程图 详细设计 import java.awt.BorderLayout; //导入AWT页面设置类 import java.awt.Button; //导入AWT按钮类 import java.awt.Frame; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; //导入AWT鼠标监视器 import java.awt.event.WindowEvent; import java.awt.event.WindowListener;//导入AWT窗口监视器 //创建计算器的主窗口,该类含有main函数 public class Calculator { public static void main(String[] args) { //创建Frame对象 Frame f = new Frame("Calculator"); //创建文本框,用于接收数字和操作 final TextField tf = new TextField(); //声明一个面板用于存放按钮 Panel p = new Panel(); f.setLayout(new BorderLayout()); //将文本框放在北部 f.add(tf, BorderLayout.NORTH); //将按钮放于中部 f.add(p, BorderLayout.CENTER); //设置布局模式 p.setLayout(new GridLayout(4, 4)); //创建16个按钮 Button one = new Button("1"); Button two = new Button("2"); Button three = new Button("3"); Button four = new Button("4"); Button five = new Button("5"); Button six = new Button("6"); Button seven = new Button("7"); Button eight = new Button("8"); Button nine = new Button("9"); Button zero = new Button("0"); Button add = new Button("+"); Button sub = new Button("-"); Button mul = new Button("*"); Button div = new Button("/"); Button decimal = new Button("."); final Button equal = new Button("="); //将按钮添加到P p.add(one); p.add(two); p.add(three); p.add(four); p.add(five); p.add(six); p.add(seven); p.add(eight); p.add(nine); p.add(zero); p.add(add); p.add(sub); p.add(mul); p.add(div); p.add(decimal); p.add(equal); final String[] ary = {"", null, ""}; //该类为主类创建数字按钮对象 class NumMouseListener implements MouseListener{ public void mouseClicked(MouseEvent e) { String num = ((Button)e.getSource()).getLabel().trim(); if(ary[1] == null) { ary[0] = ary[0] + "" + num; } else{ ary[2] = ary[2] + "" + num; } tf.setText(tf.getText() + "" + num); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(Mous
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值