为什么JSplitPane会为我的组件添加边框,如何阻止它?

时间:2012-10-09 12:05:23

标签: java swing border jsplitpane

JSplitPane似乎为添加到其中的任何Component添加了边框。

对于嵌套的JSplitPanes,这是最明显的 - 例如:

public class JSplitPaneToy {

  public static void main(String[] args) {
    JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
      makePanel(), makePanel());
    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);

    JFrame frame = new JFrame("JSplitPane Toy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(sp);
    frame.pack();
    frame.setVisible(true);
  }

  private static JScrollPane makePanel() {
    JScrollPane pane = new JScrollPane(new JTable(
      new Object[][]{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}}, new Object[]{1, 2, 3}));
    pane.setPreferredSize(new Dimension(200, 100));
    return pane;
  }
}

即。每个后续的嵌套组件似乎都被设置得更远 - 即添加了某种形式的阴影边框。

  • 为什么要添加此边框? (这实际上是一个边界被添加......?)
  • 如何防止添加此“边框”?

5 个答案:

答案 0 :(得分:6)

如果要在所有JSplitPane上删除这些边框,可以像这样更改UI的默认值。但是,我通常尽量不要弄乱UI默认值。

import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JSplitPaneToy {

    public static void main(String[] args) {
        UIManager.getDefaults().put("SplitPane.border", BorderFactory.createEmptyBorder());
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JSplitPaneToy().initUI();
            }
        });
    }

    public void initUI() {
        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), makePanel());
        sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
        sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
        sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);

        JFrame frame = new JFrame("JSplitPane Toy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(sp);
        frame.pack();
        frame.setVisible(true);
    }

    private JScrollPane makePanel() {
        JScrollPane pane = new JScrollPane(new JTable(new Object[][] { { 0, 1, 2 }, { 1, 2, 3 }, { 2, 3, 4 } }, new Object[] { 1, 2, 3 }));
        pane.setPreferredSize(new Dimension(200, 100));
        return pane;
    }
}

您可能需要查看SwingX项目的JXMultiSplitPane,而不是嵌套这么多的分割窗格。

答案 1 :(得分:3)

我们使用此方法“展平”JSplitPane。也许这就是你要找的东西:

/**
 * Makes a split pane invisible. Only contained components are shown.
 *
 * @param splitPane
 */
public static void flattenJSplitPane(JSplitPane splitPane) {
    splitPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    BasicSplitPaneUI flatDividerSplitPaneUI = new BasicSplitPaneUI() {
        @Override
        public BasicSplitPaneDivider createDefaultDivider() {
            return new BasicSplitPaneDivider(this) {
                @Override
                public void setBorder(Border b) {
                }
            };
        }
    };
    splitPane.setUI(flatDividerSplitPaneUI);
    splitPane.setBorder(null);
}

至于为什么要添加边框,不知道。显然它是某种功能。我们发现它是一个不需要的,上面的方法可以解决它。可能有一种更简单的方法可以解决这个问题,但是,当你找到一些有效的东西时,你就会停止寻找替代方案。

答案 2 :(得分:2)

我用它来重置分隔符的边框。

    SplitPaneUI ui = sp.getUI();
    if( ui instanceof BasicSplitPaneUI ) {
        ((BasicSplitPaneUI)ui).getDivider().setBorder( null );
    }

注意:您的示例无效splitPane未知,应为sp

public class JSplitPaneToy {

  public static void main(String[] args) {
    JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT,  makePanel(), makePanel());
    SplitPaneUI ui = sp.getUI();

    if( ui instanceof BasicSplitPaneUI ) {
        ((BasicSplitPaneUI)ui).getDivider().setBorder( null );
    }
    sp.setBorder( BorderFactory.createEmptyBorder() );

    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
    ui = sp.getUI();
    if( ui instanceof BasicSplitPaneUI ) {
        ((BasicSplitPaneUI)ui).getDivider().setBorder( null );
    }
    sp.setBorder( BorderFactory.createEmptyBorder() );

    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
    ui = sp.getUI();
    if( ui instanceof BasicSplitPaneUI ) {
        ((BasicSplitPaneUI)ui).getDivider().setBorder( null );
    }
    sp.setBorder( BorderFactory.createEmptyBorder() );

    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
    ui = sp.getUI();
    if( ui instanceof BasicSplitPaneUI ) {
        ((BasicSplitPaneUI)ui).getDivider().setBorder( null );
    }
    sp.setBorder( BorderFactory.createEmptyBorder() );

    JFrame frame = new JFrame("JSplitPane Toy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(sp);
    frame.pack();
    frame.setVisible(true);
  }

  private static JScrollPane makePanel() {
    JScrollPane pane = new JScrollPane(new JTable(
      new Object[][]{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}}, new Object[]{1, 2, 3}){
    });
    pane.setPreferredSize(new Dimension(200, 100));
    return pane;
  }
}

答案 3 :(得分:0)

另一个答案是将Component上的每个JSplitPane设置为空边框 - 例如

JComponent a = ...;
JComponent b = ...; 
a.setBorder(BorderFactory.createEmptyBorder());
b.setBorder(BorderFactory.createEmptyBorder());
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, a, b);

答案 4 :(得分:0)

只需通过JScrollPane覆盖setBroder

public class MyScrollPane extends JScrollPane {
 ...
 @Override
 public void setBorder(Border b) {}
}