我之前发布了这段代码,我得到了很多有用的答案,其中大部分是我需要完全更改我的代码。我明白了,明天我会做的!但是现在,这让我觉得为什么这行不通。
我正在尝试从 ChatBox 类获取 sendText 到我的 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;
}
}