这是此问题的扩展:Java gridbaglayout problems。由于panel1的宽度溢出,Panel1与panel2重叠。但是,如果我删除gc.gridwidth = 2
它将正确对齐panel2,但它也会将组合框移动到右侧的原始点。我一直在玩不同的gridbaglayout属性,但不能像我想要的那样对齐它。
继承我的代码:
public void createGUI()
{
main_panel = new JPanel();
main_panel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 0;
gc.insets = new Insets(5, 0, 10, 0);
main_panel.add(label, gc);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 1;
gc.gridy = 0;
gc.insets = new Insets(5, 0, 10, 0);
main_panel.add(band_combobox, gc);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 1;
gc.gridwidth = 2;
gc.insets = new Insets(0, 0, 10, 0);
main_panel.add(panel1, gc);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 1;
gc.gridy = 1;
gc.gridwidth = 2;
gc.insets = new Insets(0, 0, 10, 0);
main_panel.add(panel2, gc);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 2;
gc.gridwidth = 2;
gc.insets = new Insets(0, 0, 10, 0);
main_panel.add(panel3, gc);
}
我正在尝试做这样的事情:
答案 0 :(得分:2)
试试这个:
public void createGUI()
{
main_panel = new JPanel();
main_panel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 0;
gc.insets = new Insets(5, 0, 10, 0);
main_panel.add(label, gc);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 1;
gc.gridy = 0;
gc.insets = new Insets(5, 0, 10, 0);
main_panel.add(band_combobox, gc);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 1;
gc.gridwidth = 1;
gc.weightx = 1;
gc.weighty = 1;
gc.insets = new Insets(0, 0, 10, 0);
main_panel.add(panel1, gc);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 1;
gc.gridy = 1;
gc.gridwidth = 1;
gc.weightx = 1;
gc.weighty = 1;
gc.insets = new Insets(0, 0, 10, 0);
main_panel.add(panel2, gc);
gc.fill = GridBagConstraints.BOTH;
gc.gridx = 0;
gc.gridy = 2;
gc.weightx = 1;
gc.weighty = 1;
gc.gridwidth = 2;
gc.insets = new Insets(0, 0, 10, 0);
main_panel.add(panel3, gc);
}
你没有设定重量,你的宽度设置有点狡猾。
还将乐队标签和组合放在JPanel中,并将其宽度设置为2,并将其添加到顶部。它看起来好多了。
另请参阅我的GridBagLayout教程(在我的博客中,在我的个人资料中)以获得更多帮助。
答案 1 :(得分:2)
不要害怕使用复合布局来满足您的需求。您的标签和组合框想要“浮动”在顶行。使用GridBagLayout
来实现这一点很困难,但通过将它们添加到另一个容器中,使用不同的布局管理器,它是可以实现的......
当您更改组件gridWidth
或gridHeight
时,它将允许它们“流”到其他列和行中,这意味着它们可能位于占据该空间的组件的上方或下方,具体取决于当他们被添加...
另外,请查看weight
属性。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;
public class TestPane {
private JPanel main_panel;
public static void main(String[] args) {
new TestPane();
}
public TestPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
createGUI();;
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(main_panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public void createGUI() {
main_panel = new JPanel();
main_panel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
JLabel label = new JLabel("Band Directory");
JComboBox band_combobox = new JComboBox();
JPanel panel1 = new JPanel();
panel1.add(new JLabel("1"));
panel1.setBorder(new TitledBorder("Panel1"));
JPanel panel2 = new JPanel();
panel2.setBorder(new TitledBorder("Panel2"));
panel2.add(new JLabel("2"));
JPanel panel3 = new JPanel();
panel3.setBorder(new TitledBorder("Panel3"));
panel3.add(new JLabel("3"));
JPanel top = new JPanel();
top.add(label);
top.add(band_combobox);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 1;
gc.gridx = 0;
gc.gridy = 0;
gc.gridwidth = GridBagConstraints.REMAINDER;
gc.insets = new Insets(5, 0, 10, 0);
main_panel.add(top, gc);
gc.gridwidth = 1;
gc.weightx = 0.5;
gc.gridx = 0;
gc.gridy = 1;
gc.insets = new Insets(0, 0, 10, 0);
main_panel.add(panel1, gc);
gc.gridx = 1;
gc.gridy = 1;
gc.insets = new Insets(0, 0, 10, 0);
main_panel.add(panel2, gc);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 1;
gc.gridx = 0;
gc.gridy = 2;
gc.gridwidth = GridBagConstraints.REMAINDER;
gc.insets = new Insets(0, 0, 10, 0);
main_panel.add(panel3, gc);
}
}
您可能会发现阅读How to use GridBagLayout
很有用答案 2 :(得分:2)
Band Directory标签,Combo Box,Member Panel和CD List Panel应分配gridwidth = 1并将CD Panel添加为gridwith = 2.
public JPanel getComponentPanel()
{
main_panel = new JPanel();
main_panel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.insets = new Insets(5, 0, 10, 0);
main_panel.add(label, gc);
gc.gridx = 1;
gc.gridy = 0;
gc.insets = new Insets(5, 10, 10, 0);
main_panel.add(band_combobox, gc);
gc.gridx = 0;
gc.gridy = 1;
gc.insets = new Insets(0, 10, 10, 0);
panel1.setBorder(
BorderFactory.createLineBorder(Color.BLACK));
panel1.setMinimumSize(new Dimension(100, 50));
panel1.setPreferredSize(new Dimension(100, 50));
panel1.setMaximumSize(new Dimension(100, 50));
main_panel.add(panel1, gc);
gc.gridx = 1;
gc.gridy = 1;
gc.insets = new Insets(0, 10, 10, 0);
panel2.setBorder(
BorderFactory.createLineBorder(Color.BLACK));
panel2.setMinimumSize(new Dimension(100, 50));
panel2.setPreferredSize(new Dimension(100, 50));
panel2.setMaximumSize(new Dimension(100, 50));
main_panel.add(panel2, gc);
gc.gridx = 0;
gc.gridy = 2;
gc.gridwidth = 2;
gc.insets = new Insets(0, 10, 10, 0);
panel3.setBorder(
BorderFactory.createLineBorder(Color.BLACK));
panel3.setMinimumSize(new Dimension(100, 50));
panel3.setPreferredSize(new Dimension(100, 50));
panel3.setMaximumSize(new Dimension(100, 50));
main_panel.add(panel3, gc);
return main_panel;
}
答案 3 :(得分:1)
GBC需要在第1行创建列坐标,然后为整个容器创建矩阵,调整大小时需要缓慢的渐变而不会闪烁
确定可以虚拟地创建这个GBC矩阵,而不会将不可见的JComponents(在这种情况下添加了边框的JLabel)放到contianer上,这可能是为什么烦恼,
这是关于使用GBC的AbsoluteLayout,另一种方法是使用隐形JComponents
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class FrameAndGBC {
private JLabel hidelLabel;
private JLabel firstLabel;
private JTextField firstText;
private JFrame frame = new JFrame("Frame And GBC");
public FrameAndGBC() {
JPanel panel = copyTextNorthPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
private JPanel copyTextNorthPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
for (int k = 0; k < 50; k++) {
hidelLabel = new JLabel(" ");
hidelLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0.5;
gbc.weighty = 0.5;
gbc.gridx = k;
gbc.gridy = 0;
panel.add(hidelLabel, gbc);
}
for (int k = 0; k < 5; k++) {
firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 0;
gbc.gridwidth = 8;
gbc.gridy = k + 1;
panel.add(firstLabel, gbc);
}
for (int k = 0; k < 5; k++) {
firstText = new JTextField("Testing TextField");
firstText.setFont(new Font("Serif", Font.BOLD, 20));
firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 9;
gbc.gridwidth = k + 8;
gbc.gridy = k + 1;
panel.add(firstText, gbc);
}
for (int k = 0; k < 5; k++) {
firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 20 + k;
gbc.gridwidth = 8;
gbc.gridy = k + 1;
panel.add(firstLabel, gbc);
}
for (int k = 0; k < 5; k++) {
firstText = new JTextField("Testing TextField");
firstText.setFont(new Font("Serif", Font.BOLD, 20));
firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 29 + k;
gbc.gridwidth = 21 - k;
gbc.gridy = k + 1;
panel.add(firstText, gbc);
}
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
FrameAndGBC fs = new FrameAndGBC();
}
});
}
}