如何使用mouseClicked在循环内获取JLabel的值?

时间:2019-03-28 07:01:28

标签: java swing jlabel mouselistener

我修改了我的简单程序,而不是使用表,而是使用JPanel创建了自定义代码来显示我的列表。我现在的问题是,当我单击按钮时,它给了我最后一个列表的值。

这是我的方法,如下所示:

private void customize(List<Student> students) {
    panelStudentHolder.removeAll();
    panelStudentHolder.revalidate();
    panelStudentHolder.repaint();
    for (Student student : students) {
        panelStudentHolder.setLayout(new java.awt.GridLayout(0, 1, 0, 0));
        panelStudentHolder.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Student List", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Century Gothic", 1, 14))); // NOI18N
        panelStudent = new javax.swing.JPanel();
        lblStudent = new javax.swing.JLabel();

        btnBackUp = new javax.swing.JLabel();
        btnEdit = new javax.swing.JLabel();
        btnBlock = new javax.swing.JLabel();

        lblStudent.setFont(new java.awt.Font("Century Gothic", 0, 14)); // NOI18N
        lblStudent.setText(student.toString()+" >> "+student.getId());

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/icons8-view-20.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                System.out.println(" "+lblStudent.getText());
            }
        });


        javax.swing.GroupLayout panelStudentLayout = new javax.swing.GroupLayout(panelStudent);
        panelStudent.setLayout(panelStudentLayout);
        panelStudentLayout.setHorizontalGroup(
        panelStudentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelStudentLayout.createSequentialGroup()
            .addComponent(lblStudent, javax.swing.GroupLayout.DEFAULT_SIZE, 740, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addComponent(btnBackUp)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(btnEdit)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(btnBlock)
            .addGap(2, 2, 2))
        );
        panelStudentLayout.setVerticalGroup(
            panelStudentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblStudent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnBackUp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnEdit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnBlock, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );


        panelStudentHolder.add(panelStudent);

    }
}

这是我的问题发生的地方

btnView.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                System.out.println(" "+lblStudent.getText());

//在这里,它始终为我提供列表的最后一个值。                 }             });

When ever I clicked the buttons it always gives me the ID of the bottom one.

1 个答案:

答案 0 :(得分:0)

对于第二部分, 您可以将背景颜色设置为面板,

为了缩小, 为应该解决第二个问题的面板提供最小尺寸,最大尺寸和首选尺寸

您还可以使用方框布局作为我的帮助。