java - 如何自定义 JSplitPane 分隔线并保持一键式箭头功能?

标签 java swing jsplitpane

所以,我的问题归结为这个...默认分隔线有点难看,而且我想给它添加一个标签(在我想要文本的意义上,而不是在“将 JLabel 添加到其布局“感觉”)。我看到您可以更改拆分 Pane 分隔线上的边框,但是当我这样做时,它会删除我想保留的一键式箭头。

关于如何同时拥有两者的任何想法?

这是一个 SSCCE,它演示了默认行为以及更改分隔符边框时发生的情况:

import javax.swing.*;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SplitPaneFun {
public static void main(String[] args) {

        //Here I'm messing around with the divider look. This seems to remove the one-touch arrows.  These blocked-out lines illustrate
        // what I'm doing to modify the divider's border.  Does this look right?:
    //------------------------------------------------------------------------------------------------
    JSplitPane withCustomDivider = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JPanel(), new JPanel());
    BasicSplitPaneDivider divider = ( (BasicSplitPaneUI) withCustomDivider.getUI()).getDivider();
    withCustomDivider.setOneTouchExpandable(true);
    divider.setDividerSize(15);
    divider.setBorder(BorderFactory.createTitledBorder(divider.getBorder(), "Custom border title -- gets rid of the one-touch arrows!"));
    //------------------------------------------------------------------------------------------------

        //build a different splitpane with the default look and behavior just for comparison
    JSplitPane withDefaultDivider = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JPanel(), new JPanel());
    withDefaultDivider.setOneTouchExpandable(true);

        //slap it all together and show it...
    CardLayout splitsLayout = new CardLayout();
    final JPanel splits = new JPanel(splitsLayout);
    splits.add(withCustomDivider, "custom");
    splits.add(withDefaultDivider,"default");

    JButton toggle = new JButton( "click to see the other split pane");
    toggle.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ((CardLayout)splits.getLayout()).next(splits);
        }
    });

    JFrame frame = new JFrame("Split Pane Divider Comparison");
     frame.setLayout(new BorderLayout());
    frame.add(splits, BorderLayout.CENTER);
    frame.add(toggle, BorderLayout.PAGE_END);
    frame.setSize(600,500);
    frame.setVisible(true);
}
}

最佳答案

I see that you can change the border on the split pane divider, but when I do that, it removes the one-touch arrows, which I want to keep around.

  • 所有这些方法都可以覆盖来自 Mouse(Xxx)ListenerPropertyChangeListenerButtonModel 的事件,没有比这更好的了作为物质 SubstanceSplitPaneDivider

  • Custom Look and Feels 的一部分|也覆盖这些方法

关于java - 如何自定义 JSplitPane 分隔线并保持一键式箭头功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11057175/

相关文章:

java - 将字符串拆分为不同部分的正则表达式(使用 Java)

java - RecyclerView 的动态部分标题使用当前日期时间

java - 从 mysql 检索数据到 JTable

java - 是否有任何工具/软件可以分离 Java Swing GUI 逻辑和业务逻辑?

java - 无滚动条 - 带 JPanel 的 JScrollPane

java - 在 JSplitPane 上设置分隔线位置不起作用

java - 这个模式有什么问题吗?

java - 如何在 Java 中调用 DateJS 的 Date.parse()?

java - 是否可以强制 JButton 始终为正方形?

java - 将 JSplitPane 与 AWT 组件一起使用