使用JFrame和布局的GUI计算器

时间:2018-11-10 19:19:33

标签: java swing jframe grid-layout

我目前正在使用一个计算器,该计算器应该执行基本的计算,例如加法,减法,乘法和除法。为了获得最终结果,我必须对计算器进行某种设计。这个问题提供了计算器的设计。我尽了最大的努力去匹配计算器的官方设计,但是不匹配。

这是计算器的官方设计。这就是它的外观。

enter image description here

这是我运行代码时得到的。

enter image description here

代码:

    package patel.Jainam;

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

public class CalculatorFrame extends JFrame {

    /**
     * All the buttons that will be used in the calculator have been initialized 
     */
    private JButton button1;
    private JButton button2; 
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6; 
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JButton button0; 

    private JButton buttonEqual;
    private JButton buttonDot;

    private JButton buttonClearLast;
    private JButton buttonClearAll;

    private JButton buttonAdd;
    private JButton buttonSub;
    private JButton buttonMul;
    private JButton buttonDiv;

    private JTextArea textArea; 


    public CalculatorFrame(){

    JPanel panel2 = new JPanel();       
    panel2.setLayout(new GridLayout(1,1));
    panel2.add(buttonClearLast = new JButton ("Clear Last"));
    panel2.add(buttonClearAll = new JButton ("Clear All"));
    add(panel2, BorderLayout.PAGE_START);

    JPanel panel3 = new JPanel();
    textArea = new JTextArea(2,10);
//  textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    panel3.add(scrollPane);
    add(panel3, BorderLayout.LINE_START);



    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridLayout(4,4));      

    panel1.add(button7 = new JButton ("7"));
    panel1.add(button8 = new JButton ("8"));
    panel1.add(button9 = new JButton ("9"));
    panel1.add(buttonAdd = new JButton ("+"));
    panel1.add(button4 = new JButton ("4"));
    panel1.add(button5 = new JButton ("5"));
    panel1.add(button6 = new JButton ("6"));
    panel1.add(buttonSub = new JButton ("-"));
    panel1.add(button1 = new JButton ("1"));
    panel1.add(button2 = new JButton ("2"));
    panel1.add(button3 = new JButton ("3"));
    panel1.add(buttonMul = new JButton ("*"));
    panel1.add(button0 = new JButton ("0"));
    panel1.add(buttonDot = new JButton ("."));
    panel1.add(buttonEqual = new JButton ("="));
    panel1.add(buttonDiv = new JButton ("/"));

    add(panel1, BorderLayout.PAGE_END);


    }
    }

谢谢。

2 个答案:

答案 0 :(得分:2)

问题是您已经结束使用GridLayout。

我建议您继续使用框架的默认布局管理器BorderLayout

然后您将执行以下操作:

  1. 使用GridLayout创建两个按钮的面板。将此面板添加到框架的BorderLayout.PAGE_START

  2. 将包含文本区域的滚动窗格添加到框架的BorderLayout.CENTER

  3. 使用GridLayout为底部的按钮创建一个面板。将此面板添加到框架的BorderLayout.PAGE_END

有关BorderlayoutGridLayout的更多信息和工作示例,请阅读Swing教程上的Using Layout Managers部分。

答案 1 :(得分:0)

camickr的答案在这里是最佳的。

这里是SSCCE

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class CalculatorFrame extends JFrame {

    public CalculatorFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(createClearPanel(), BorderLayout.PAGE_START);
        getContentPane().add(createTextArea(), BorderLayout.CENTER);
        getContentPane().add(createNumberPanels(), BorderLayout.PAGE_END);
        setSize(300, 300);
        pack();
    }

    private JPanel createNumberPanels() {
        JPanel main = new JPanel();
        main.setLayout(new GridLayout(4, 0));
        for (int i = 0; i < 16; i++) {
            JButton button = new JButton("" + i);
            main.add(button);
        }
        return main;
    }

    private JScrollPane createTextArea() {
        JTextArea area = new JTextArea(5, 10);
        JScrollPane sp = new JScrollPane(area);
        sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        return sp;
    }

    private JPanel createClearPanel() {
        JButton clearAll = new JButton("clear all");
        JButton clearLast = new JButton("Clear last");
        JPanel panel = new JPanel(new GridLayout(1, 0));
        panel.add(clearLast);
        panel.add(clearAll);
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new CalculatorFrame().setVisible(true));
    }
}

预览:

Preview

P.S:忽略我只使用数字而不是*,?,+,=等运算符的事实。