2017-02-03 3 views
0

ここでは、以下のテキストを逆にするコードを示します。Javaメインフレームを使用して他のコンポーネントを取得する

TextFrameクラス私のGUIの入力および出力

import java.awt.GridLayout; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class InputOutputPanel extends JPanel { 

    private JTextField input; 
    private JTextField output; 
    private JTextField situation; 

    public InputOutputPanel() { 

     this.setLayout(new GridLayout(2, 2)); 
     this.add(new JLabel("header")); 
     this.add(situation = new JTextField("Situation")); 
     this.add(input = new JTextField("input text here")); 
     this.add(output = new JTextField()); 


    } 

    public void setSituation(String sit){ 
     situation.setText(sit); 

    } 

    public void setOutPut(String s){ 
    output.setText(s); 
    } 

    public String getInput(){ 
     return input.getText(); 


    } 
} 

コントロールクラス

を扱う

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 


public class TextFrame extends JFrame implements ActionListener{ 
    private Controls theControls; 
    private ReverseText theReverseText; 
    private InputOutputPanel theInputOutputPanel; 

    public TextFrame(){ 

    this.getContentPane().setLayout(new BorderLayout()); 

    theInputOutputPanel = new InputOutputPanel(); 
    theReverseText = new ReverseText(this); 
    theControls = new Controls(theReverseText); 

    this.getContentPane().add(theInputOutputPanel, BorderLayout.NORTH); 

    this.getContentPane().add(theControls, BorderLayout.SOUTH); 
    this.pack(); 
    this.setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent ae) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 


    public Controls getControls(){ 
    return this.theControls; 
    } 

    public InputOutputPanel getInputOutPanel(){ 
     return this.theInputOutputPanel; 

    } 

    public static void main(String[] args) { 
     // All we need to do is create the frame, as the constructor does everything required. 
     TextFrame theFrame = new TextFrame(); 
//  theFrame.setSize(150, 150); 
//  theFrame.setVisible(true); 
    } 

} 

InputOutputでクラスパネルのメインフレーム私のイベントハンドラ

import java.awt.*; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 

import java.awt.event.ActionListener; 

public class ReverseText implements ActionListener { 

    private TextFrame theTextFrame; 
    private InputOutputPanel inputOutPanel; 


    public ReverseText(TextFrame theTextFrame) { 
     this.theTextFrame = theTextFrame; 



    } 

    @Override 
    public void actionPerformed(ActionEvent event) { 
    String buttonAction = event.getActionCommand(); 

    // trying to use the mainframe to get other components 

    inputOutPanel = theTextFrame.getInputOutPanel(); // but this line complaining about null pointer error 

    String input = inputOutPanel.getInput(); 


    if (buttonAction.equals("Text-Reverse")) 
     System.out.println("yes"); 
     inputOutPanel.setSituation(buttonAction); 

     //Reverse The Text and send it to the Output 
     String reversedText = new StringBuffer(input).reverse().toString(); 

     // 
     inputOutPanel.setOutPut(reversedText); 

    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 

    } 

} 

ある

import javax.swing.JButton; 
import javax.swing.JPanel; 


public class Controls extends JPanel { 
    private ReverseText reverseText; //the Event handler 
    private TextFrame theTextFrame; // the main frame to display the text 

    public Controls(ReverseText reverseText){ 
    this.reverseText = reverseText; 

    reverseText = new ReverseText(theTextFrame); 

    JButton reversetheTextButton;  

    this.add(reversetheTextButton = new JButton("Text-Reverse")); 
    reversetheTextButton.addActionListener(reverseText); 


    } 

} 

ReverseTextクラスIは、これらのコンポーネントを呼び出すために私のイベントハンドラクラスのメインフレームを使用したいbutthis機能していません。

私はTextFrameというメインフレームにメソッドを取得して設定しています。

私の質問は、GUIから入力されたメインフレームcato takeを使用してこのイベントハンドラクラスを使用し、この入力を逆にする方法です。

答えて

3

コントロールのTextFrameフィールドをTextFrameインスタンスで設定することはありません。モデルを作成するのと同じように、パラメータを介してControlsコンストラクタに渡す必要があります。 (チェックし、システムの.outを使用して線を印刷することができる)

theControls = new Controls(theReverseText, this); // **** note change 
+0

うわー。できます!ありがとうございました –

+0

@ EvelynDrew:ようこそ。しかし、それが価値あるものであれば、コントロールはJPanelでも、JButtonなどのビューコンポーネントを持つこともできません。 –

0

新しいJButton(...)の後、ボタンのsetActionCommandを呼び出します。それ以外の場合は、ラベルを設定するだけで、リスナーが起動されてもボタンのActionCommandは取得されません。

+0

事は私のボタンが無事に動作しているにマッチしたコマンドを取得するには、文字列を使用していますので:

public class Controls extends JPanel { private ReverseText reverseText; //the Event handler private TextFrame theTextFrame; // the main frame to display the text // **** note changes to constructor public Controls(ReverseText reverseText, TextFrame theTextFrame){ this.reverseText = reverseText; this.theTextFrame = textFrame; // ***** added reverseText = new ReverseText(theTextFrame); 

はその後変更します私のリスナークラスのボタンの名前、私はそれらの他のコンポーネントを取得し、これらのフィールドを設定するメインフレームを使用できるようにしたいですか? –

関連する問題