关于简单计算器中的布局

发布于 2024-12-10 22:53:50 字数 2175 浏览 5 评论 0原文

您好,我正在尝试自己制作一个具有编码大小、布局等的计算器(尝试不使用 NetBeans,这不是作业)。但我面临着关于空位的问题。我有一个文本区域和按钮,但正如你在下面看到的,我无法处理这个空间问题。这是我的代码,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;

public class calculator extends JFrame {

    public calculator(){

        initComponents();

    }

    private void initComponents(){

        JPanel panelScreen = new JPanel(new GridLayout(0,1));

        JTextArea screen = new JTextArea();
        panelScreen.add(screen);

        JFrame frame = new JFrame("CALCULATOR");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel panelButtons = new JPanel(new GridLayout(3,3));

        JButton oneButton = new JButton("1");
        panelButtons.add(oneButton);

        JButton twoButton = new JButton("2");
        panelButtons.add(twoButton);

        JButton threeButton = new JButton("3");
        panelButtons.add(threeButton);

        JButton fourButton = new JButton("4");
        panelButtons.add(fourButton);

        JButton fiveButton = new JButton("5");
        panelButtons.add(fiveButton);

        JButton sixButton = new JButton("6");
        panelButtons.add(sixButton);

        JButton sevenButton = new JButton("7");
        panelButtons.add(sevenButton);

        JButton eightButton = new JButton("8");
        panelButtons.add(eightButton);

        JButton nineButton = new JButton("9");
        panelButtons.add(nineButton);

        frame.getContentPane().add(panelScreen, BorderLayout.NORTH);
        //frame.getContentPane().add(new JSeparator(), BorderLayout.CENTER);
        frame.getContentPane().add(panelButtons, BorderLayout.SOUTH);
        frame.setBounds(50, 50, 500, 500);
        frame.setResizable(false);
        //frame.pack();
        frame.setVisible(true);


    }


    public static void main(String[] args) {

        new calculator();

    }

}

这是程序的图片;

在此处输入图像描述

如果您能帮助我,我将不胜感激。不管怎样谢谢:)

hi there i am trying to make a calculator with coding sizes,layouts etc. by myself (trying to not use NetBeans and it is not a homework). but i am facing with a problem about empty spaces. i have a TextArea and Buttons but as you can see below i cant handle this space problem. here is my code,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;

public class calculator extends JFrame {

    public calculator(){

        initComponents();

    }

    private void initComponents(){

        JPanel panelScreen = new JPanel(new GridLayout(0,1));

        JTextArea screen = new JTextArea();
        panelScreen.add(screen);

        JFrame frame = new JFrame("CALCULATOR");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel panelButtons = new JPanel(new GridLayout(3,3));

        JButton oneButton = new JButton("1");
        panelButtons.add(oneButton);

        JButton twoButton = new JButton("2");
        panelButtons.add(twoButton);

        JButton threeButton = new JButton("3");
        panelButtons.add(threeButton);

        JButton fourButton = new JButton("4");
        panelButtons.add(fourButton);

        JButton fiveButton = new JButton("5");
        panelButtons.add(fiveButton);

        JButton sixButton = new JButton("6");
        panelButtons.add(sixButton);

        JButton sevenButton = new JButton("7");
        panelButtons.add(sevenButton);

        JButton eightButton = new JButton("8");
        panelButtons.add(eightButton);

        JButton nineButton = new JButton("9");
        panelButtons.add(nineButton);

        frame.getContentPane().add(panelScreen, BorderLayout.NORTH);
        //frame.getContentPane().add(new JSeparator(), BorderLayout.CENTER);
        frame.getContentPane().add(panelButtons, BorderLayout.SOUTH);
        frame.setBounds(50, 50, 500, 500);
        frame.setResizable(false);
        //frame.pack();
        frame.setVisible(true);


    }


    public static void main(String[] args) {

        new calculator();

    }

}

and this the picture of programme;

enter image description here

i appreciate if you can help me. anyway thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

留一抹残留的笑 2024-12-17 22:53:50

一些建议:

  1. 不要设置 JFrame 的大小,实际上不要设置任何大小。
  2. 调用 pack 给所有组件设置它们自己的大小。
  3. 如果您希望按钮更大,请考虑更改其字体大小。

例如,

Calc2 GUI

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

import javax.swing.*;

public class Calc2 {
   public static final String[][] BUTTON_TEXTS = {
      {"7", "8", "9", "+"},
      {"4", "5", "6", "-"},
      {"1", "2", "3", "*"},
      {"0", ".", "/", "="}
   };
   public static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24);

   private static void createAndShowGui() {
      JTextField field = new JTextField(10);
      field.setFont(BTN_FONT.deriveFont(Font.PLAIN));
      JPanel btnPanel = new JPanel(new GridLayout(BUTTON_TEXTS.length,
            BUTTON_TEXTS[0].length));

      for (int i = 0; i < BUTTON_TEXTS.length; i++) {
         for (int j = 0; j < BUTTON_TEXTS[i].length; j++) {
            JButton btn = new JButton(BUTTON_TEXTS[i][j]);
            btn.setFont(BTN_FONT);
            btnPanel.add(btn);
         }
      }

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(field, BorderLayout.PAGE_START);
      mainPanel.add(btnPanel, BorderLayout.CENTER);


      JFrame frame = new JFrame("Calc2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

A few suggestions:

  1. Don't set the JFrame's size, and in fact don't set any sizes.
  2. Call pack to all the components to set their own sizes.
  3. If you want the buttons bigger, consider changing the size of their fonts.

e.g.,

Calc2 GUI

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

import javax.swing.*;

public class Calc2 {
   public static final String[][] BUTTON_TEXTS = {
      {"7", "8", "9", "+"},
      {"4", "5", "6", "-"},
      {"1", "2", "3", "*"},
      {"0", ".", "/", "="}
   };
   public static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24);

   private static void createAndShowGui() {
      JTextField field = new JTextField(10);
      field.setFont(BTN_FONT.deriveFont(Font.PLAIN));
      JPanel btnPanel = new JPanel(new GridLayout(BUTTON_TEXTS.length,
            BUTTON_TEXTS[0].length));

      for (int i = 0; i < BUTTON_TEXTS.length; i++) {
         for (int j = 0; j < BUTTON_TEXTS[i].length; j++) {
            JButton btn = new JButton(BUTTON_TEXTS[i][j]);
            btn.setFont(BTN_FONT);
            btnPanel.add(btn);
         }
      }

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(field, BorderLayout.PAGE_START);
      mainPanel.add(btnPanel, BorderLayout.CENTER);


      JFrame frame = new JFrame("Calc2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
美人迟暮 2024-12-17 22:53:50

计算器 GUI

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

// no need to extend frame!
//public class Calculator extends JFrame {
public class Calculator {

    public Calculator(){    
        initComponents();    
    }

    private void initComponents(){
        // I find it easier to create a panel and SET it as the content pane
        JPanel gui = new JPanel(new BorderLayout(5,5));
        // add some padding to the main GUI
        gui.setBorder(new EmptyBorder(4,4,4,4));

        // not needed if only a single compoinent is to be added!
        //JPanel panelScreen = new JPanel(new GridLayout(0,1));

        // add some constraints to make the output field bigger.
        // if it is intended to be single line, a JTextField should be used.
        JTextArea screen = new JTextArea(2,25);
        gui.add(screen, BorderLayout.NORTH);
        //panelScreen.add(screen);

        JFrame frame = new JFrame("CALCULATOR");
        frame.setContentPane(gui);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // add padding around the buttons
        JPanel panelButtons = new JPanel(new GridLayout(3,3,4,4));

        JButton oneButton = new JButton("1");
        panelButtons.add(oneButton);

        JButton twoButton = new JButton("2");
        panelButtons.add(twoButton);

        JButton threeButton = new JButton("3");
        panelButtons.add(threeButton);

        JButton fourButton = new JButton("4");
        panelButtons.add(fourButton);

        JButton fiveButton = new JButton("5");
        panelButtons.add(fiveButton);

        JButton sixButton = new JButton("6");
        panelButtons.add(sixButton);

        JButton sevenButton = new JButton("7");
        panelButtons.add(sevenButton);

        JButton eightButton = new JButton("8");
        panelButtons.add(eightButton);

        JButton nineButton = new JButton("9");
        panelButtons.add(nineButton);

        //frame.getContentPane().add(new JSeparator(), BorderLayout.CENTER);

        // Add the buttons to the CENTER and they will
        // fill whatever space they are provided.
        gui.add(panelButtons, BorderLayout.CENTER);
        //frame.setBounds(50, 50, 500, 500);
        //frame.setResizable(false);
        frame.pack();
        frame.setVisible(true); 
    }    

    public static void main(String[] args) {    
       java.awt.EventQueue.invokeLater(new Runnable() {

         @Override
         public void run() {
            new Calculator();
         }
       });    
    }    
}

Calculator GUI

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

// no need to extend frame!
//public class Calculator extends JFrame {
public class Calculator {

    public Calculator(){    
        initComponents();    
    }

    private void initComponents(){
        // I find it easier to create a panel and SET it as the content pane
        JPanel gui = new JPanel(new BorderLayout(5,5));
        // add some padding to the main GUI
        gui.setBorder(new EmptyBorder(4,4,4,4));

        // not needed if only a single compoinent is to be added!
        //JPanel panelScreen = new JPanel(new GridLayout(0,1));

        // add some constraints to make the output field bigger.
        // if it is intended to be single line, a JTextField should be used.
        JTextArea screen = new JTextArea(2,25);
        gui.add(screen, BorderLayout.NORTH);
        //panelScreen.add(screen);

        JFrame frame = new JFrame("CALCULATOR");
        frame.setContentPane(gui);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // add padding around the buttons
        JPanel panelButtons = new JPanel(new GridLayout(3,3,4,4));

        JButton oneButton = new JButton("1");
        panelButtons.add(oneButton);

        JButton twoButton = new JButton("2");
        panelButtons.add(twoButton);

        JButton threeButton = new JButton("3");
        panelButtons.add(threeButton);

        JButton fourButton = new JButton("4");
        panelButtons.add(fourButton);

        JButton fiveButton = new JButton("5");
        panelButtons.add(fiveButton);

        JButton sixButton = new JButton("6");
        panelButtons.add(sixButton);

        JButton sevenButton = new JButton("7");
        panelButtons.add(sevenButton);

        JButton eightButton = new JButton("8");
        panelButtons.add(eightButton);

        JButton nineButton = new JButton("9");
        panelButtons.add(nineButton);

        //frame.getContentPane().add(new JSeparator(), BorderLayout.CENTER);

        // Add the buttons to the CENTER and they will
        // fill whatever space they are provided.
        gui.add(panelButtons, BorderLayout.CENTER);
        //frame.setBounds(50, 50, 500, 500);
        //frame.setResizable(false);
        frame.pack();
        frame.setVisible(true); 
    }    

    public static void main(String[] args) {    
       java.awt.EventQueue.invokeLater(new Runnable() {

         @Override
         public void run() {
            new Calculator();
         }
       });    
    }    
}
浅笑依然 2024-12-17 22:53:50

您可能想研究一下这个遵循 @HFOE 和 @mre 建议的示例 。请注意,“大小”在代码中没有出现。

You might like to study this example that follows the suggestions of @HFOE and @mre. Note that "size" appears nowhere in the code.

箜明 2024-12-17 22:53:50
  1. 阅读在容器内布置组件
  2. 实施适当的布局

编辑-

快速解决方案-用BoxLayout替换JFrame布局管理器(即setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)) )。

  1. Read Laying Out Components Within a Container
  2. Implement appropriate layout(s)

EDIT -

Quick solution - replace the JFrame layout manager with BoxLayout (i.e. setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS))).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文