1

我正在编写一个程序,该程序将涉及用户按下将生成随机数的按钮。该数字将决定 JLabel 上将显示什么文本。该程序的工作方式是主 JFrame 包含一个名为“Work”的按钮,该按钮打开另一个 JFrame,其中包含一个名为“Get a new job”的按钮,并显示 JLabel 和结果。我似乎无法将保存从数字生成器类中随机生成的数字的变量传递给“工作”按钮 ActionListener 类。另外,我将如何将文本保存在 JLabel 上,以便如果我使用 JLabel 退出该 JFrame 并重新打开它,它将显示在我关闭它之前的文本,而不是重置为无文本的默认值。谢谢,如果您需要更多详细信息,请告诉我。

主类:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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;
import javax.swing.JPanel;



public class Main{

            public static void main(String [] args){

                JFrame main = new JFrame("This is the real life!");
                main.setSize(500,500);
                main.setResizable(false);
                main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                main.setVisible(true);

                JPanel mainPanel = new JPanel(new GridBagLayout());
                main.getContentPane().add(mainPanel, BorderLayout.NORTH);
                GridBagConstraints c = new GridBagConstraints();

                JLabel mainText = new JLabel("This is your life.");
                c.gridx = 50;
                c.gridy = 50;
                c.insets = new Insets(50,10,10,10);
                mainPanel.add(mainText, c);

                JButton workButton = new JButton("Work");
                c.gridx = 50;
                c.gridy = 75;
                c.insets = new Insets(150,10,10,10);
                mainPanel.add(workButton, c);
                workButton.addActionListener(new workButtonAction());

            }
}

“工作”按钮 ActionListener 类:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class workButtonAction extends numGeneratorAction implements ActionListener{

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

        JFrame workFrame = new JFrame("Your job");
        workFrame.setSize(250,250);
        workFrame.setResizable(false);
        workFrame.setVisible(true);

        JPanel workPanel = new JPanel();
        workFrame.add(workPanel);

        JButton numGenerator = new JButton("Get a new job.");
        workPanel.add(numGenerator);
        numGenerator.addActionListener(new numGeneratorAction());

        JLabel Job = new JLabel();

        numGeneratorAction generatorObject = new numGeneratorAction();
        generatorObject.actionPerformed(e);

        if(job == 0){ //The job variable is not recognized in this class.
            Job.setText("Text will go here.");
        }
    }

}

数字生成器类:

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

public class numGeneratorAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        int job;

        Random dice = new Random();
        job = dice.nextInt(4);
        System.out.println(job);

    }

}
4

2 回答 2

1

如果需要对父类进行回调,我使用简单的技术作为模式 - 必须使用一些方法扩展子类以保持对父类的引用。父级必须实现子级将用来向父级发出信号的新接口。对于此示例,我将尝试基于按钮将接口添加到新类。然后创建一个新的动作监听器类,它将按钮作为对象作为其构造函数中的参数,这样你就可以知道它的起源。

但是,忽略上面写的 - 那是模式,在你的情况下不需要。有 ActionEvent 直接进入 ActionListener 并保存事件源: http ://www.daniweb.com/software-development/java/threads/196917/the-e.getsource-method-from-actionevent-e-in -a-listener

于 2013-07-02T23:40:29.567 回答
1

无法识别您的工作,因为您声明为局部变量,在方法之外没有范围,只需将 outisde actionPerformed 声明为protected int job;

BTW 一些建议:

  • 最后打电话frame.setVisible(true)

  • 2-public class workButtonAction extends numGeneratorAction implements ActionListener为什么要扩展这个?如果你扩展你不需要实现 ActionListener 因为父实现它。

  • 3- 不要在顶级类中实现 ActionListener 我更喜欢使用匿名类或私有内部类。

  • 4- 不要在 actionListener 方法中创建你的框架,把它们作为全局变量而不是局部变量

  • 5-遵循java代码约定(类的第一个字母应该
    是大写)

现在在您的代码中:

创建一个包含 Frame 的类

public class MainFrame {

private JFrame main;
private JPanel mainPanel;
private JLabel mainText;
private JButton workButton;

public MainFrame(){
                main = new JFrame("This is the real life!");
                main.setSize(500,500);
                main.setResizable(false);
                main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


                mainPanel = new JPanel(new GridBagLayout());
                main.getContentPane().add(mainPanel, BorderLayout.NORTH);
                GridBagConstraints c = new GridBagConstraints();

                mainText = new JLabel("This is your life.");
                c.gridx = 50;
                c.gridy = 50;
                c.insets = new Insets(50,10,10,10);
                mainPanel.add(mainText, c);

                workButton = new JButton("Work");
                c.gridx = 50;
                c.gridy = 75;
                c.insets = new Insets(150,10,10,10);
                mainPanel.add(workButton, c);
                workButton.addActionListener(new WorkButtonAction());
                main.pack();
                main.setVisible(true);
}


private class WorkButtonAction implements ActionListener {
       @override
       public void actionPerformed(ActionEvent evt){
               // here create a JDialog instead of a JFrame
                 JDialog dialog = new MyDialog(main,true);
                 dialog.setVisible(true);
       }


}

}

对话框

public class MyDialog extends JDialog{

private JPanel workPanel;
private JButton numGenerator;
private JLabel jlabel;
private Random dice = new Random();

public MyDialog(){

        setTitle("Your job");
        setSize(250,250);
        setResizable(false);
        workPanel = new JPanel();
        add(workPanel);
        numGenerator = new JButton("Get a new job.");
        workPanel.add(numGenerator);

       numGenerator.addActionListener(new NumGeneratorAction(){
                @Override
                public void actionPerformed(ActionEvent evt){
                      int job = dice.nextInt(4);
                      if(job == 0)
                            jobLabel.setText("Text will go here.");
                }

       });

        jobLabel = new JLabel();

}




}

在你的主要课程中

public class Main {
  public static void main(String args []){
       SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run(){
                  new MainFrame();
                }
         });

  }
}
于 2013-07-03T00:11:33.427 回答