5

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

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

  • 为什么要添加此边框?(这实际上是在添加边框吗...?)
  • 如何防止添加此“边框”?
4

5 回答 5

6

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

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,而不是嵌套这么多拆分窗格。

于 2012-10-09T13:03:02.437 回答
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);
}

至于为什么要添加边框,就不得而知了。显然它的某种功能。我们发现它是不受欢迎的,上面的方法可以解决它。可能有一种更简单的方法可以解决这个问题,但是,嘿,当你找到可行的方法时,你就停止寻找替代方案了。

于 2012-10-09T12:14:25.177 回答
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;
  }
}
于 2012-10-09T12:42:54.393 回答
0

另一种答案是将每个设置ComponentJSplitPane具有空边框 - 例如

JComponent a = ...;
JComponent b = ...; 
a.setBorder(BorderFactory.createEmptyBorder());
b.setBorder(BorderFactory.createEmptyBorder());
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, a, b);
于 2012-10-09T13:35:56.667 回答
0

只需像这样通过 JScrollPane 覆盖 setBroder

public class MyScrollPane extends JScrollPane {
 ...
 @Override
 public void setBorder(Border b) {}
}
于 2014-08-19T11:35:55.743 回答