初学JAVA,在做个界面练习
我在JScrollPane里面放了个JTable,然后想去掉JTable的外边框试了直接table.setBorder(null);无效,然后试了下table.setBorder(new EmptyBorder(new Insets(10,10,10,10));无效,貌似放在JScrollPane里面的表格都无法设置外边框于是乎我想这边框应该是JScrollPane的,但是我就是想不明白,我已经写了scrollPane.setBorder(null);而且JViewport又是无边框的,为毛会多个边框啊= =望高手指教
测试代码如下:
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
private static final long serialVersionUID=20110124L;
public Test() {
Container container=this.getContentPane();

this.setBounds(200, 200, 200, 600);

JScrollPane scrollPane=new JScrollPane();
scrollPane.setBorder(null);
JViewport viewport=scrollPane.getViewport();
viewport.setBackground(Color.WHITE);
container.add(scrollPane);

String[] columnNames={"aa","bb"};
String[][] value={{"11","22"},{"33","44"}};
JTable table=new JTable(value,columnNames);
table.setShowGrid(false);
scrollPane.setViewportView(table);

this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test();
}}

解决方案 »

  1.   


    public class Test extends JFrame
    {
    private static final long serialVersionUID = 20110124L; public Test()
    {
    Container container = this.getContentPane(); this.setBounds(0, 0, 600, 600); JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBorder(new CompoundBorder(new TitledBorder("scrollPane的外边框"),
    new EmptyBorder(8, 8, 8, 8)));
    JViewport viewport = scrollPane.getViewport();
    viewport.setBackground(Color.WHITE);

    container.add(scrollPane); String[] columnNames = { "aa", "bb" };
    String[][] value = { { "11", "22" }, { "33", "44" } };
    JTable table = new JTable(value, columnNames);
    table.setShowGrid(false);
    scrollPane.setViewportView(table); this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } public static void main(String[] args)
    {
    new Test();
    }}
      

  2.   

    ……很明白地告诉了我这边框是JScrollPane的,但是要如何去除这边框……………………为啥NULL无效
      

  3.   

    哦,对了,可以家Empty,可以了但是为啥NULL无效…………
      

  4.   

    看了下sun源码
    if (border == null || oldBorder == null ||
                    !(border.getBorderInsets(this).equals(oldBorder.getBorderInsets(this)))) {
                    revalidate();
                } 
    好像是border是null的话,它会给一个默认的、、、