Java GUI 布局不太正确

标签 java user-interface layout

我尝试了很多不同的布局,但没有一个能给我想要的效果。

我想要这样的东西:

+-----------------------------+
         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

enter image description here

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

}

看看Laying Out Components Within a Container了解更多示例和详细信息

关于Java GUI 布局不太正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23902248/

相关文章:

ios - AutoLayout - 如何绑定(bind)两个 View 并相应地更改大小

android - 如何根据设置的主题更改flutter中的状态栏图标和文本颜色?

jquery - 删除 JQuery UI 选项卡中的填充

css - 使用 CSS : Last div on left to fill remaining area 的两列布局

JavaFX 全屏窗口 : remove popup text

java - JNI释放内存

c++ - 将 GUI C++ 应用程序转换为控制台应用程序

java - 我可以从 Java 线程中更改标签文本吗?

java - 无法调整 JInternalFrame 的大小

iphone - 修复 Mobile Safari (iPhone) 上文本呈现不一致且某些字体比其他字体大的字体大小问题?