[英]Java How to inherit variables from ActionListener Classes
I am writing a program that will involve a user pressing a button that will generate a random number. 我正在编写一个程序,其中涉及用户按下将生成随机数的按钮。 That number will determine what text will display on a JLabel. 该数字将确定JLabel上将显示什么文本。 The way the program works is that the main JFrame holds a button called "Work", which opens another JFrame with a button called "Get a new job" and displays the JLabel with the result. 该程序的工作方式是主JFrame包含一个名为“ Work”的按钮,该按钮将打开另一个JFrame,并带有一个名为“ Get a new job”的按钮,并显示带有结果的JLabel。 I can't seem to pass the variable that holds the randomly generated number from the number generator class to the "Work" buttons ActionListener class. 我似乎无法将保存从数字生成器类中随机生成的数字的变量传递给“工作”按钮ActionListener类。 Also, how would I save the text on the JLabel so that if I exited out of that JFrame with the JLabel and re-opened it, it would display the text it had before I closed it and not reset to the default of no text. 另外,我将如何在JLabel上保存文本,以便如果我退出带有JLabel的JFrame并重新打开它,它将显示关闭前的文本,而不重置为默认值no text。 Thanks, let me know if you need more details. 谢谢,让我知道您是否需要更多详细信息。
Main class: 主班:
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());
}
}
"Work" Button ActionListener class: “工作”按钮的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.");
}
}
}
Number Generator Class: 编号生成器类:
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);
}
}
I use simple technique as pattern if there is need to make callback to parent class - child class must be extended with some methods to hold reference to parent. 如果需要回调父类,我将使用简单的技术作为模式-必须使用某些方法扩展子类以保留对父类的引用。 Parent must implement new interface the child will use to signal parent. 父级必须实现子级将用来向父级发出信号的新接口。 For this example i would try to add interface to new class based on button. 对于此示例,我将尝试根据按钮向新类添加接口。 then make a new class of action listener which takes button as object as argument in its constructor so you will have the origin of it. 然后创建一个新的动作侦听器类,该类将按钮作为对象作为其构造函数中的参数,以便您可以了解按钮的来源。
But, disregard what is written above - that is pattern, which is not needed in your case. 但是,请忽略上面写的内容-模式,这在您的情况下是不需要的。 There is ActionEvent which goes straight to the ActionListener and holds source of event: http://www.daniweb.com/software-development/java/threads/196917/the-e.getsource-method-from-actionevent-e-in-a-listener 有一个ActionEvent可直接转到ActionListener并保存事件源: http : //www.daniweb.com/software-development/java/threads/196917/the-e.getsource-method-from-actionevent-e-in -a监听
is not recognized your job cause you are declaring as local variable, has no scope outside method, just declare outisde actionPerformed as protected int job;
无法识别您的作业,因为您将其声明为局部变量,在方法之外没有作用域,仅声明过时的动作执行为protected int job;
BTW Some advices: BTW一些建议:
Call frame.setVisible(true)
at the end. 最后调用frame.setVisible(true)
。
2- public class workButtonAction extends numGeneratorAction implements ActionListener
Why you extend this? 2- public class workButtonAction extends numGeneratorAction implements ActionListener
为何要扩展它? If you extend you don't need to implements ActionListener cause parent implements it. 如果扩展,则无需实现ActionListener,因为父级可以实现它。
3- Don't implement ActionListener in top classes i prefer using annonymus classes or private inner classes. 3-不要在顶级类中实现ActionListener,我更喜欢使用匿名类或私有内部类。
4- Don't create your frame in actionListener method put them as global variable no local variable 4-不要在actionListener方法中创建框架,而是将其作为全局变量而不是局部变量
5- Follow java code convetions (first letter of classes should 5-遵循Java 代码要求 (类的首字母应
be UpperCase) 被大写)
Now in your code: 现在在您的代码中:
Create a class that holds the 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);
}
}
}
The JDialog JDialog
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();
}
}
and in your Main class 在你的主班
public class Main {
public static void main(String args []){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new MainFrame();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.