JAVA中的JTabbedPane问题

时间:2011-05-13 07:33:22

标签: java swing jtabbedpane

HY!

我想创建一个带有jTable的TabbedPane。

设计: enter image description here

JForm中的构造函数代码:

    initComponents();
    this.add(jTabbedPane1);
    jTabbedPane1.addTab("Test", jPanel1);
    jPanel1.add(jTable1);

自动根代码:

private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton_add = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setMinimumSize(new java.awt.Dimension(550, 300));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"dddsds", 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"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        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(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jButton_add.setText("add");
        jButton_add.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton_add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_addActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 579, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jButton_add)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton_add)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

问题:

jTable1不可见,按钮位于jTabbedPane下,而不是jPanel下。

请帮忙

2 个答案:

答案 0 :(得分:2)

您不应将jTable1两次添加到jPanel1initComponents()已通过使用jScrollPane1jTable1(包括jPanel1)添加到jPanel1Layout。但是在您的构造函数中,您第二次将jTable1显式添加到jPanel1。因此,您的构造函数应该如下所示:

initComponents();
this.add(jTabbedPane1);
jTabbedPane1.addTab("Test", jPanel1);

请注意,我删除了行jPanel1.add(jTable1);

然后你遇到了与jTabbedPane1相同的问题,它被添加到内容窗格两次。

从我看到的生成代码中,我会说你可能对Netbeans GUI设计器进行了一些不好的操作,因为jTable1不在jTabbedPane1内,而是直接在框架的内容窗格内。我想你应该从头开始重新设计你的面板,或者至少在设计师中修改你的UI。

需要理解的重点是,当您使用GroupLayout API时,组件会自动添加到其容器中,因此不应过时添加。

实际上,我最好的建议是:不要使用NetBeands UI设计器并手动编写UI代码(但不能使用GroupLayout,因为它太复杂而无法手动编码)。

答案 1 :(得分:1)

可能有多种原因: 1. jPanel1布局不正确 2. this是错误的容器。 3. jTable1未正确初始化。

只需发布更多代码(如果可能,sscce

编辑: 您应该添加jScrollPane1而不是表 另请参阅order layout.setHorizo​​ntalGroup() 你添加了jPanel1,JTabbedPane1,JButton_add。