문제

I'm developing a Java application for homework. This is my code

 JLabel queryHandlerL = new JLabel("Create php to handle query results", JLabel.CENTER);
 final JCheckBox queryHandlerCB = new JCheckBox();
 JPanel checkBoxPanel = new JPanel(new FlowLayout());
 checkBoxPanel.add(queryHandlerL);
 checkBoxPanel.add(queryHandlerCB);
 
 // Query Panel
 // set image
 picLabelQuery = new JLabel("",JLabel.LEFT);
 picLabelQuery.setIcon(currentPicForm);
 
 JPanel queryPanel = new JPanel();
 final JButton queryButton = new JButton("Insert a query");
 
 queryPanel.add(queryButton);
 queryPanel.add(picLabelQuery);
 
 // Panel create
 final JButton createButton = new JButton("Create");
 JPanel createPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
 createPanel.add(createButton);
 
 
 JPanel finalPanel = new JPanel(new GridLayout(0, 1,5,2)); 
 finalPanel.add(queryPanel);
 finalPanel.add(checkBoxPanel);
 finalPanel.add(createPanel);
 
 finalPanel.setBorder(BorderFactory.createTitledBorder("SQL connection"));

 setLayout(new GridBagLayout());
 add(finalPanel); 

I have a CardLayout and this is a Window inside this CardLayout. The last add(finalPanel) refers to the panel of the CardLayout.

This piece of code works but this is the result enter image description here

How do I remove the space that is automatically created between the panels?

도움이 되었습니까?

해결책

How do I remove the space that is automatically created between the panels?

Use a different layout manager for the panel. The GridLayout will always resize components to take up all the space in the panel.

Maybe you can use a BoxLayout or a GridBagLayout. You can also nest panels with different layout managers to get your desired effect.

Read the section from the Swing tutorial on Layout Managers for more information and examples.

다른 팁

You should pack() your surrounding panel or set the height to a desired value.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top