如何在 gridbaglayout 中更改网格的最小高度
how to change minimum Height of a grid in gridbag layout
这里我在面板上使用 gridbag
布局来添加 5 行排列的 5 个组件,我希望它们具有不同的高度 like this 但是在这样做之后
public void mainpanel3(){
pupper.setBackground(Color.yellow);
ptable.setBackground(Color.BLACK);
lthird.setOpaque(true);
lthird.setBackground(Color.BLUE);
tffourth.setBackground(Color.GREEN);
pfifth.setBackground(Color.WHITE);
outerp3.setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
c.weightx=0.5;
c.weighty=0.5;
c.fill=GridBagConstraints.BOTH;
c.weightx=0.5;
c.weighty=0.1;
c.ipady=5;
c.gridx=0;
c.gridy=0;
outerp3.add(pupper,c);
c.weightx=0.5;
c.weighty=0.4;
c.ipady=20;
c.gridx=0;
c.gridy=1;
outerp3.add(ptable,c);
c.weightx=0.5;
c.weighty=0.1;
c.ipady=5;
c.gridx=0;
c.gridy=2;
outerp3.add(lthird,c);
c.weightx=0.5;
c.weighty=0.1;
c.ipady=5;
c.gridx=0;
c.gridy=3;
outerp3.add(tffourth,c);
c.weightx=0.5;
c.weighty=0.1;
c.ipady=5;
c.gridx=0;
c.gridy=4;
outerp3.add(pfifth,c);
}
这会发生
output
第五分量(a panel
)白色没有出现
有关此面板的编码仅在此方法中完成,如果您需要任何其他代码,请告诉我。
您需要指定 weighty
值,但每个组件的 weightx
将始终为 1.0
,因为每个组件仅在 height
方面有所不同,而不是width
。这是一个工作示例:
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutExample {
private GridBagConstraints m_GBC;
public GridBagLayoutExample () {
m_GBC = new GridBagConstraints ();
m_GBC.anchor = GridBagConstraints.FIRST_LINE_START;
m_GBC.fill = GridBagConstraints.BOTH;
}
private void displayGUI () {
JFrame frame = new JFrame ( "GridBagLayout Example" );
frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
JPanel contentPane = new JPanel ();
contentPane.setLayout ( new GridBagLayout () );
addComponent ( contentPane, getPanel (), 0, 0, 1, 1, 1.0, 0.1 );
addComponent ( contentPane, getPanel (), 0, 1, 1, 5, 1.0, 0.5 );
addComponent ( contentPane, getPanel (), 0, 6, 1, 1, 1.0, 0.1 );
addComponent ( contentPane, getPanel (), 0, 7, 1, 2, 1.0, 0.2 );
addComponent ( contentPane, getPanel (), 0, 9, 1, 1, 1.0, 0.1 );
frame.setContentPane ( contentPane );
frame.pack ();
frame.setLocationByPlatform ( true );
frame.setVisible ( true );
}
private void addComponent ( JComponent contentPane, JComponent component,
int x, int y, int gridwidth, int gridheight,
double weightx, double weighty ) {
m_GBC.gridx = x;
m_GBC.gridy = y;
m_GBC.gridwidth = gridwidth;
m_GBC.gridheight = gridheight;
m_GBC.weightx = weightx;
m_GBC.weighty = weighty;
contentPane.add ( component, m_GBC );
}
private JPanel getPanel () {
JPanel panel = new JPanel ();
panel.setOpaque ( true );
Color color = new Color ( ( float ) Math.random (), ( float ) Math.random (),
( float ) Math.random (), ( float ) Math.random () );
panel.setBackground ( color );
return panel;
}
public static void main ( String [] args ) {
Runnable runnable = new Runnable () {
@Override
public void run () {
new GridBagLayoutExample ().displayGUI ();
}
};
EventQueue.invokeLater ( runnable );
}
}
输出
这里我在面板上使用 gridbag
布局来添加 5 行排列的 5 个组件,我希望它们具有不同的高度 like this 但是在这样做之后
public void mainpanel3(){
pupper.setBackground(Color.yellow);
ptable.setBackground(Color.BLACK);
lthird.setOpaque(true);
lthird.setBackground(Color.BLUE);
tffourth.setBackground(Color.GREEN);
pfifth.setBackground(Color.WHITE);
outerp3.setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
c.weightx=0.5;
c.weighty=0.5;
c.fill=GridBagConstraints.BOTH;
c.weightx=0.5;
c.weighty=0.1;
c.ipady=5;
c.gridx=0;
c.gridy=0;
outerp3.add(pupper,c);
c.weightx=0.5;
c.weighty=0.4;
c.ipady=20;
c.gridx=0;
c.gridy=1;
outerp3.add(ptable,c);
c.weightx=0.5;
c.weighty=0.1;
c.ipady=5;
c.gridx=0;
c.gridy=2;
outerp3.add(lthird,c);
c.weightx=0.5;
c.weighty=0.1;
c.ipady=5;
c.gridx=0;
c.gridy=3;
outerp3.add(tffourth,c);
c.weightx=0.5;
c.weighty=0.1;
c.ipady=5;
c.gridx=0;
c.gridy=4;
outerp3.add(pfifth,c);
}
这会发生
output
第五分量(a panel
)白色没有出现
有关此面板的编码仅在此方法中完成,如果您需要任何其他代码,请告诉我。
您需要指定 weighty
值,但每个组件的 weightx
将始终为 1.0
,因为每个组件仅在 height
方面有所不同,而不是width
。这是一个工作示例:
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutExample {
private GridBagConstraints m_GBC;
public GridBagLayoutExample () {
m_GBC = new GridBagConstraints ();
m_GBC.anchor = GridBagConstraints.FIRST_LINE_START;
m_GBC.fill = GridBagConstraints.BOTH;
}
private void displayGUI () {
JFrame frame = new JFrame ( "GridBagLayout Example" );
frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
JPanel contentPane = new JPanel ();
contentPane.setLayout ( new GridBagLayout () );
addComponent ( contentPane, getPanel (), 0, 0, 1, 1, 1.0, 0.1 );
addComponent ( contentPane, getPanel (), 0, 1, 1, 5, 1.0, 0.5 );
addComponent ( contentPane, getPanel (), 0, 6, 1, 1, 1.0, 0.1 );
addComponent ( contentPane, getPanel (), 0, 7, 1, 2, 1.0, 0.2 );
addComponent ( contentPane, getPanel (), 0, 9, 1, 1, 1.0, 0.1 );
frame.setContentPane ( contentPane );
frame.pack ();
frame.setLocationByPlatform ( true );
frame.setVisible ( true );
}
private void addComponent ( JComponent contentPane, JComponent component,
int x, int y, int gridwidth, int gridheight,
double weightx, double weighty ) {
m_GBC.gridx = x;
m_GBC.gridy = y;
m_GBC.gridwidth = gridwidth;
m_GBC.gridheight = gridheight;
m_GBC.weightx = weightx;
m_GBC.weighty = weighty;
contentPane.add ( component, m_GBC );
}
private JPanel getPanel () {
JPanel panel = new JPanel ();
panel.setOpaque ( true );
Color color = new Color ( ( float ) Math.random (), ( float ) Math.random (),
( float ) Math.random (), ( float ) Math.random () );
panel.setBackground ( color );
return panel;
}
public static void main ( String [] args ) {
Runnable runnable = new Runnable () {
@Override
public void run () {
new GridBagLayoutExample ().displayGUI ();
}
};
EventQueue.invokeLater ( runnable );
}
}
输出