gpt4 book ai didi

Java:如何将变量从 JButton ActionListener 传递到主类?

转载 作者:行者123 更新时间:2023-12-02 21:10:38 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,当单击按钮时生成一个随机数,然后将输出显示在屏幕上。但是,我无法将保存随机数的变量传递到带有 JLabel 的类中,以便可以在该类中使用它。我编写了一个尽可能简单的程序来演示:

import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main{
public static void main(String[] args){

JFrame mainFrame = new JFrame("Experiment");
mainFrame.setSize(500,500);
mainFrame.setVisible(true);

Panel panel = new Panel();
mainFrame.getContentPane().add(panel);

JLabel output = new JLabel("This is where the result from the num variable in the numGenerator class would go");
panel.add(output);

JButton numGenerator = new JButton("Generate Number");
panel.add(numGenerator);
numGenerator.addActionListener(new numGenerator());

}

static class numGenerator implements ActionListener{
public void ActionPerfomed (ActionEvent e){

int num; //This is the variable I want to be passed to the
//Main class so it can be displayed in the "output" Jlabel.

Random dice = new Random();
num = dice.nextInt(3);

}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}
}
}

我在网上看到其他帮助来创建该类的对象,然后在您想要该变量所在的类中使用它,但我无法在这种情况下让它工作。

最佳答案

您可以使用多种方法来完成此任务...

你可以...

创建一个类、实例变量可供numGenerator直接访问...

public class Main{
public static void main(String[] args){
new Main();
}

// This variable will be visible to the inner class numGenerator
private JLabel output;

public Main() {
JFrame mainFrame = new JFrame("Experiment");
mainFrame.setSize(500,500);
mainFrame.setVisible(true);

Panel panel = new Panel();
mainFrame.getContentPane().add(panel);

output = new JLabel("This is where the result from the num variable in the numGenerator class would go");
panel.add(output);

JButton numGenerator = new JButton("Generate Number");
panel.add(numGenerator);
numGenerator.addActionListener(new numGenerator());
}

public class numGenerator implements ActionListener{
public void actionPerformed(ActionEvent e){

Random dice = new Random();
int num = dice.nextInt(3);
output.setText(Integer.toString(num));

}
}
}

这将您的操作与标签紧密结合在一起,从而降低了代码的可重用性...

你可以...

将要更改的标签的引用传递给numGenerator...

public class Main{
public static void main(String[] args){
new Main();
}

public Main() {
JFrame mainFrame = new JFrame("Experiment");
mainFrame.setSize(500,500);
mainFrame.setVisible(true);

Panel panel = new Panel();
mainFrame.getContentPane().add(panel);

JLabel output = new JLabel("This is where the result from the num variable in the numGenerator class would go");
panel.add(output);

JButton numGenerator = new JButton("Generate Number");
panel.add(numGenerator);
numGenerator.addActionListener(new numGenerator(output));
}

public class numGenerator implements ActionListener{
private JLabel label;

public numGenerator(JLabel label) {
this.label = label;
}

public void actionPerformed(ActionEvent e){

Random dice = new Random();
int num = dice.nextInt(3);
if (label != null) {
label.setText(Integer.toString(num));
}

}
}
}

这使得 numGenerator 更加可重用,因为它不依赖于 JLabel 的单个实例

你可以...

使用 Observer style pattern这可以告诉一些感兴趣的团体已经生成了一个新的号码......

public class Main{
public static void main(String[] args){
new Main();
}

public Main() {
JFrame mainFrame = new JFrame("Experiment");
mainFrame.setSize(500,500);
mainFrame.setVisible(true);

Panel panel = new Panel();
mainFrame.getContentPane().add(panel);

final JLabel output = new JLabel("This is where the result from the num variable in the numGenerator class would go");
panel.add(output);

JButton numGenerator = new JButton("Generate Number");
panel.add(numGenerator);
numGenerator.addActionListener(new numGenerator(new NumberGeneratorListener() {
public void numberGenerated(int number) {
output.setText(Integer.toString(number));
}
}));
}

public interface NumberGeneratorListener {
public void numberGenerated(int number);
}

public class numGenerator implements ActionListener{
private NumberGeneratorListener listener;

public numGenerator(NumberGeneratorListener listener) {
this.listener = listener;
}

public void actionPerformed(ActionEvent e){

Random dice = new Random();
int num = dice.nextInt(3);
if (listener != null) {
listener.numberGenerated(num);
}

}
}
}

这不仅将 numGenerator 与代码的其余部分分离,因为它不依赖于代码的任何其他部分,而且使它非常可重用,因为它不关心数字在哪里去向或如何使用它,由观察者/听众决定......

旁注...

您可能还想通读...

关于Java:如何将变量从 JButton ActionListener 传递到主类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16888423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com