Java中的GridBagLayout不起作用
我一直在试着按我想要的方式调整我的显示器,但它似乎不起作用。我想使用GridBagLayout制作如下内容: 我找到了一段代码,并对其进行了编辑:Java中的GridBagLayout不起作用,java,jpanel,gridbaglayout,gridx,Java,Jpanel,Gridbaglayout,Gridx,我一直在试着按我想要的方式调整我的显示器,但它似乎不起作用。我想使用GridBagLayout制作如下内容: 我找到了一段代码,并对其进行了编辑: public class GBLPanel extends JPanel { private static final long serialVersionUID = 1L; GridBagConstraints gbc = new GridBagConstraints(); public GBLPanel(Dimen
public class GBLPanel extends JPanel
{
private static final long serialVersionUID = 1L;
GridBagConstraints gbc = new GridBagConstraints();
public GBLPanel(Dimension appdim)
{
GridBagConstraints c = new GridBagConstraints();
setLayout(new GridBagLayout());
add(gbcComponent(0,0,2,1,0,0), gbc);
add(gbcComponent(0,1,1,1,0,50), gbc);
add(gbcComponent(1,1,1,1,0,50), gbc);
}
private JPanel gbcComponent(int x, int y, int w, int h, int ipadyx, int ipadyy){
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.ipadx=ipadyx;
gbc.ipady=ipadyy;
gbc.fill = GridBagConstraints.BOTH;
JPanel panel = new JPanel();
JTextField text = new JTextField("(" + w + ", " + h + ")");
panel.setBorder(new TitledBorder("(" + x + ", " + y + ")"));
panel.add(text);
return panel;
}
}
我不知道如何塑造它,因为我想,任何人都可以帮助?非常感谢 A
BorderLayout
对您来说可能更容易做到这一点
但是如果您想要/需要使用GridBagLayout
,当前的问题是您将每个面板的x和y的权重设置为1。这意味着它们将均匀分布
通过这样做,尝试更改它们以反映您想要的值
public GBLPanel(Dimension appdim)
{
GridBagConstraints c = new GridBagConstraints();
setLayout(new GridBagLayout());
// Pass in weights also
add(gbcComponent(0,0,2,1,0,0, 1, 0.25), gbc); // 100% x and 25% y
add(gbcComponent(0,1,1,1,0,50, 0.25, 0.75), gbc); // 25% x and 75% y
add(gbcComponent(1,1,1,1,0,50, 0.75, 0.75), gbc); // 75% x and 75% y
}
private JPanel gbcComponent(int x, int y, int w, int h, int ipadyx, int ipadyy, double wx, double wy)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbc.weightx = wx; // Set to passed in values here
gbc.weighty = wy;
gbc.ipadx=ipadyx;
gbc.ipady=ipadyy;
gbc.fill = GridBagConstraints.BOTH;
JPanel panel = new JPanel();
JTextField text = new JTextField("(" + w + ", " + h + ")");
panel.setBorder(new TitledBorder("(" + x + ", " + y + ")"));
panel.add(text);
return panel;
}
非常感谢你的回答,爪哇恶魔,你帮了我很多。我实际上使用了BorderLayout来制作第一张图片,但我最近开始学习Java,不知道如何制作ResizeHandler。我还读到GridBagLayout是最灵活的,所以我想试试。