gpt4 book ai didi

java - MAC jdk11中的Gridbag布局问题

转载 作者:行者123 更新时间:2023-12-01 17:16:08 26 4
gpt4 key购买 nike

我在我的 java aaplication 中使用 gridbag 布局。我在布局内使用按钮和文本字段。我为每个组件分配权重。 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 测试版 (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)

Layout

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

}
}

关于java - MAC jdk11中的Gridbag布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61383547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com