简体   繁体   English

Java Swing使用主机框架获取其他组件

[英]Java Swing using a main frame to get other components

Here I have this code below which reverses a text. 在这里,我有下面的这段代码可以反转文本。

TextFrame Class the main frame of my GUI 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 Class Panel which handles the input and output 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();


    }
}

Controls class 控件类

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 Class which is my event handler 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

    }

}

I want to use the mainframe in my event handler class to call these components butthis is not working. 我想在事件处理程序类中使用大型机来调用这些组件,但这不起作用。

I have get and set methods in my main Frame called TextFrame. 我在名为TextFrame的主框架中有get和set方法。

My question is how do I use my event handler class using the main frame cato take input from the GUI and reverse this input. 我的问题是如何使用主框架cato使用事件处理程序类,并从GUI接受输入并反转此输入。

You never set the Controls TextFrame field with any TextFrame instance. 您永远不会使用任何TextFrame实例设置“控件TextFrame”字段。 You need to pass it into the Controls constructor via a parameter, just as you do the model. 您需要像通过模型一样通过参数将其传递到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);

Then change: 然后更改:

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

after new JButton(...), call setActionCommand on the button. 在新的JButton(...)之后,请在按钮上调用setActionCommand。 Otherwise you just set the label, and the listener won't get an ActionCommand of the button even when the listener is fired. 否则,您只需设置标签,并且即使激发了侦听器,侦听器也不会获得按钮的ActionCommand。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM