[英]GridLayout size doesn't fill all space
我需要你的帮助来完成我明天需要完成的项目。 我正在尝试实现一个选项卡,它将从数据库中放入一些值。 但我面临一个我无法解决的问题。 我想一一添加几个布局。 每个布局都分为 3 列,但这些布局(和列)不会占用您在此屏幕上看到的所有可用空间:
这是我的代码:
package pizzeria;
import java.sql.SQLException;
import javax.swing.*;
import java.awt.*;
public class CarteDesPizzas extends JPanel{
static GridLayout grid1 = new GridLayout(1,1);
static GridLayout grid2 = new GridLayout(1,3);
static GridLayout grid3 = new GridLayout(1,1);
public static JPanel affichageCarte() throws SQLException ,Exception {
JLabel label = new JLabel("Carte des pizzas", JLabel.CENTER);
label.setForeground(new Color(120, 90, 40));
JPanel selection = new JPanel();
GridBagLayout grid = new GridBagLayout();
selection.setLayout(grid);
JPanel header;
JPanel content;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0.1;
c.gridy = 0;
header = new JPanel(new GridBagLayout());
header.add(label);
selection.add(header,c);
content = new JPanel();
c.weightx = 1;
c.weighty = 0.9;
c.gridy = 1;
JPanel selection2 = new JPanel(grid2);
JPanel pinkSelection = new JPanel();
pinkSelection.setBackground(Color.PINK);
JPanel redSelection = new JPanel();
redSelection.setBackground(Color.RED);
JPanel greenSelection = new JPanel();
greenSelection.setBackground(Color.GREEN);
selection2.add(pinkSelection);
selection2.add(redSelection);
selection2.add(greenSelection);
content.add(selection2);
selection.add(content,c);
return selection;
}
}
谢谢您的帮助
JPanel
是一个容器。 它的目的是包含其他Component
。
GridBagLayout将组件放置在网格中的矩形(单元格)中,然后使用组件的首选大小来确定单元格的大小。
默认情况下, JPanel
没有首选大小。 它的大小取决于它所包含的组件的大小。 由于代码中的JPanel
不包含任何组件,因此它们的首选大小非常小。 因此它们在您的 GUI 中显得非常小。
最简单的解决方案是为JPanel
显式分配首选大小。
在下面的代码中,我只设置了pinkSelection
的首选大小。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class CarteDesPizzas extends JPanel {
static GridLayout grid1 = new GridLayout(1,1);
static GridLayout grid2 = new GridLayout(1,3);
static GridLayout grid3 = new GridLayout(1,1);
public static JPanel affichageCarte() {
JLabel label = new JLabel("Carte des pizzas", JLabel.CENTER);
label.setForeground(new Color(120, 90, 40));
JPanel selection = new JPanel();
GridBagLayout grid = new GridBagLayout();
selection.setLayout(grid);
JPanel header;
JPanel content;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0.1;
c.gridy = 0;
header = new JPanel(new GridBagLayout());
header.add(label);
selection.add(header,c);
content = new JPanel();
c.weightx = 1;
c.weighty = 0.9;
c.gridy = 1;
JPanel selection2 = new JPanel(grid2);
JPanel pinkSelection = new JPanel();
pinkSelection.setPreferredSize(new Dimension(100, 60));
pinkSelection.setBackground(Color.PINK);
JPanel redSelection = new JPanel();
redSelection.setBackground(Color.RED);
JPanel greenSelection = new JPanel();
greenSelection.setBackground(Color.GREEN);
selection2.add(pinkSelection);
selection2.add(redSelection);
selection2.add(greenSelection);
content.add(selection2);
selection.add(content,c);
return selection;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(affichageCarte());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
}
这是一个屏幕截图:
尽管您声明了这三个变量,但您没有使用 GridLayout。 您正在使用带有一些特定值的 GridBagLayout,这些值会导致您得到什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.