如何让按钮用完 BorderLayout 中的所有 space,不留下白色 space?
How do I make a button use up all of the space within a BorderLayout leaving no white space?
我有一个程序可以创建一个用随机颜色填充的 8x8 网格(已经完成了这一点),我应该在底部添加一个按钮,它应该填充整个底部,用于重置颜色。我已经有了网格和按钮,但是,我的按钮并没有填满底部的整个 space,只有大约一半。如何让它填满整个 space?
我的代码:
public void createGUI()
{
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Label demo");
JPanel mainPanel = new JPanel();
JPanel gridPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton refreshButton = new JButton("Press me to refresh labels");
mainPanel.setLayout(new BorderLayout());
guiFrame.getContentPane().add(mainPanel);
gridPanel.setLayout(new GridLayout(8, 8));
for (int i = 0; i < arrayLabels.length; i++)
{
arrayLabels[i] = new ColorLabel(80, 80, new Color(rand.nextInt()), 0, new Color(rand.nextInt()));
}
for (int i = 0; i < 8*8; i++)
{
gridPanel.add(arrayLabels[i]);
}
guiFrame.getContentPane().add(gridPanel);
buttonPanel.add(refreshButton);
guiFrame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
guiFrame.setVisible(true);
}
我的程序目前的输出是这样的:https://imgur.com/a/fw1Bx92
同时设置刷新按钮的JPanel布局!下面我举个例子大家可以看看:
public static void createGUI()
{
JFrame guiFrame = new JFrame("a");
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Label demo");
JPanel mainPanel = new JPanel();
JPanel gridPanel = new JPanel();
JPanel buttonPanel = new JPanel(new BorderLayout());
JButton refreshButton = new JButton("Press me to refresh labels");
mainPanel.setLayout(new BorderLayout());
guiFrame.getContentPane().add(mainPanel);
gridPanel.setLayout(new GridLayout(8, 8));
JLabel arrayLabels[] = new JLabel[64];
for (int i = 0; i < 64; i++)
{
arrayLabels[i] = new JLabel("a");
}
for (int i = 0; i < 8*8; i++)
{
gridPanel.add(arrayLabels[i]);
}
guiFrame.getContentPane().setLayout(new BorderLayout());
guiFrame.getContentPane().add(gridPanel,BorderLayout.CENTER);
buttonPanel.add(refreshButton,BorderLayout.CENTER);
guiFrame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
guiFrame.setVisible(true);
}
我有一个程序可以创建一个用随机颜色填充的 8x8 网格(已经完成了这一点),我应该在底部添加一个按钮,它应该填充整个底部,用于重置颜色。我已经有了网格和按钮,但是,我的按钮并没有填满底部的整个 space,只有大约一半。如何让它填满整个 space?
我的代码:
public void createGUI()
{
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Label demo");
JPanel mainPanel = new JPanel();
JPanel gridPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton refreshButton = new JButton("Press me to refresh labels");
mainPanel.setLayout(new BorderLayout());
guiFrame.getContentPane().add(mainPanel);
gridPanel.setLayout(new GridLayout(8, 8));
for (int i = 0; i < arrayLabels.length; i++)
{
arrayLabels[i] = new ColorLabel(80, 80, new Color(rand.nextInt()), 0, new Color(rand.nextInt()));
}
for (int i = 0; i < 8*8; i++)
{
gridPanel.add(arrayLabels[i]);
}
guiFrame.getContentPane().add(gridPanel);
buttonPanel.add(refreshButton);
guiFrame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
guiFrame.setVisible(true);
}
我的程序目前的输出是这样的:https://imgur.com/a/fw1Bx92
同时设置刷新按钮的JPanel布局!下面我举个例子大家可以看看:
public static void createGUI()
{
JFrame guiFrame = new JFrame("a");
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Label demo");
JPanel mainPanel = new JPanel();
JPanel gridPanel = new JPanel();
JPanel buttonPanel = new JPanel(new BorderLayout());
JButton refreshButton = new JButton("Press me to refresh labels");
mainPanel.setLayout(new BorderLayout());
guiFrame.getContentPane().add(mainPanel);
gridPanel.setLayout(new GridLayout(8, 8));
JLabel arrayLabels[] = new JLabel[64];
for (int i = 0; i < 64; i++)
{
arrayLabels[i] = new JLabel("a");
}
for (int i = 0; i < 8*8; i++)
{
gridPanel.add(arrayLabels[i]);
}
guiFrame.getContentPane().setLayout(new BorderLayout());
guiFrame.getContentPane().add(gridPanel,BorderLayout.CENTER);
buttonPanel.add(refreshButton,BorderLayout.CENTER);
guiFrame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
guiFrame.setVisible(true);
}