简体   繁体   中英

Gridbag layout issues in MAC jdk11

I am using gridbag layout in my java aaplication. I'm using a button & a text field inside the layout. I assign the weights to each component. The UI is as expected in Windows where the text field occupies most of the space with the button at the end. However on MAC, the opposite happens where the button occupies most of the space. I don't know why this is happening on MAC. Please help.Thanks in advance.

Here is my code:

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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