Java 如何摆脱 JTable / JScrollPane 的边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3333623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get rid of the border with a JTable / JScrollPane
提问by sproketboy
If you run the small sample below you'll see a border around the center region. I'm not sure why this border is showing.
如果您运行下面的小示例,您将看到中心区域周围有一个边框。我不确定为什么会显示此边框。
It happens when a JTable is in a JScrollPane. I tried various things to remove it but so far no luck. A JTable without the JScrollPane shows no border.
当 JTable 在 JScrollPane 中时会发生这种情况。我尝试了各种方法来删除它,但到目前为止没有运气。没有 JScrollPane 的 JTable 不显示边框。
See sample below. TIA.
请参阅下面的示例。TIA。
public class TestScrollPane extends JFrame {
public static void main(String[] args) {
JFrame frame = new TestScrollPane();
JPanel panel = new JPanel();
JTable table = new JTable();
panel.setLayout(new BorderLayout());
panel.add(new JLabel("NORTH"), BorderLayout.NORTH);
panel.add(new JLabel("SOUTH"), BorderLayout.SOUTH);
JScrollPane sp = new JScrollPane(table);
// None of these have any effect
sp.setBorder(null);
sp.getInsets().set(0, 0, 0, 0);
sp.setViewportBorder(null);
sp.getViewport().setBorder(null);
sp.getViewport().getInsets().set(0, 0, 0, 0);
sp.getViewport().setOpaque(true);
panel.add(sp, BorderLayout.CENTER);
// Adding the table alone shows no border
// panel.add(table, BorderLayout.CENTER);
frame.add(panel);
frame.setVisible(true);
}
public TestScrollPane() throws HeadlessException {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(100, 100));
}
}
采纳答案by sly7_7
Use BorderFactory.createEmptyBorder() instead of null...
使用 BorderFactory.createEmptyBorder() 而不是 null ...
by using:
通过使用:
sp.setBorder(createEmptyBorder());
it works.
有用。
Your main method becomes:
您的主要方法变为:
public static void main(String[] args) {
JFrame frame = new TestScrollPane();
JPanel panel = new JPanel();
JTable table = new JTable();
panel.setLayout(new BorderLayout());
panel.add(new JLabel("NORTH"), BorderLayout.NORTH);
panel.add(new JLabel("SOUTH"), BorderLayout.SOUTH);
JScrollPane sp = new JScrollPane(table);
sp.setBorder(BorderFactory.createEmptyBorder());
panel.add(sp, BorderLayout.CENTER);
frame.add(panel);
frame.setVisible(true);
}
回答by Guillaume
Interestingly the border disappears when you remove this line:
有趣的是,当您删除此行时,边框会消失:
sp.setBorder(null);
回答by RSC
I think the proper fix is to set the border on the viewportView to 'null'.
我认为正确的解决方法是将 viewportView 上的边框设置为“null”。
回答by Krishna Gupta
I was looking for the answer for the same question but above answers could not do... so I found a better answer:
我正在寻找同一问题的答案,但上述答案无法做到……所以我找到了更好的答案:
JScrollPane jsp = new JScrollPane();
//ur other codes
jsp.setViewportBorder(null);
回答by Gaurav vijayvargiya
For JTable table.setIntercellSpacing(new Dimension(0, 0))
works.
对于 JTabletable.setIntercellSpacing(new Dimension(0, 0))
作品。