有如下代码:public class TestBorder extends JFrame{
public TestBorder()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(9,9));
panel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.blue));
JLabel label[] = new JLabel[81];
for(int i=0;i<label.length;i++)
{
label[i]=new JLabel(""+i);
label[i].setBorder(BorderFactory.createLineBorder(Color.red));
panel.add(label[i]);
}
this.add(panel);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setBounds(new Rectangle(245,345));
//this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String args[])
{
new TestBorder();
}}为什么panel的最右边还会有一些空白的区域呢?
运行结果为:
public TestBorder()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(9,9));
panel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.blue));
JLabel label[] = new JLabel[81];
for(int i=0;i<label.length;i++)
{
label[i]=new JLabel(""+i);
label[i].setBorder(BorderFactory.createLineBorder(Color.red));
panel.add(label[i]);
}
this.add(panel);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setBounds(new Rectangle(245,345));
//this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String args[])
{
new TestBorder();
}}为什么panel的最右边还会有一些空白的区域呢?
运行结果为:
要实现没有空白,必须得设置label的大小,然后frame使用pack函数去控制窗口的大小,或者自己手动计算窗口的大小