我早些时候发布了这段代码,并且得到了很多有用的答案,大部分是我需要完全更改代码。我了解,明天我会做!但是现在,这正在蚕食我为什么不起作用。
我试图将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时就知道,吸气剂/塞特剂将是我要完全理解的一个问题,我想我大声笑是正确的...
谢谢你的帮助!!!
新密码
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;
}
}
最佳答案
您需要从Chatbox
到MessageWindow
的链接,以便来回发送消息。
可以做的是如下修改
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);
}
现在输入内容,您应该会看到打印输出