繁体   English   中英

MAC jdk11 中的 Gridbag 布局问题

[英]Gridbag layout issues in MAC jdk11

我在我的 java 应用程序中使用网格包布局。 我在布局内使用按钮和文本字段。 我将权重分配给每个组件。 UI 与 Windows 中的预期一致,其中文本字段占据了大部分空间,按钮位于末尾。 但是在 MAC 上,按钮占据大部分空间的情况正好相反。 我不知道为什么这会发生在 MAC 上。 请帮助。在此先感谢。

这是我的代码:

JPanel p = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
textField = new JTextField(100);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 0.90;
p.add(textField,gc);       
button = new JButton();
button.setPreferredSize(null);
button.setText("Click");
gc.weightx = 0.10;
p.add(button,gc);

macOS Catalina 版本 10.15.5 Beta (19F62f)

> java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

布局

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gc = new GridBagConstraints();
            JTextField textField = new JTextField(100);
            gc.fill = GridBagConstraints.HORIZONTAL;
            gc.weightx = 0.90;
            add(textField, gc);
            JButton button = new JButton();
            button.setPreferredSize(null);
            button.setText("Click");
            gc.weightx = 0.10;
            add(button, gc);
        }

    }
}

暂无
暂无

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

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