我试图制作一个程序,当单击一个按钮然后在屏幕上显示输出时,它将生成一个随机数。但是,我无法使用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
}
}
}
Run Code Online (Sandbox Code Playgroud)
I saw other help online to create an object of the class and then use that in the class you want the variable to be in, but I couldn't get that to work in this situation.
There are a number of methods that you could use to accomplish this...
Make a class, instance variable available for the numGenerator to access directly...
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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
This tightly couples you action with your label, making the code less reusable...
Pass a reference of the label to be changed to the 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));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
This makes the numGenerator more reusable, as it's not reliant on a single instance of the JLabel
Use a Observer style pattern that could tell some interested party that a new number has being generated...
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不仅使numGenerator代码与其余代码脱钩,因为它不依赖于代码的任何其他部分,而且还使其具有极高的可重用性,因为它不在乎数字的位置或使用方式,这取决于观察员/听众决定...
旁注...
您可能还需要通读...
| 归档时间: |
|
| 查看次数: |
13552 次 |
| 最近记录: |