我有一个用 JSplitPane 划分内容的 Java GUI 程序。JSplitPane 的左侧是一个带有许多选项卡的 JTabbedPane - 一个非常复杂的布局。当左侧达到一定复杂程度时,拖动分隔线移动分隔线位置不再起作用,但我仍然可以通过显式设置位置来移动分隔线。(如果重要的话,我正在使用 Nimbus LAF。)
它似乎不仅仅是左侧的选项卡数量。我在左侧包含的某些选项卡使其停止工作,而其他选项卡则正常。
有没有人遇到过这个?
我可以通过在我的 JSplitPane 子类上添加一个 hack 变通方法来解决它。
public void enableDividerWorkaround() {
javax.swing.plaf.basic.BasicSplitPaneUI l_ui = (javax.swing.plaf.basic.BasicSplitPaneUI) getUI();
BasicSplitPaneDivider l_divider = l_ui.getDivider();
l_divider.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
Dimension l_pane_size = getSize();
if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
int l_new_loc = getDividerLocation() + e.getX();
if (l_new_loc >= 0 && l_new_loc <= l_pane_size.width) {
setDividerLocation(l_new_loc);
}
} else {
int l_new_loc = getDividerLocation() + e.getY();
if (l_new_loc >= 0 && l_new_loc <= l_pane_size.height) {
setDividerLocation(l_new_loc);
}
}
}
});
}
更新这里是 SSCCE(下)。当我运行它时,第一次向右拖动滑块时,它会“捕捉”到长标签的末尾,然后保持固定在那里。我相信它是由长标签触发的。如果我缩短标签,我会在滑块上获得更大的运动范围。那么它是一个错误,还是预期的行为?
public class SplitPaneTest extends javax.swing.JFrame {
public SplitPaneTest() {
initComponents();
}
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jSplitPane1.setDividerLocation(450);
jSplitPane1.setName("jSplitPane1");
jLabel1.setText("right side");
jLabel1.setName("jLabel1");
jSplitPane1.setRightComponent(jLabel1);
jPanel1.setName("jPanel1");
jLabel2.setText("left side asd adsf asdf asdf asdf sadf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdfa end");
jLabel2.setName("jLabel2");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addContainerGap(420, Short.MAX_VALUE))
);
jSplitPane1.setLeftComponent(jPanel1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSplitPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSplitPane1)
);
pack();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SplitPaneTest().setVisible(true);
}
});
}
protected javax.swing.JLabel jLabel1;
protected javax.swing.JLabel jLabel2;
protected javax.swing.JPanel jPanel1;
protected javax.swing.JSplitPane jSplitPane1;
}