我试图将多个JLists放在JPanel中,JPanel放在JScrollPane中。问题是,当我将JScrollPane添加到方程中时,JList和外部边框之间有一个空格,如何去掉这个边框,使其水平填充?
下面是一个用JLabel而不是JList演示这个问题的小示例代码。
/edit:好像是windows,它在Mac .上运行得很好。
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
public static void main(String[] args)
{
Test test = new Test();
}
public Test()
{
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setContentPane(contentPane);
this.setMinimumSize(new Dimension(400, 300));
JScrollPane sp = new JScrollPane(createLeftPane());
sp.setBorder(BorderFactory.createLineBorder(Color.yellow));
this.add(sp, BorderLayout.WEST);
LookAndFeel.installBorder(sp, "BorderFactory.createEmptyBorder()");
StackTraceElement[] st = Thread.currentThread().getStackTrace();
for(int i = 0; i < st.length; i++) {
System.out.println(st[i]);
}
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private JPanel createLeftPane()
{
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JLabel label = new JLabel("TEST LABEL");
label.setBorder(BorderFactory.createLineBorder(Color.blue));
panel.add(label, c);
panel.setBorder(BorderFactory.createLineBorder(Color.red));
return panel;
}
}
这是一个屏幕截图(第一个没有滚动窗格)
堆栈跟踪:
java.lang.Thread.getStackTrace(Thread.java:1479)
test.Test.<init>(Test.java:27)
test.Test.main(Test.java:10)
/edit:经过一些尝试和错误之后,我发现当我在对比中添加c.weightx = 0.5时,它会水平地填充,但是当scrollPane变得大于它的内容时,它会使自己变得更宽,这是很奇怪的。见下面的截图:
发布于 2013-09-04 11:51:58
component.setBorder(null)
移除组件上设置的任何边框。
https://stackoverflow.com/questions/7687217
复制相似问题