保留两个 JPanel 之间变量的值

retain the value of a variable between two JPanel

我想在我的两个 JPanel 之间存储变量 (id_Category),因为我需要在第二个 JPanel 上处理它的值 这是第一个 JPanel

public class PreparerTest extends JPanel
int idCategorie;
public void mouseClicked(MouseEvent e) {
            idCategorie = categories.get(table.getSelectedRow()).getId();

第二个 JPanel

public class PreparerTestManuelle extends JPanel {
JButton btnAfficher = new JButton("Afficher");
    btnAfficher.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            questions=GestionTestDelegate.doPrepareManuallyTest(the id is supposed to be here, nombreQuestion.getText());

        }
    });

两个 JPanel 是否在同一个 class 中?如果是,您应该创建如下字段:public int idCategorie;

而且您可以在 class 任何地方使用它