簡體   English   中英

Java GUI布局不能完全正確

[英]Java GUI layout cannot quite get it right

我嘗試了一堆不同的布局,但是都沒有給我想要的效果。

我想要這樣的東西:

+-----------------------------+
         Centered Text
+-------+
|Button |
+-------+
+-----------------------------+

在html中,它可能看起來像這樣:

<p align="center">Some text</p>
<input type="button" value="Press"/>

我遇到的麻煩是某些布局(BorderLayout),它喜歡調整按鈕的大小以適合自己。 其他布局(Boxlayout和GroupLayout)將執行以下操作:

+-----------------------------+
         Centered Text
               +-------+
               |Button |
               +-------+
+-----------------------------+

即使當我將JLabel對齊到CENTER並將Button對齊到LEFT時。

非常感謝我的助手們。

有多種布局可以實現此目的,實際上,您甚至可以同時使用BorderLayoutFlowLayout來完成此操作,但是此示例僅使用GridBagLayout

在此處輸入圖片說明

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ExampleLayout {

    public static void main(String[] args) {
        new ExampleLayout();
    }

    public ExampleLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;

            JLabel center = new JLabel("Centered Text");
            center.setHorizontalAlignment(JLabel.CENTER);
            add(center, gbc);

            gbc.gridy++;
            gbc.fill = GridBagConstraints.NONE;
            gbc.gridwidth = 1;
            gbc.weightx = 0;
            gbc.anchor = GridBagConstraints.WEST;

            JButton button = new JButton("Button");
            add(button, gbc);
        }
    }

}

查看有關在容器布置組件的更多示例和詳細信息

FlowLayout(int align)允許您定義對齊。 默認值為CENTER。 如果僅使包含按鈕的面板的FlowLayout對齊,則無需手動使用GridBagLayout可以工作。 NetBeans提供了一個出色的GridBagLayout定制器,但是您不想觸摸它自動生成的代碼。

在此處輸入圖片說明

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

public class MyLooks extends JFrame {

    public MyLooks() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        p = new JPanel(new GridLayout(2, 1));
        p1 = new JPanel();
        p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        myLabel = new JLabel("this is a label");
        myButton = new JButton("press");
        p1.add(myLabel);
        p2.add(myButton);
        p.add(p1);
        p.add(p2);
        setContentPane(p);
        pack();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MyLooks().setVisible(true);
            }
        });
    }

    private JLabel myLabel;
    private JButton myButton;
    private JPanel p, p1, p2;
}

盡管MadProgrammer和Costis Aivalis已經回答了您的問題, MigLayout也是MigLayout的答案:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

public class MigLayoutDemo {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();

    JLabel label = new JLabel("Centered text");
    JButton button = new JButton("Button");

    public MigLayoutDemo() {
        panel.setLayout(new MigLayout());
        label.setHorizontalAlignment(JLabel.CENTER);
        panel.add(label, "wrap, pushx, growx");
        panel.add(button);

        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(MigLayoutDemo::new);
    }

}

效果相同,但是這種方法不像GridBagLayout那樣冗長,我個人認為MigLayout更易於使用。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM