为什么 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;
  }
}

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

  • 为什么要添加此边框?(这实际上是正在添加的边框吗?
  • 如何防止添加此"边框"?

如果要在所有 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,而不是嵌套这么多的 splitpane。

我们使用此方法"平展"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);
}

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

我用这个来重置分隔线的边框。

    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;
  }
}

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

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

只需像这样通过JScrollPane覆盖setBroder即可

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

相关内容

  • 没有找到相关文章

最新更新