我正在学习如何使用 Java Swing 中的 GridBagLayout ...
在此示例中,我想在每行中放置 4 个元素,如您在我的代码中所见:
this.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
//constraints.weighty = 1;
constraints.anchor = GridBagConstraints.NORTHWEST;
constraints.fill = constraints.HORIZONTAL;
this.add(label1, constraints);
constraints.weighty = 0.0;
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 0;
constraints.gridheight = 1;
constraints.weighty = 1;
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = constraints.BOTH;
this.add(label, constraints);
constraints.weighty = 0.0;
JLabel precio = new JLabel();
precio.setText("Precio 12345.5");
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 2;
constraints.gridheight = 2;
constraints.weighty = 1;
constraints.fill = constraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.NORTH;
this.add(precio, constraints);
constraints.weighty = 0.0;
JLabel cantidad = new JLabel();
cantidad.setText("Cantidad ");
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridwidth = 2;
constraints.gridheight = 2;
constraints.weightx = 1;
constraints.fill = constraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.NORTH;
this.add(cantidad, constraints);
constraints.weighty = 0.0;
JTextField textfield = new JTextField();
textfield.setPreferredSize(new Dimension(100 , 30));
constraints.gridx = 2;
constraints.gridy = 3;
constraints.gridwidth = 2;
constraints.gridheight = 2;
constraints.weighty = 1;
constraints.fill = constraints.HORIZONTAL;
this.add(textfield, constraints);
constraints.weighty = 0.0;
JButton boton = new JButton();
boton.setText("Agregar");
constraints.gridx = 0;
constraints.gridy = 4;
constraints.gridwidth = 2;
constraints.gridheight = 2;
constraints.weightx = 1;
constraints.fill = constraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.NORTH;
this.add(precio, constraints);
constraints.weighty = 0.0;
正如您在代码中看到的那样。
在第 0 行,我想放一个 JLabel
在第 1 行,我想放一个带有图标的 JLabel
在第 2 行,我想放一个 JTextField
在第 3 行,我想在右侧放置一个 JTextfield 和 JTextField
最后在第 4 行,我想在列的中心放置一个 JButton
但是当我运行 JFrame 时我得到了这个:
从第二行开始,我让组件重叠,我不知道为什么。
也许我在 GridBagLayout 行为中遗漏了一些东西,因为我仍在学习如何使用它。
欢迎任何帮助!谢谢!
Oracle 有一个有用的教程,使用 Swing 创建 GUI。跳过使用 NetBeans IDE 学习摇摆部分。
您的代码有几个语法错误。一旦我修复了这些,我就运行了你的代码。
我不确定你到底想要什么。根据您的描述,我修改了您的代码以生成以下 GUI。
使用 aGridBagLayout
时,您设置了某些参数,并且您为每个 Swing 组件修改了其他参数。有关详细信息,请参阅 Oracle 教程如何使用 GridBagLayout。
下一次,发布可以复制到 IDE 并进行测试的可运行代码。
这是我使用的完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class GridBagLayoutExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GridBagLayoutExample());
}
@Override
public void run() {
JFrame frame = new JFrame("Cell-33");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JPanel createMainPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.LINE_START;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(0, 10, 5, 10);
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.gridx = 0;
constraints.gridy = 0;
JLabel label1 = new JLabel("Label1");
panel.add(label1, constraints);
constraints.gridx++;
JLabel label = new JLabel("Label");
panel.add(label, constraints);
constraints.gridx++;
JLabel precio = new JLabel();
precio.setText("Precio 12345.5");
panel.add(precio, constraints);
constraints.gridx++;
JLabel cantidad = new JLabel("Cantidad");
panel.add(cantidad, constraints);
constraints.gridx++;
JTextField textfield = new JTextField(20);
panel.add(textfield, constraints);
constraints.gridx++;
JButton boton = new JButton();
boton.setText("Agregar");
panel.add(boton, constraints);
return panel;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句