0

我有将 oneTouchExpandable 设置为 true 的 JSplitPane。

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane.setDividerSize(10);
        splitPane.setOneTouchExpandable(true);

问题是我不知道如何将键绑定附加到 JSplitPane 分隔器上的向上和向下箭头。对于向上箭头,我想要 Ctrl+U,向下箭头 - Ctrl + D。

谢谢!

4

2 回答 2

3

显示的箭头按钮的实现OneTouchExpandable是 UI 标签,并且会花费不必要的额外工作来绑定它们。JSplitPane您可以轻松地在自身上使用 Key Binding来JSplitPane使用setDividerLocation(int). 在 上增加Ctrl + U和减少Ctrl + D。例如:

    Action incrDividerLoc = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        JSplitPane srcSplitPan = (JSplitPane) e.getSource();
        (srcSplitPan).setDividerLocation(srcSplitPan.getDividerLocation()+10);
     }
   };

  Action decrDividerLoc = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        JSplitPane srcSplitPan = (JSplitPane) e.getSource();
        (srcSplitPan).setDividerLocation(srcSplitPan.getDividerLocation()-10);
    }
   };

    jSplitPane1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.CTRL_DOWN_MASK),
                                "increaseDivider");
    jSplitPane1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_DOWN_MASK),
                                "decreaseDivider");
    jSplitPane1.getActionMap().put("increaseDivider", incrDividerLoc);
    jSplitPane1.getActionMap().put("decreaseDivider", decrDividerLoc);

注意: 方法传递给的值小于 0setDividerLocation(int) 意味着应将分隔符重置为尝试遵守左/上组件的首选大小的值。通知听众后,最后一个分隔线位置通过 更新setLastDividerLocation

于 2013-11-01T14:53:06.567 回答
3

问题是我不知道如何将键绑定附加到 JSplitPane 分隔器上的向上和向下箭头。

通常您会尝试访问按钮的操作。在许多情况下,组件已经定义了您可以使用的 Action。有关JSplitPane 的默认绑定列表,请参阅键绑定。不幸的是,没有支持一键点击选项的操作。

所以我们需要直接从 UI 访问按钮:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class SplitPaneDividerAction extends AbstractAction
{
    private boolean leading;

    public SplitPaneDividerAction(boolean leading)
    {
        this.leading = leading;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        JSplitPane splitPane = (JSplitPane)e.getSource();
        BasicSplitPaneUI ui = (BasicSplitPaneUI)splitPane.getUI();
        BasicSplitPaneDivider divider = ui.getDivider();

        if (leading)
            ((JButton)divider.getComponent(0)).doClick();
        else
            ((JButton)divider.getComponent(1)).doClick();
    }

    private static void createAndShowUI()
    {
        JPanel leading = new JPanel();
        leading.setPreferredSize( new Dimension(200, 100) );
        leading.setBackground( Color.BLUE );
        leading.setFocusable(true);

        JPanel trailing = new JPanel();
        trailing.setPreferredSize( new Dimension(200, 100) );
        trailing.setBackground( Color.RED );
        trailing.setFocusable(true);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leading, trailing);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(100);

        InputMap im = splitPane.getInputMap(JSplitPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        ActionMap am = splitPane.getActionMap();
        im.put(KeyStroke.getKeyStroke("control U"), "leading");
        im.put(KeyStroke.getKeyStroke("control D"), "trailing");
        am.put("leading", new SplitPaneDividerAction(true));
        am.put("trailing", new SplitPaneDividerAction(false));

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( splitPane );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

当然,这种方法只有在您的 LAF 从 BasicSplitPaneUI 扩展时才有效。

于 2013-11-01T16:02:20.523 回答