我找到了JXMultiSplitPane 的 javadoc,但我一无所知。我将如何使用它来显示 5 个水平排列的 JPanel,其间有分隔器?
问问题
3161 次
1 回答
4
它不能准确回答您的问题,但我希望它对您有所帮助。
请查看这段代码。代码取自2007 年的 swinglabs 演示文稿。加载需要一段时间,请耐心等待。
//Simple case: creates a split pane with three
//compartments
JXMultiSplitPane sp = new JXMultiSplitPane();
sp.setModel(new DefaultSplitPaneModel());
sp.add(left, DefaultSplitPaneModel.LEFT);
sp.add(top, DefaultSplitPaneModel.TOP);
sp.add(bottom, DefaultSplitPaneModel.BOTTOM);
编辑: 我理解你的挫败感。我希望看到swingx 的所有网站从java.net 的bl**dy“崩溃”之前恢复到以前的状态。这么多好的项目现在很难看到。
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout.Divider;
import org.jdesktop.swingx.MultiSplitLayout.Leaf;
import org.jdesktop.swingx.MultiSplitLayout.Split;
public class JXMultiSplitPaneTest extends JPanel
{
private static final long serialVersionUID = 1L;
public JXMultiSplitPaneTest()
{
//Simple case: creates a split pane with three compartments
JXMultiSplitPane sp = new JXMultiSplitPane();
JPanel p1 = new JPanel();
p1.setBackground(Color.PINK);
JPanel p2 = new JPanel();
p2.setBackground(Color.YELLOW);
JPanel p3 = new JPanel();
p3.setBackground(Color.CYAN);
JPanel p4 = new JPanel();
p4.setBackground(Color.RED);
JPanel p5 = new JPanel();
p5.setBackground(Color.BLUE);
sp.setModel(new FiveHorizontalSplitPaneModel(true));
sp.add(p1, FiveHorizontalSplitPaneModel.P1);
sp.add(p2, FiveHorizontalSplitPaneModel.P2);
sp.add(p3, FiveHorizontalSplitPaneModel.P3);
sp.add(p4, FiveHorizontalSplitPaneModel.P4);
sp.add(p5, FiveHorizontalSplitPaneModel.P5);
setLayout(new BorderLayout());
add(sp);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JXMultiSplitPaneTest p = new JXMultiSplitPaneTest();
JFrame f = new JFrame();
f.setContentPane(p);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}
class FiveHorizontalSplitPaneModel extends Split
{
//5 possible positions
public static final String P1 = "1";
public static final String P2 = "2";
public static final String P3 = "3";
public static final String P4 = "4";
public static final String P5 = "5";
public FiveHorizontalSplitPaneModel()
{
this(false);
}
public FiveHorizontalSplitPaneModel(boolean isEqualyWeighted)
{
setRowLayout(true);
Leaf p1 = new Leaf(P1);
Leaf p2 = new Leaf(P2);
Leaf p3 = new Leaf(P3);
Leaf p4 = new Leaf(P4);
Leaf p5 = new Leaf(P5);
if(isEqualyWeighted)
{
p1.setWeight(0.2);
p2.setWeight(0.2);
p3.setWeight(0.2);
p4.setWeight(0.2);
p5.setWeight(0.2);
}
setChildren(p1, new Divider(), p2, new Divider(),
p3, new Divider(), p4, new Divider(), p5);
}
}
检查他们的代码并从那里开始是非常有帮助的。DefaultSplitPaneModel 的代码对我非常有用。我相信你自己会设法做到这一点。毕竟,正如您在模型中看到的那样,这并不难。该模型可以用三行编码,但我添加了等权重功能。
乐于助人。玩得开心 :)
我建议您下载该库的代码、文档和 jar,并将它们捆绑到一个 NetBeans 库中。然后,每当您想查看例如他们如何编写特定类时,您按住CTRL
并在类名上单击鼠标,它将带您进入其实现。始终对所有开源库执行此操作。在开始时这样做可以在将来节省大量时间。如果您需要帮助,我很乐意提供帮助。
于 2011-05-25T09:40:53.900 回答