[英]Java How to inherit variables from ActionListener Classes
我正在编写一个程序,其中涉及用户按下将生成随机数的按钮。 该数字将确定JLabel上将显示什么文本。 该程序的工作方式是主JFrame包含一个名为“ Work”的按钮,该按钮将打开另一个JFrame,并带有一个名为“ Get a new job”的按钮,并显示带有结果的JLabel。 我似乎无法将保存从数字生成器类中随机生成的数字的变量传递给“工作”按钮ActionListener类。 另外,我将如何在JLabel上保存文本,以便如果我退出带有JLabel的JFrame并重新打开它,它将显示关闭前的文本,而不重置为默认值no text。 谢谢,让我知道您是否需要更多详细信息。
主班:
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);
}
}
如果需要回调父类,我将使用简单的技术作为模式-必须使用某些方法扩展子类以保留对父类的引用。 父级必须实现子级将用来向父级发出信号的新接口。 对于此示例,我将尝试根据按钮向新类添加接口。 然后创建一个新的动作侦听器类,该类将按钮作为对象作为其构造函数中的参数,以便您可以了解按钮的来源。
但是,请忽略上面写的内容-模式,这在您的情况下是不需要的。 有一个ActionEvent可直接转到ActionListener并保存事件源: http : //www.daniweb.com/software-development/java/threads/196917/the-e.getsource-method-from-actionevent-e-in -a监听
无法识别您的作业,因为您将其声明为局部变量,在方法之外没有作用域,仅声明过时的动作执行为protected int job;
BTW一些建议:
最后调用frame.setVisible(true)
。
2- public class workButtonAction extends numGeneratorAction implements ActionListener
为何要扩展它? 如果扩展,则无需实现ActionListener,因为父级可以实现它。
3-不要在顶级类中实现ActionListener,我更喜欢使用匿名类或私有内部类。
4-不要在actionListener方法中创建框架,而是将其作为全局变量而不是局部变量
5-遵循Java 代码要求 (类的首字母应
被大写)
现在在您的代码中:
创建一个包含框架的类
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);
}
}
}
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();
}
}
在你的主班
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.