我正在编写一个程序,该程序将涉及用户按下将生成随机数的按钮。该数字将决定JLabel上将显示的文本。程序的工作方式是主JFrame包含一个名为“Work”的按钮,该按钮用一个名为“获取新作业”的按钮打开另一个JFrame,并显示带有结果的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);
}
}
答案 0 :(得分:1)
如果需要对父类进行回调,我使用简单的技术作为模式 - 必须使用某些方法扩展子类以保持对父类的引用。父必须实现孩子用来向父母发信号的新界面。 对于此示例,我将尝试基于按钮将接口添加到新类。然后创建一个新的动作侦听器类,它将按钮作为对象作为其构造函数中的参数,这样您就可以获得它的原点。
但是,不管上面写的是什么 - 这是模式,在你的情况下是不需要的。 ActionEvent直接进入ActionListener并保存事件源: http://www.daniweb.com/software-development/java/threads/196917/the-e.getsource-method-from-actionevent-e-in-a-listener
答案 1 :(得分:1)
无法识别您的工作,因为您声明为局部变量,没有范围外的方法,只需声明outisde actionPerformed为protected int job;
BTW一些建议:
最后致电frame.setVisible(true)
。
2- public class workButtonAction extends numGeneratorAction
implements ActionListener
为什么要扩展这个?如果你扩展你
不需要实现ActionListener导致父实现
它
3-不要在我喜欢使用的顶级类中实现ActionListener annonymus类或私人内部类。
4-不要在actionListener方法中创建框架 全局变量无局部变量
5-遵循java code convetions(班级的第一个字母应该是 是UpperCase)
现在在你的代码中:
创建一个包含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);
}
}
}
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();
}
});
}
}