jScrollPane中的jPanel不起作用

时间:2016-12-22 23:57:34

标签: java swing jscrollpane

嗨我想在jScrollPane中插入面板,如列表水平,但面板不尊重我设置的边界,这是我的代码

public PrincipalPanel() {
        initComponents();
        for(int i=0;i<3;i++){
            addItem(new ItemCurso(),i);
        }
    }

    public void addItem(ItemCurso item,int index){
        item.setVisible(true);
        item.setBounds(400*index, 0, 400, 300);
        jScrollPane1.getViewport().add(item,null);
    }

ItemCurso从jPanel扩展,我设置的最后一个ItemCurso获取jScrollpane的大小并且滚动不起作用

ItemCurso的代码是下一个:

public class ItemCurso extends javax.swing.JPanel {

    public ItemCurso() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    private void initComponents() {

 jLabel1 = new javax.swing.JLabel();


 jPanel1 = new javax.swing.JPanel();
    nombreTxt = new javax.swing.JLabel();
    escuelaTxt = new javax.swing.JLabel();
    bimestresComboBox = new javax.swing.JComboBox();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jButton6 = new javax.swing.JButton();
    jButton7 = new javax.swing.JButton();

jLabel1.setText("jLabel1");

setBackground(new java.awt.Color(229, 232, 221));
setPreferredSize(new java.awt.Dimension(100, 100));

jPanel1.setBackground(new java.awt.Color(255, 255, 255));

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 100, Short.MAX_VALUE)
);

nombreTxt.setText("Nombre");

escuelaTxt.setText("Escuela");

bimestresComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Cristian_Franco\\Documents\\Sithec\\Proyectos\\Proyecto Escuelas\\SITCEA\\mavenproject1\\Images\\Resources\\Recurso 14.png")); // NOI18N

jButton2.setIcon(new javax.swing.ImageIcon("C:\\Users\\Cristian_Franco\\Documents\\Sithec\\Proyectos\\Proyecto Escuelas\\SITCEA\\mavenproject1\\Images\\Resources\\Recurso 28.png")); // NOI18N

jButton3.setIcon(new javax.swing.ImageIcon("C:\\Users\\Cristian_Franco\\Documents\\Sithec\\Proyectos\\Proyecto Escuelas\\SITCEA\\mavenproject1\\Images\\Resources\\Recurso 27.png")); // NOI18N

jButton4.setIcon(new javax.swing.ImageIcon("C:\\Users\\Cristian_Franco\\Documents\\Sithec\\Proyectos\\Proyecto Escuelas\\SITCEA\\mavenproject1\\Images\\Resources\\Recurso 29.png")); // NOI18N

jButton5.setIcon(new javax.swing.ImageIcon("C:\\Users\\Cristian_Franco\\Documents\\Sithec\\Proyectos\\Proyecto Escuelas\\SITCEA\\mavenproject1\\Images\\Resources\\Recurso 30.png")); // NOI18N

jButton6.setIcon(new javax.swing.ImageIcon("C:\\Users\\Cristian_Franco\\Documents\\Sithec\\Proyectos\\Proyecto Escuelas\\SITCEA\\mavenproject1\\Images\\Resources\\Recurso 31.png")); // NOI18N

jButton7.setIcon(new javax.swing.ImageIcon("C:\\Users\\Cristian_Franco\\Documents\\Sithec\\Proyectos\\Proyecto Escuelas\\SITCEA\\mavenproject1\\Images\\Resources\\Recurso 32.png")); // NOI18N

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(bimestresComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(nombreTxt)
                                    .addComponent(escuelaTxt)))
                            .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))))
        .addContainerGap())
);
layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(nombreTxt)
                .addGap(18, 18, 18)
                .addComponent(escuelaTxt))
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(18, 18, 18)
        .addComponent(bimestresComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(18, 18, 18)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(18, 18, 18)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
            .addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addComponent(jButton6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
);        

}// </editor-fold>   




    // Variables declaration - do not modify                     
    private javax.swing.JComboBox bimestresComboBox;
    private javax.swing.JLabel escuelaTxt;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel nombreTxt;
    // End of variables declaration                   
} 

1 个答案:

答案 0 :(得分:2)

jScrollPane1.getViewport().add(item,null);

您无法添加3&#34;项目&#34;到视口。您只能将单个组件添加到视口中。

所以你的逻辑应该是这样的:

JPanel panel = new JPanel( ); // specify your layout manager here

for(int i=0;i<3;i++)
{
    panel.add( new ItemCurso() );
}

jScrollPane1.getViewport().add(panel);

默认情况下,JPanel使用FlowLayout,因此所有组件都会在同一行水平显示。

您需要在ItemCurso类中实现getPreferredSize()方法,以确保它返回Dimension(400,300)。不要尝试使用setBounds(...)。