以下代码表示问题.由于我有南北面板的高度设置,其余部分使用GridLayout进入中心面板.我认为,因为它不能在它的行之间平等地共享剩余像素,所以它只留下它们.因此,在下面的代码中,我们在南面板上有一条丑陋的白线.
我的问题是:如何确保GridLayout不占用整个空间时至少居中?
通常情况下,我会使用TableLayout并对情况进行排序,但由于我正在编写答案,因此我只想使用标准管理器.事先知道这对我来说非常有用.
例:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class AligningButonsTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame f = new JFrame();
f.setSize(800, 600);
double CONSTANT_FACTOR = .1;
int noOfRows = 5;
JPanel centerP = new JPanel(new GridLayout(noOfRows,1));
for(int i = 0; i < noOfRows; i++)
{
BoxPanel bP = new BoxPanel();
centerP.add(bP);
}
JPanel contentPane = new JPanel(new BorderLayout());
f.setContentPane(contentPane);
contentPane.add(centerP, BorderLayout.CENTER);
JPanel southP = new JPanel();
southP.setBackground(Color.RED.darker());//southP.setOpaque(false);
southP.setPreferredSize(new Dimension(1, (int)(CONSTANT_FACTOR* f.getHeight())));
contentPane.add(southP, BorderLayout.SOUTH);
JPanel northP = new JPanel();
northP.setBackground(Color.RED.darker());//northP.setOpaque(false);
northP.setPreferredSize(new Dimension(1, (int)(CONSTANT_FACTOR* f.getHeight())));
contentPane.add(northP, BorderLayout.NORTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}
class BoxPanel extends JPanel
{
public BoxPanel()
{
setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.RED));
setBackground(Color.DARK_GRAY);
}
}
解决方法:
BoxLayout在使用Box.createVerticalGlue()分配组件之间的空间方面做得非常好.此示例使用Box.createVerticalStrut(),top和bottom.间隔物在How to Use BoxLayout: Using Invisible Components as Filler中描述.
附录:BoxTest2是一种变体,它使用BoxLayout创建固定尺寸的边缘面板和垂直胶水,以更均匀地分布空间. Box.Filler也可用于控制“剩余”垂直空间.
/** @see https://stackoverflow.com/questions/6072956 */
public class BoxTest2 {
private static final int WIDE = 480;
private static final int HIGH = WIDE / 8;
private static final int ROWS = 5;
private static final Box center = new Box(BoxLayout.Y_AXIS);
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
center.setOpaque(true);
center.setBackground(Color.lightGray);
center.add(Box.createVerticalGlue());
center.add(new EdgePanel());
for (int i = 0; i < ROWS; i++) {
center.add(new BoxPanel());
}
center.add(new EdgePanel());
center.add(Box.createVerticalGlue());
f.add(center, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
private static class EdgePanel extends JPanel {
public EdgePanel() {
Dimension d = new Dimension(WIDE, 2 * HIGH / 3);
setPreferredSize(d);
setBackground(Color.red.darker());
}
}
private static class BoxPanel extends JPanel {
public BoxPanel() {
setPreferredSize(new Dimension(WIDE, HIGH));
setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.red));
setBackground(Color.darkGray);
}
}
}
标签:java,user-interface,layout,swing
来源: https://codeday.me/bug/20190723/1516112.html