无法从另一个类中获取文本以在另一个类上打印出来(noob)

时间:2013-08-26 02:55:07

标签: java getter getter-setter

我之前发布了这段代码,我得到了很多有用的答案,而且大部分是我需要完全更改代码。据我所知,明天我会做!但是现在,这就是在为什么这不起作用而惹恼我。

我试图从ChatBox类中获取sendText,到messagePane中的MessageWindow类和输出。而已。这看起来很简单,而且可能是......但我已经连续10个小时了。我只是希望它将我放在ChatBox中的内容输出到MessageWindow,而不是完全改变我的代码。请帮忙:(

public class ChatBox extends JPanel {

private JScrollPane scrollPane;
private String sendText;

public ChatBox() {
    final JTextArea chatPane = new JTextArea();

    scrollPane = new JScrollPane(chatPane,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    add(scrollPane);
    scrollPane.setMinimumSize(new Dimension(550, 50));
    scrollPane.setPreferredSize(new Dimension(550, 50));

    chatPane.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if( e.getKeyCode() == KeyEvent.VK_ENTER ) {
                sendText = chatPane.getText();
                setText(sendText);
                chatPane.setText(null);
                // System.out.println(sendText); // I can see this in console
            }

        }

        @Override
        public void keyTyped(KeyEvent e) {
        }

    });

}


public String getText() {
    return sendText;
}


public void setText(String sendText) {
    this.sendText = sendText;
}

}

在我脑海中,我正在设置sendText - >无论我输入什么。然后在MessageWindow类中,我尝试使用getter来获取messagePane中的文本和输出。

public class MessageWindow extends JPanel {

private ChatBox box = new ChatBox();

public MessageWindow() {
    JTextArea messagePane = new JTextArea();

    setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    gc.weightx = 1;
    gc.weighty = 1;
    gc.fill = GridBagConstraints.BOTH;
    gc.insets = new Insets(5, 5, 5, 5);
    add(new JScrollPane(messagePane), gc);

    System.out.println(box.getText());   // Getting null in the console.
    messagePane.append(box.getText());   // Not getting anything on messagePane.

}

}

我知道我需要使用ActionListeners和JTextField,而不是JTextArea。我保证明天会开始。我会废弃整个程序,我只需要知道为什么这些基本的东西让我失望:(我知道在学习Java时,getter / setter对我来说是一个完全理解的问题,我想我我是对的,大声笑......

感谢您的帮助!!!

新代码

public class MessageWindow extends JPanel {

private ChatBox box = new ChatBox(this);

public void OnTextSet(String s) {
    System.out.println(s);
}

public MessageWindow() {
    JTextArea messagePane = new JTextArea();

    setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    gc.weightx = 1;
    gc.weighty = 1;
    gc.fill = GridBagConstraints.BOTH;
    gc.insets = new Insets(5, 5, 5, 5);
    add(new JScrollPane(messagePane), gc);

    System.out.println(box.getText()); // Getting null in the console.
    messagePane.append(box.getText()); // Not getting anything on
                                        // messagePane.

}

}

public class ChatBox extends JPanel {

private JScrollPane scrollPane;
private String sendText = "";
private MessageWindow mw;

public ChatBox() {
    final JTextArea chatPane = new JTextArea();

    scrollPane = new JScrollPane(chatPane,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    add(scrollPane);
    scrollPane.setMinimumSize(new Dimension(550, 50));
    scrollPane.setPreferredSize(new Dimension(550, 50));

    chatPane.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                sendText = chatPane.getText();
                setText(sendText);
                chatPane.setText(null);
                mw.OnTextSet(sendText);
                // System.out.println(sendText); // I can see this in
                // console
            }

        }

        @Override
        public void keyTyped(KeyEvent e) {
        }

    });

}

public ChatBox(MessageWindow mw) {
    this.mw = mw;
}


public String getText() {
    return sendText;
}

public void setText(String sendText) {
    this.sendText = sendText;
}

}

2 个答案:

答案 0 :(得分:0)

您需要从ChatboxMessageWindow的链接,以便来回发送消息。

可以做的是修改如下

private ChatBox box = new ChatBox(this); //is this legal in java?
                                  ^^^^
public void OnTextSet(String s){
    System.out.println(s);      
}

//elsewhere
private MessageWindow mw;
public ChatBox(MessageWindow mw) {
               ^^^^^^^^^^^^^^^^
   this.mw = mw

...
public void keyReleased(KeyEvent e) {
...
   mw.OnTextSet(sendText);
}

现在输入内容,你应该看到打印输出

答案 1 :(得分:0)

ChatBox类中的变量 sendText 在创建时未初始化。由于 keyReleased 事件尚未触发, sendText 保持为NULL。 尝试修改Chatbox中的代码

从:

private String sendText;

为:

private String sendText = "";