简体   繁体   中英

How to remove extra spaces in JFrame using GridBagLayout?

When I insert any button or any component in my frame and set the component gridx and gridy to values 0 , it does not put that component at the starting of the frame instead it put the component somewhere in the center.

How to remove the spaces and put my component at the very starting of the frame?

Following is the code:

         JFrame frame = new JFrame("Grigbag layout");

         JPanel pane = new JPanel(new GridBagLayout());

         GridBagConstraints c = new GridBagConstraints();

         JButton button = new JButton("First button");

         c.gridx = 0;
         c.gridy = 0;
         pane.add(button, c);

在此处输入图片说明

I suggest you to use Flow Layout and also add this code

frame.pack();

as mentioned by Andrew Thompson above

for more : https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html

Use the frame.pack() method and remove the frame.setSize(); method.

frame is using the default layout. Set frame's layout with something like

frame.setLayout(new FlowLayout(FlowLayout.LEFT));

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