Netbeans 中的 jsplitpane

发布于 2024-09-27 21:29:09 字数 3243 浏览 2 评论 0原文

我尝试在 Netbeans 6.9.1 中使用 JSplitPane,但由于某种原因无法正确配置它。例如 我在 jframe 中添加一个 jtree 和一个 jtable。然后我使用“包含在 splitpane 中”。 然后我将方向设置为垂直(从水平)。
问题是,当我尝试调整分隔线位置时,无法将其放置在我想要的位置。要么一路上涨 或一直向下。手动扩展一侧后,我得到了接近我想要的格式。 我尝试做同样的事情,并在相同的 jform 中创建第二个 jsplitpane。 然后我尝试将两个 jsplitpane 包含到另一个 splitpane 中,但 splitpane 的顺序颠倒了。 我做错了什么或者分割面板不能正常工作吗?

谢谢

更新

public class Testing extends javax.swing.JFrame {

    /** Creates new form Testing */
    public Testing() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jSplitPane1.setDividerLocation(5);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane2.setViewportView(jTable1);

        jSplitPane1.setLeftComponent(jScrollPane2);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTree1.setName("jTree1"); // NOI18N
        jScrollPane1.setViewportView(jTree1);

        jSplitPane1.setRightComponent(jScrollPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(258, 258, 258))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

I am trying to use a JSplitPane in Netbeans 6.9.1, but I can not configure it properly for some reason. E.g.
I add a jtree and a jtable in a jframe. Then I use "enclose in splitpane".
Then I set the orientation to vertical (from horizontal).
Problem, when I try to adjust the divider location, I can not place it where I want. Either it goes all the way up
or all the way down. After manually expanding one side, I get a format close to what I want.
I try to do the same, and create a second jsplitpane in the same jform.
Then I try to enclose both jsplitpane into another splitpane but the order of the splitpanes get reversed.
Am I doing something wrong or splitpanes are not working ok?

Thanks

UPDATE

public class Testing extends javax.swing.JFrame {

    /** Creates new form Testing */
    public Testing() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jSplitPane1.setDividerLocation(5);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane2.setViewportView(jTable1);

        jSplitPane1.setLeftComponent(jScrollPane2);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTree1.setName("jTree1"); // NOI18N
        jScrollPane1.setViewportView(jTree1);

        jSplitPane1.setRightComponent(jScrollPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(258, 258, 258))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2024-10-04 21:29:09

如果您希望以相反的方式使用它们,只需在选择将它们包含在 JSplitPane 中之前交换树和表的位置即可。话虽如此,如果 NetBeans 允许您选择左/右组件,即使在完成此操作后,我也不会感到惊讶。

至于分隔线,我通常使用 setDividerLocation(Double location) 方法,该方法采用 0.0 到 1.0 之间的值。这是分隔线应位于的位置的百分比。

您使用的方法 setDividerLocation(intlocation) 设置像素值中的绝对位置,我怀疑 5 并不是您真正想要的。它还不允许您将分隔线设置到一个会使组件小于其首选尺寸的位置,当您将其设置为 5 时,这肯定会发生。

因此,如果您的屏幕高 400 像素,并且您想将其分割成中间将分隔线设置为 200。

推荐阅读 如何使用 Split Java 教程中的窗格

If you want them the other way around just swap places of the tree and table before you choose to enclose them in a JSplitPane. Having said that it would not surprise me if NetBeans lets you choose the left/right components even after having done this.

As for the divider I'd normally use the setDividerLocation(Double location) method which takes a value between 0.0 and 1.0. This works as a percentage of where the divider should be located.

The method you are using setDividerLocation(intlocation) sets the absolute position in pixel values and I suspect that 5 is not really what you want. It also wont let you set the divider into a location that would make a component smaller than its preferred size and this is most certainly what happens when you set it to 5.

So if your screen is 400 pixels high and you want to split it in the middle set the divider to 200.

Recommended reading is How to Use Split Panes from the Java Tutorial trail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文