簡體   English   中英

Java Swing面板/按鈕布局

[英]Java Swing Panel/Button Layout

我正在創建一個簡單的計算器GUI。 另外,我在將“幫助”和“退出”按鈕的大小調整到大約數字按鈕的大小時遇到​​問題。 我知道我在GridLayout做錯了什么,但不確定是什么,代碼沒有問題。

看起來是這樣的:

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

這是一個主要問題: setSize(WIDTH, HEIGHT); 請勿執行此操作,因為這可能會導致您不希望調整大小的組件這樣做。 在添加所有組件之后,通過在頂部窗口JFrame上調用pack() ,將所有組件調整為自己想要的大小。 同樣,這將使GUI本身具有大小。

不要將GridLayout用於JPanel,而應將其保留為BorderLayout,並使用您的GUI組件字體。 例如

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