gpt4 book ai didi

java - 无法从另一个类获取文本以在另一个类上打印(菜鸟)

转载 作者:行者123 更新时间:2023-12-01 04:26:44 25 4
gpt4 key购买 nike

我之前发布了这段代码,我得到了很多有用的答案,其中大多数是我需要完全更改我的代码。我明白了,明天我就会这么做!但现在,这让我很困惑为什么这行不通。

我试图将 sendText 从 ChatBox 类获取到我的 MessageWindow 类并在 messagePane 中输出。就是这样。这看起来很简单,而且可能是……但我实际上已经连续 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;
}

}

最佳答案

您需要从 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);
}

现在输入一些内容,您应该会看到打印输出

关于java - 无法从另一个类获取文本以在另一个类上打印(菜鸟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18435961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com