2013-07-02 22 views
1

我正在編寫一個程序,該程序將涉及用戶按下一個將生成隨機數的按鈕。該數字將決定在JLabel上顯示哪些文本。程序的工作方式是主JFrame擁有一個名爲「Work」的按鈕,該按鈕用一個名爲「Get a new job」的按鈕打開另一個JFrame,並顯示帶有結果的JLabel。我似乎無法將包含隨機生成的數字的變量從數字生成器類傳遞給「Work」按鈕的ActionListener類。另外,如何將文本保存到JLabel中,以便如果我使用JLabel從該JFrame退出並重新打開該文本,它將顯示它在我關閉它之前所具有的文本,並且不會重置爲默認的沒有文本。謝謝,讓我知道你是否需要更多細節。Java如何從ActionListener類繼承變量類

主要類:

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

可能重複[Java的:我如何傳遞變量從JButton的ActionListener添加到主類](HTTP:// stackoverflow.com/questions/16888423/java-how-do-i-pass-variables-from-jbutton-actionlistener-to-main-class) –

+0

無法識別您的作業,因爲您聲明爲局部變量,沒有範圍內,只需將'actionPerformed'聲明爲'protected int job;' – nachokk

回答

1

我用簡單的技術,因爲模式如果有需要使回調父類 - 子類必須擴展與一些方法來引用父。家長必須實施孩子將用來表示父母的新接口。 對於這個例子,我會嘗試添加接口到基於按鈕的新類。然後創建一個新的動作監聽器類,它將構造函數中的button作爲參數作爲參數,這樣您就可以獲得它的起源。

但是,無視以上所寫 - 這是模式,這是你的情況不需要。這裏是ActionEvent其直接去到的ActionListener並持有事件來源: http://www.daniweb.com/software-development/java/threads/196917/the-e.getsource-method-from-actionevent-e-in-a-listener

1

不認可你的工作,因爲你被聲明爲局部變量,有外界方法沒有範圍,只是聲明outisde的actionPerformed爲protected int job;

順便提一些建議:

  • 撥打frame.setVisible(true)在最後。

  • 2- public class workButtonAction extends numGeneratorAction implements ActionListener爲什麼要擴展它?如果你擴展你的 不需要實現ActionListener導致父實現 它。

  • 3-不要在頂級類中實現ActionListener我更喜歡使用 annonymus類或私有內部類。

  • 4-不要創建你在的ActionListener方法框架把他們當成 全局變量沒有本地變量

  • 5-關注的java code convetions(類的首字母應該
    大寫)

現在在你的代碼:

創建包含框架類

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(); 
       } 
     }); 

    } 
}