[英]Java: How do I pass variables from JButton ActionListener to main class?
我正在嘗試制作一個程序,當單擊一個按鈕時生成一個隨機數,然后輸出顯示在屏幕上。 但是,我無法通過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
的單個實例
使用觀察者樣式模式可以告訴一些感興趣的一方已經生成了一個新號碼...
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
與其余代碼分離,因為它不依賴於代碼的任何其他部分,它使得它非常可重用,因為它不關心數字的去向或使用方式,這取決於觀察者/聽眾決定...
旁注...
您可能還需要通讀...
首先,如果您要訪問JLabel必須在LEVEL CLASS聲明中或在actionListener中通過參數傳遞,則導致ur標簽引用僅存在於主上下文中。
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(output));
}
private class NumGenerator implements ActionListener{
private final JLabel label;
public NumGenerator(final JLabel label){
this.label=label;
}
@Override
public void actionPerformed(ActionEvent arg0) {
int num;
Random dice = new Random();
num = dice.nextInt(3);
label.setText(num);
}
}
}
或者你可以做這樣的事情
public class Main{
//if u want to hold all at class level properties but u really only interested in JLabel
private JFrame mainFrame;
private JPanel panel;
private JLabel label;
private JButton numGenerator;
//add Constructor
public Main(){
mainFrame = new JFrame("Experiment");
mainFrame.setSize(500,500);
mainFrame.setVisible(true);
panel = new JPanel();
mainFrame.getContentPane().add(panel);
output = new JLabel();
panel.add(output);
numGenerator = new JButton("Generate Number");
panel.add(numGenerator);
numGenerator.addActionListener(new NumGenerator(output));
}
public static void main(String[] args){
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().mainFrame.setVisible(true);
}
});
}
private class NumGenerator implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
int num;
Random dice = new Random();
num = dice.nextInt(3);
label.setText(num); // now u have access
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.