Java 使用JFrames和布局的GUI计算器

Java 使用JFrames和布局的GUI计算器,java,swing,jframe,grid-layout,Java,Swing,Jframe,Grid Layout,我目前正在开发一个计算器,它可以执行一些基本的计算,比如加法、减法、乘法和除法。为了获得最终结果,我必须遵循计算器的特定设计。计算器的设计是与这个问题一起提供的。我已经尽了最大努力去匹配计算器的官方设计,但它与之不匹配 这是计算器的官方设计。应该是这样的 这就是我运行代码时得到的结果 守则: package patel.Jainam; import java.awt.*; import java.awt.event.*; import javax.swing.*; import j

我目前正在开发一个计算器,它可以执行一些基本的计算,比如加法、减法、乘法和除法。为了获得最终结果,我必须遵循计算器的特定设计。计算器的设计是与这个问题一起提供的。我已经尽了最大努力去匹配计算器的官方设计,但它与之不匹配

这是计算器的官方设计。应该是这样的

这就是我运行代码时得到的结果

守则:

    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);


    }
    }

多谢各位

问题是您已经不再使用GridLayout

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

然后您将执行以下操作:

  • 为这两个按钮使用
    GridLayout
    创建一个面板。将此面板添加到框架的边框布局。页面开始

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

  • 使用底部按钮的
    GridLayout
    创建面板。将此面板添加到框架的边框布局。页面结束


  • 阅读Swing教程中关于的部分,以了解有关
    Borderlayout
    GridLayout
    的更多信息和工作示例。问题是您已经过度使用了GridLayout

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

    然后您将执行以下操作:

  • 为这两个按钮使用
    GridLayout
    创建一个面板。将此面板添加到框架的边框布局。页面开始

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

  • 使用底部按钮的
    GridLayout
    创建面板。将此面板添加到框架的边框布局。页面结束


  • 阅读Swing教程中关于的部分,以了解有关
    Borderlayout
    GridLayout
    的更多信息和工作示例

    以下是一份:

    导入java.awt.BorderLayout;
    导入java.awt.GridLayout;
    导入javax.swing.JButton;
    导入javax.swing.JFrame;
    导入javax.swing.JPanel;
    导入javax.swing.JScrollPane;
    导入javax.swing.JTextArea;
    导入javax.swing.SwingUtilities;
    @抑制警告(“串行”)
    公共类计算器框架扩展了JFrame{
    公共计算器框架(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(新的BorderLayout());
    getContentPane().add(createClearPanel(),BorderLayout.PAGE_START);
    getContentPane().add(createTextArea(),BorderLayout.CENTER);
    getContentPane().add(createNumberPanels(),BorderLayout.PAGE_END);
    设置大小(300300);
    包装();
    }
    私有JPanel createNumberPanels(){
    JPanel main=新的JPanel();
    main.setLayout(新的GridLayout(4,0));
    对于(int i=0;i<16;i++){
    JButton按钮=新JButton(“+i”);
    main.add(按钮);
    }
    回水总管;
    }
    私有JScrollPane createTextArea(){
    JTextArea=新的JTextArea(5,10);
    JScrollPane sp=新的JScrollPane(区域);
    sp.setVerticalScrollbar策略(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS);
    返回sp;
    }
    私有JPanel createClearPanel(){
    JButton clearAll=新JButton(“全部清除”);
    JButton clearLast=新JButton(“Clear last”);
    JPanel面板=新JPanel(新网格布局(1,0));
    面板。添加(clearLast);
    面板。添加(clearAll);
    返回面板;
    }
    公共静态void main(字符串[]args){
    调用器(()->new CalculatorFrame().setVisible(true));
    }
    }
    
    预览:


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

    camickr的答案在这里是最优的

    以下是一份:

    导入java.awt.BorderLayout;
    导入java.awt.GridLayout;
    导入javax.swing.JButton;
    导入javax.swing.JFrame;
    导入javax.swing.JPanel;
    导入javax.swing.JScrollPane;
    导入javax.swing.JTextArea;
    导入javax.swing.SwingUtilities;
    @抑制警告(“串行”)
    公共类计算器框架扩展了JFrame{
    公共计算器框架(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(新的BorderLayout());
    getContentPane().add(createClearPanel(),BorderLayout.PAGE_START);
    getContentPane().add(createTextArea(),BorderLayout.CENTER);
    getContentPane().add(createNumberPanels(),BorderLayout.PAGE_END);
    设置大小(300300);
    包装();
    }
    私有JPanel createNumberPanels(){
    JPanel main=新的JPanel();
    main.setLayout(新的GridLayout(4,0));
    对于(int i=0;i<16;i++){
    JButton按钮=新JButton(“+i”);
    main.add(按钮);
    }
    回水总管;
    }
    私有JScrollPane createTextArea(){
    JTextArea=新的JTextArea(5,10);
    JScrollPane sp=新的JScrollPane(区域);
    sp.setVerticalScrollbar策略(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS);
    返回sp;
    }
    私有JPanel createClearPanel(){
    JButton clearAll=新JButton(“全部清除”);
    JButton clearLast=新JButton(“Clear last”);
    JPanel面板=新JPanel(新网格布局(1,0));
    面板。添加(clearLast);
    面板。添加(clearAll);
    返回面板;
    }
    公共静态void main(字符串[]args){
    调用器(()->new CalculatorFrame().setVisible(true));
    }
    }
    
    预览:


    注意:忽略我只使用数字而不是像*、?、+、=等运算符的事实。

    所以我尝试了你告诉我的方法,然后出现了
    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));
        }
    }