简体   繁体   English

Java Swing面板/按钮布局

[英]Java Swing Panel/Button Layout

I am creating a simple calculator GUI. 我正在创建一个简单的计算器GUI。 Also,I am having an issue with resizing the help and exit button to about the size of the numbers button. 另外,我在将“帮助”和“退出”按钮的大小调整到大约数字按钮的大小时遇到​​问题。 I know I am doing something wrong with GridLayout , but not sure what it is.The code has no issues. 我知道我在GridLayout做错了什么,但不确定是什么,代码没有问题。

This is what it looks like: 看起来是这样的:

public static final int WIDTH = 350;
public static final int HEIGHT = 500;
public static final int NUMBER_OF_CHAR = 4;

private JTextField single;
private JTextField equation;
boolean setBlank = false;
Double result;
String resultStr;
String verifyAction = "";
public Calculator()
{
    super("Calculator");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridLayout(3,0,0,0));
    JPanel top = new JPanel();
    top.setLayout(new GridLayout(1,2));

    JPanel mid = new JPanel();
    mid.setLayout(new GridLayout(2,1));

    JPanel bottom = new JPanel();
    bottom.setLayout(new GridLayout(4,4));
    //TOP
    JButton exit = new JButton("Exit");
    exit.addActionListener(this);
    top.add(exit);

    JButton help = new JButton("Help");

    help.addActionListener(this);
    top.add(help);
    //MID
    single = new JTextField(NUMBER_OF_CHAR);
    mid.add(single);
    equation = new JTextField(NUMBER_OF_CHAR);
    mid.add(equation);
    //BOTTOM
    String [] key = {"7","8","9","+","4","5","6","","1","2","3","/","0",".","clear","="};
    for(int i = 0 ; i < key.length;i++)
    {
        JButton button = new JButton(key[i]);
        button.addActionListener(this);
        bottom.add(button);
    }
    add(top,BorderLayout.NORTH);
    add(mid,BorderLayout.CENTER);
    add(bottom,BorderLayout.SOUTH);
}

This is a main problem: setSize(WIDTH, HEIGHT); 这是一个主要问题: setSize(WIDTH, HEIGHT); . Don't do this as this can cause components that you don't want to have re-size do so. 请勿执行此操作,因为这可能会导致您不希望调整大小的组件这样做。 Instead all the components to size to their own preferred sizes by calling pack() on the top window, the JFrame, after adding all components. 在添加所有组件之后,通过在顶部窗口JFrame上调用pack() ,将所有组件调整为自己想要的大小。 Again this will let the GUI size itself. 同样,这将使GUI本身具有大小。

Don't use GridLayout for the JPanel, but rather leave it BorderLayout, and play with your GUI component fonts. 不要将GridLayout用于JPanel,而应将其保留为BorderLayout,并使用您的GUI组件字体。 For example 例如

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

public class Calculator extends JFrame implements ActionListener {
    public static final int WIDTH = 350;
    public static final int HEIGHT = 500;
    public static final int NUMBER_OF_CHAR = 4;
    public static final Font MY_FONT = new Font(Font.DIALOG, Font.BOLD, 24);

    private JTextField single;
    private JTextField equation;
    boolean setBlank = false;
    Double result;
    String resultStr;
    String verifyAction = "";

    public Calculator() {
        super("Calculator");
        // setSize(WIDTH, HEIGHT);  // !! No ***************
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // setLayout(new GridLayout(3, 0, 0, 0)); // !! No ***************
        JPanel top = new JPanel();
        top.setLayout(new GridLayout(1, 2));

        JPanel mid = new JPanel();
        mid.setLayout(new GridLayout(2, 1));

        JPanel bottom = new JPanel();
        bottom.setLayout(new GridLayout(4, 4));
        // TOP
        JButton exit = new JButton("Exit");
        exit.setFont(MY_FONT);
        exit.addActionListener(this);
        top.add(exit);

        JButton help = new JButton("Help");
        help.setFont(MY_FONT);

        help.addActionListener(this);
        top.add(help);
        // MID
        single = new JTextField(NUMBER_OF_CHAR);
        single.setFont(MY_FONT);
        mid.add(single);
        equation = new JTextField(NUMBER_OF_CHAR);
        equation.setFont(MY_FONT);
        mid.add(equation);
        // BOTTOM
        String[] key = { "7", "8", "9", "+", "4", "5", "6", "", "1", "2", "3", "/", "0", ".", "clear", "=" };
        for (int i = 0; i < key.length; i++) {
            JButton button = new JButton(key[i]);
            button.setFont(MY_FONT);
            button.addActionListener(this);
            bottom.add(button);
        }
        add(top, BorderLayout.NORTH);
        add(mid, BorderLayout.CENTER);
        add(bottom, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM