這裏我有下面這段代碼反轉文本。使用主框架獲取其他組件的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中取得輸入並且反轉此輸入。
哇。有用!非常感謝 –
@EvelynDrew:不客氣。對於它的價值,控件不應該是一個JPanel,也不應該包含像JButton這樣的組件。 –