2011-06-22 10 views
0

JTextFieldとMainというクラスのボタンがあります。 Actionという別のクラスにActionListenerがあります。私はActionListenerがJTextFieldを参照できるようにしたい。私はnullポインタの例外を取得し続けます。私はJTextFieldとActionListenerを別々にしておきたい。私は多くのActionListenersを持っているつもりです、そして、私がこのようにそれを整理することはもっと簡単でしょう。Java:別のクラスの変数をリフレッシュする1つのクラスのActionListenerの使用

public class Main { 

    public JTextField text; 

    public JTextField getText(){ 
     return this.text; 
    } 

public static void main(String[] args) { 

     Main main=new Main(); 
     main.blah(); 
    } 

public void blah(){ 

    JFrame myWindow=new JFrame("ff"); 
    myWindow.setSize(500,500); 
    myWindow.setVisible(true); 
    myWindow.setDefaultCloseOperation(3); 
    text=new JTextField(10); 
    JLabel lengthL = new JLabel("Enter a number",SwingConstants.CENTER ); 
    JButton button=new JButton("Click button"); 
    myWindow.getContentPane(); 
    myWindow.setLayout(new GridLayout(4,4)); 
    myWindow.add(lengthL); 
    myWindow.add(text); 
    myWindow.add(button); 

    Action hand=new Action(); 
    button.addActionListener(hand); 
} 
} 



public class Action implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     Main main=new Main(); 
     double length=Double.parseDouble(main.text.getText()); 
     System.out.println(length); 

    } 
} 

答えて

1

私はあなたがイベント扱うときに新しいMainインスタンスを作成しているためだnullポインタ例外

得続ける:

public void actionPerformed(ActionEvent e) { 
    Main main=new Main(); 
    ... 

あなたがより良い行動aを持っている必要がありますがコンテナクラス(Main)の参照とそのテキストフィールド値にアクセスするメソッド:

Main aMain; 
    public void actionPerformed(ActionEvent e) { 
    this.aMain.getText(); 
    .... 

あるいはさらに良い:

public void actionPerformed(ActionEvent e) { 
     double lenght = this.main.getValue();// returns double 
     ... 
+0

私はあなたが提案し何をやってますが、私はまだNULLポインタ – clay123

+1

を取得しています試してみました@粘土:あなたはそれを間違ってやっているのですが、私たちがもっと言いたいのは、あなたがオスカーの提案をどのように実装しようとしているのか、そしてどこにNPEがあるのか​​を示す必要があります。心を読むことはできません。 –

2

あなたはActionListenerを作成するときのJTextFieldを渡さないのはなぜ、それほどのように:

public class Action implements ActionListener{ 
    private JTextField text; 

    public Action(JTextField text) { 
     this.text = text; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     double length=Double.parseDouble(text.getText()); 
     System.out.println(length); 
    } 
} 

//in Main: 
public void blah(){ 
    JFrame myWindow=new JFrame("ff"); 
    myWindow.setSize(500,500); 
    myWindow.setVisible(true); 
    myWindow.setDefaultCloseOperation(3); 
    text=new JTextField(10); 
    JLabel lengthL = new JLabel("Enter a number",SwingConstants.CENTER ); 
    JButton button=new JButton("Click button"); 
    myWindow.getContentPane(); 
    myWindow.setLayout(new GridLayout(4,4)); 
    myWindow.add(lengthL); 
    myWindow.add(text); 
    myWindow.add(button); 

    Action hand=new Action(text); //change this line 
    button.addActionListener(hand); 
} 
+0

彼は望んでいるので、JTextFieldとActionListenerを分離した状態に保つために* * – OscarRyz

+0

@OscarRyz - ActionListenerに 'Main'インスタンスへの参照を与えることは、それらを別々に保つことではありません。その 'Main'クラスに関連するテキストフィールドでのみ有効です。テキストフィールドを渡すことで、ActionListenerはより再利用可能になります。これは、クラスを分離しておくことが達成されることに精神を保っています。 – aroth

+0

ありがとうございます。それは素晴らしい仕事でした! – clay123

関連する問題