这是另一种解决方案,可能有点脏,但它有效;)我希望代码不言自明。
public class ExtOneTouchJSplitPane extends JSplitPane {
private static final long serialVersionUID = -2320161961382260438L;
JButton jBLeftUp;
JButton jBRightDown;
public ExtOneTouchJSplitPane() {
super();
setOneTouchExpandable(true);
extractDividerButtons();
}
public ExtOneTouchJSplitPane(int newOrientation) {
super(newOrientation);
setOneTouchExpandable(true);
extractDividerButtons();
}
public ExtOneTouchJSplitPane(int newOrientation, boolean newContinuousLayout) {
super(newOrientation, newContinuousLayout);
setOneTouchExpandable(true);
extractDividerButtons();
}
public ExtOneTouchJSplitPane(int newOrientation, boolean newContinuousLayout, Component newLeftComponent, Component newRightComponent) {
super(newOrientation, newContinuousLayout, newLeftComponent, newRightComponent);
setOneTouchExpandable(true);
extractDividerButtons();
}
public ExtOneTouchJSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent) {
super(newOrientation, newLeftComponent, newRightComponent);
setOneTouchExpandable(true);
extractDividerButtons();
}
private void extractDividerButtons() {
BasicSplitPaneUI ui = (BasicSplitPaneUI) getUI();
jBLeftUp = (JButton) ui.getDivider().getComponent(0);
jBRightDown = (JButton) ui.getDivider().getComponent(1);
}
public void oneTouchClickLeft() {
jBLeftUp.doClick();
}
public void oneTouchClickRight() {
jBRightDown.doClick();
}
public void oneTouchClickUp() {
jBLeftUp.doClick();
}
public void oneTouchClickDown() {
jBRightDown.doClick();
}
}
以及如何使用它的示例:
public class SplitPaneDemo extends JFrame implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new SplitPaneDemo());
}
ExtOneTouchJSplitPane hSplitPane;
ExtOneTouchJSplitPane vSplitPane;
public SplitPaneDemo() {
createView();
}
public void createView() {
setTitle("SplitPane-Demo");
setLayout(new BorderLayout(0, 0));
hSplitPane = new ExtOneTouchJSplitPane();
JButton jBLeft = new JButton("<html><body> <br>Left Component<br> </body></html>");
JButton jBRight = new JButton("<html><body> <br>Right Component<br> </body></html>");
jBLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hSplitPane.oneTouchClickLeft();
}
});
jBRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hSplitPane.oneTouchClickRight();
}
});
hSplitPane.setLeftComponent(jBLeft);
hSplitPane.setRightComponent(jBRight);
add(hSplitPane, BorderLayout.CENTER);
vSplitPane = new ExtOneTouchJSplitPane(JSplitPane.VERTICAL_SPLIT);
JButton jBUp = new JButton("<html><body> <br>Up Component<br> </body></html>");
JButton jBDown = new JButton("<html><body> <br>Down Component<br> </body></html>");
jBUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
vSplitPane.oneTouchClickUp();
}
});
jBDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
vSplitPane.oneTouchClickDown();
}
});
vSplitPane.setTopComponent(jBUp);
vSplitPane.setBottomComponent(jBDown);
add(vSplitPane, BorderLayout.SOUTH);
}
@Override
public void run() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
hSplitPane.oneTouchClickLeft();
}
}