内部循环中的MouseListener不使用更新的变量值

时间:2018-05-19 13:09:51

标签: java swing mouselistener

我想首先为我糟糕的英语道歉。 我制作了一些JPanels,我把它放在一个JPanel中,最后一个JPanel我把它放在一个JScrollPane中,每个Jpanel都有一些JLabel,里面有文字,我已经完成了,一切都工作到现在,然后我添加了一个mouseListener到System.out.print里面的JLabel文本。 文本是不同的,但在控制台上我得到相同的文字,这里是我的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testa;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout; 
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.event.*;

public class Test extends JFrame{ //see https://www.javatpoint.com/java-naming-conventions

    private JLabel[] titles;
    private JLabel[] descriptions;
    private JPanel [] panels;
    private JScrollPane jScrollPane1;
    private JPanel bigPanel;
    private final static int NUM_OF_RESULTS =10;
String identifier ;
String title;
String authors;
String resume;
String references;
    public Test() throws IOException {

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1000,500);            
        //jScrollPane1.setSize(1000, 500);    and layouts. see following comments

        bigPanel = new JPanel();
        bigPanel.setBounds(30, 90, 950, 400);
        //set layout to
        GridLayout layout = new GridLayout(NUM_OF_RESULTS, 0);
        bigPanel.setLayout(layout);
        jScrollPane1 = new JScrollPane(bigPanel);
        jScrollPane1.setBounds(30, 90, 950, 400);
        getContentPane().add(jScrollPane1);

        requetezQuery();
        //pack(); //see https://stackoverflow.com/questions/22982295/what-does-pack-do
        setVisible(true); //set visible typically comes last
    }
    public void requetezQuery() {

int actual=0;






        titles = new JLabel[NUM_OF_RESULTS];
        descriptions = new JLabel[NUM_OF_RESULTS];
        panels = new JPanel[NUM_OF_RESULTS];

        for(int i = 0; i<NUM_OF_RESULTS; i++){



                         title = "Title "+i;
                      resume = "Description "+i ;


            titles[i]= new JLabel();
            descriptions[i]= new JLabel();
            panels[i]= new JPanel();
            panels[i].setPreferredSize(new Dimension(250, 50));
            panels[i].setLayout(new FlowLayout()); //FlowLayout is default for JPanel
            titles[i].setText(title);

            descriptions[i].setText(resume.substring(0, Math.min(resume.length(), 100))+"...");
            titles[i].setForeground(Color.blue);
            descriptions[i].setForeground(Color.black);
            titles[i].addMouseListener(new MouseAdapter() { 
          public void mouseClicked(MouseEvent me) { 
            System.out.println(title);
          } 
        });
            panels[i].add(titles[i]);
            panels[i].add(descriptions[i]);
            bigPanel.add(panels[i],i, 0);

        }

    }


    public static void main(String args[]) throws IOException{
        new Test();
    }
}

2 个答案:

答案 0 :(得分:2)

我相信你对title的引用是指在运行时最后创建的标题。你可以做些什么来解决这个问题,而不是打印标题,打印标签文本。尝试将它放在mouseClicked方法中。

ActivityReport.update(
  { noteAlert: true },               //query
  { $set: { noteAlert: false}},      //update operator
  { new: true })                     // gives you new result
.exec(function (err, activityReports) {
   console.log(activityReports)
})

答案 1 :(得分:0)

来自外部类的鼠标单击事件引用变量:

    addMouseListener(new MouseAdapter() { 
      public void mouseClicked(MouseEvent me) { 
        System.out.println(title); // This `title` is a reference of Test.title variable
      } 
    });

因此,您的所有MouseAdapter引用了相同的title(其值被每个循环替换) - &gt; System.out.println全部打印相同的最后一个标题。