2017-02-03 53 views
0

這裏我有下面這段代碼反轉文本。使用主框架獲取其他組件的Java Swing

TextFrame類我的GUI的主框架

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.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 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類,這是我的事件處理程序

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 

    } 

} 

我想使用的主機在我的事件處理程序類調用這些組件但是這場不工作。

我已經在我的主框架中獲取並設置了一個名爲TextFrame的方法。

我的問題是如何使用我的事件處理程序類使用主框架cato從GUI中取得輸入並且反轉此輸入。

回答

3

您從不設置任何TextFrame實例的控件TextFrame字段。您需要通過參數將它傳遞到Controls構造函數,就像您做模型一樣。

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

然後改變:

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

哇。有用!非常感謝 –

+0

@EvelynDrew:不客氣。對於它的價值,控件不應該是一個JPanel,也不應該包含像JButton這樣的組件。 –

0

在新的JButton(...)之後,在按鈕上調用setActionCommand。否則,您只需設置標籤,即使偵聽器被觸發,偵聽器也不會獲得按鈕的ActionCommand。

+0

事情是我的按鈕正常的工作(檢查,它能夠使用系統的.out打印線),因爲我使用的字符串來獲得匹配的命令我的偵聽器類中的按鈕的名稱,我想能夠使用主框架來獲取這些其他組件,然後相應地設置這些字段? –