我怎样才能在从 jbutton 事件初始化时更改变量值?
how can i change variable value in initialize from jbutton event?
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.WEST);
JButton btnNewButton = new JButton("New button");
frame.getContentPane().add(tes, BorderLayout.NORTH);
int n = 6;
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//here iwant to change n value
}
}
});
}
我想在多个按钮的初始化中更改变量值(如果我单击任何按钮,此变量将更改)
将 n 设为全局,这样它就永远不会被释放。然后在 actionPerformed 中,您可以根据需要处理 n。
在您的示例中,n 是局部初始化的,因此它将在函数终止后丢失。
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.WEST);
JButton btnNewButton = new JButton("New button");
frame.getContentPane().add(tes, BorderLayout.NORTH);
int n = 6;
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//here iwant to change n value
}
}
});
}
我想在多个按钮的初始化中更改变量值(如果我单击任何按钮,此变量将更改)
将 n 设为全局,这样它就永远不会被释放。然后在 actionPerformed 中,您可以根据需要处理 n。
在您的示例中,n 是局部初始化的,因此它将在函数终止后丢失。