我所有的组件都放在一起,我已经尝试了一段时间了
all my components are placed over each other, i've been tryna figure it out a while now
有人能告诉我我做错了什么吗,我先尝试铺设面板,但结果相同,然后我回去使用组件,结果相同,所有组件都叠放在一起,我试过各种方法,结果一样,它们相互重叠,并排在中间
public class FnaComponents extends JPanel
{
public FnaComponents()
{
gridbag = new GridBagLayout();
setLayout(gridbag);
setPreferredSize(new Dimension(600,600));
//setBackground(Color.lightGray);
mainPanel = new JPanel(gridbag);
mainPanel.setPreferredSize(new Dimension(600, 600));
pTextField = new JTextField();
//addcomponents(new JLabel("Policy #"), mainPanel, 0, 0, 0, 0, 0, 0, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST);
//addcomponents(pTextField, mainPanel, null, 2, 0, 2, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
Invisible = new JButton();
//addcomponents(Invisible, mainPanel, 0, 0, 0, 0, 0, 0, GridBagConstraints.NONE, GridBagConstraints.EAST);
newbTextField = new JTextField();
newbButton = new JButton("Cal Icon");
//addcomponents(new JLabel("NB Date:"), mainPanel, 1, 0, 0, 0, 4, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addcomponents(newbTextField,mainPanel, null, 1, 1, 2, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addcomponents(newbButton,mainPanel, 1, 4, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
bilimButton = new JComboBox<>(bilimits);
bilimButton.setEditable(true);
bicslButton = new JComboBox<>(bicsl);
bicslButton.setEditable(true);
//addcomponents(new JLabel("BI Limit:"), mainPanel, 2, 0, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addcomponents(bilimButton, mainPanel, 2, 1, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addcomponents(bicslButton,mainPanel, 2, 2, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
lapseButton = new JComboBox<>(lapse);
//addComponents(new JLabel("Lapse:"), tophalf, 3, 0, 2, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addComponents(lapseButton, lPanel);
//addcomponents(lapseButton, tophalf, 3, 1, 0, 0, 4, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addcomponents(mainPanel, this, null);
add(mainPanel, BorderLayout.NORTH);
}
// method to add components to a container
private void addcomponents(JComponent cont, Container main, Border border)
{
Color code = new Color(0, 255, 255);
Border padborder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border bord = BorderFactory.createLineBorder(code, 1, true);
border = BorderFactory.createCompoundBorder(bord, padborder);
cont.setBorder(border);
main.add(cont);
}
// method to add components to a container
private void addcomponents(JComponent cont, Container main, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill, int anchor)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
gbc.anchor = anchor;
gbc.insets = new Insets(10, 10, 10, 10);
gridbag.setConstraints(cont, gbc);
main.add(cont);
}
// method to add components to a container
private void addcomponents(JComponent cont, Container main, Border border, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill, int anchor)
{
GridBagConstraints gbc = new GridBagConstraints();
Color code = new Color(0, 255, 255);
Border padborder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border bord = BorderFactory.createLineBorder(code, 1, true);
border = BorderFactory.createCompoundBorder(bord, padborder);
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
gbc.anchor = anchor;
gbc.insets = new Insets(10, 10, 10, 10);
cont.setBorder(border);
gridbag.setConstraints(cont, gbc);
main.add(cont);
}
}
代码有点难读...
首先,我建议不要在容器之间共享相同的 GridBagLayout
实例...
gridbag = new GridBagLayout();
setLayout(gridbag);
//...
mainPanel = new JPanel(gridbag);
这可能会导致问题,相反,为每个容器提供自己的 GridBagLayout
实例
这意味着您将无法执行类似 gridbag.setConstraints(cont, gbc);
的操作,事实上,根据您的代码尝试工作的方式,我可能会建议您不要这样做,而是使用类似 main.add(cont, gbc)
,这会将约束传递给当前分配的布局管理器。
其次,gridwidth
和gridheight
在正常情况下应该不少于1
,否则,你会遇到你遇到的问题
所以你应该使用更像...
addcomponents(new JLabel("Policy #"), mainPanel, 0, 0, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST);
addcomponents(pTextField, mainPanel, null, 2, 0, 2, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
虽然这没关系,但由于参数的数量,很容易忘记哪个参数做了什么。我可能会考虑使用构建器模式来构建约束,但这就是我
有人能告诉我我做错了什么吗,我先尝试铺设面板,但结果相同,然后我回去使用组件,结果相同,所有组件都叠放在一起,我试过各种方法,结果一样,它们相互重叠,并排在中间
public class FnaComponents extends JPanel
{
public FnaComponents()
{
gridbag = new GridBagLayout();
setLayout(gridbag);
setPreferredSize(new Dimension(600,600));
//setBackground(Color.lightGray);
mainPanel = new JPanel(gridbag);
mainPanel.setPreferredSize(new Dimension(600, 600));
pTextField = new JTextField();
//addcomponents(new JLabel("Policy #"), mainPanel, 0, 0, 0, 0, 0, 0, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST);
//addcomponents(pTextField, mainPanel, null, 2, 0, 2, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
Invisible = new JButton();
//addcomponents(Invisible, mainPanel, 0, 0, 0, 0, 0, 0, GridBagConstraints.NONE, GridBagConstraints.EAST);
newbTextField = new JTextField();
newbButton = new JButton("Cal Icon");
//addcomponents(new JLabel("NB Date:"), mainPanel, 1, 0, 0, 0, 4, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addcomponents(newbTextField,mainPanel, null, 1, 1, 2, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addcomponents(newbButton,mainPanel, 1, 4, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
bilimButton = new JComboBox<>(bilimits);
bilimButton.setEditable(true);
bicslButton = new JComboBox<>(bicsl);
bicslButton.setEditable(true);
//addcomponents(new JLabel("BI Limit:"), mainPanel, 2, 0, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addcomponents(bilimButton, mainPanel, 2, 1, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addcomponents(bicslButton,mainPanel, 2, 2, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
lapseButton = new JComboBox<>(lapse);
//addComponents(new JLabel("Lapse:"), tophalf, 3, 0, 2, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
//addComponents(lapseButton, lPanel);
//addcomponents(lapseButton, tophalf, 3, 1, 0, 0, 4, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addcomponents(mainPanel, this, null);
add(mainPanel, BorderLayout.NORTH);
}
// method to add components to a container
private void addcomponents(JComponent cont, Container main, Border border)
{
Color code = new Color(0, 255, 255);
Border padborder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border bord = BorderFactory.createLineBorder(code, 1, true);
border = BorderFactory.createCompoundBorder(bord, padborder);
cont.setBorder(border);
main.add(cont);
}
// method to add components to a container
private void addcomponents(JComponent cont, Container main, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill, int anchor)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
gbc.anchor = anchor;
gbc.insets = new Insets(10, 10, 10, 10);
gridbag.setConstraints(cont, gbc);
main.add(cont);
}
// method to add components to a container
private void addcomponents(JComponent cont, Container main, Border border, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill, int anchor)
{
GridBagConstraints gbc = new GridBagConstraints();
Color code = new Color(0, 255, 255);
Border padborder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border bord = BorderFactory.createLineBorder(code, 1, true);
border = BorderFactory.createCompoundBorder(bord, padborder);
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
gbc.anchor = anchor;
gbc.insets = new Insets(10, 10, 10, 10);
cont.setBorder(border);
gridbag.setConstraints(cont, gbc);
main.add(cont);
}
}
代码有点难读...
首先,我建议不要在容器之间共享相同的 GridBagLayout
实例...
gridbag = new GridBagLayout();
setLayout(gridbag);
//...
mainPanel = new JPanel(gridbag);
这可能会导致问题,相反,为每个容器提供自己的 GridBagLayout
这意味着您将无法执行类似 gridbag.setConstraints(cont, gbc);
的操作,事实上,根据您的代码尝试工作的方式,我可能会建议您不要这样做,而是使用类似 main.add(cont, gbc)
,这会将约束传递给当前分配的布局管理器。
其次,gridwidth
和gridheight
在正常情况下应该不少于1
,否则,你会遇到你遇到的问题
所以你应该使用更像...
addcomponents(new JLabel("Policy #"), mainPanel, 0, 0, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST);
addcomponents(pTextField, mainPanel, null, 2, 0, 2, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
虽然这没关系,但由于参数的数量,很容易忘记哪个参数做了什么。我可能会考虑使用构建器模式来构建约束,但这就是我