gpt4 book ai didi

java - JTextPane 不显示在动态添加的 JFrame 内

转载 作者:行者123 更新时间:2023-12-01 13:07:15 24 4
gpt4 key购买 nike

我正在尝试为我的即时通讯程序制作一个自定义聊天对话窗口。该对话由一个带有 JFrame 和几个额外属性的单独类组成。我可以创建 JFrame 和几个组件(一个发送消息按钮、一个用于键入消息的文本字段以及应该包含我的 JTextPane 的滚动 Pane )。不幸的是,JTextPane 不会显示,只有滚动 Pane 所在的灰色框(我假设它就是这样)。

这是我的自定义对话窗口的代码。 封装即时通讯工具;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.text.Document;


public class ChatDialogue extends JComponent{
private BuddyConversation owner;


/** COMPONENTS *****************************/
private JFrame window;
private javax.swing.JButton btnSendMessage;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane textMessages;
private javax.swing.JTextField textNewMessage;
/*******************************************/


public ChatDialogue(String username, BuddyConversation owner){
this.owner = owner;
initComponents(username);
}
private void btnSendMessageActionPerformed(java.awt.event.ActionEvent evt){
owner.addMessage(textNewMessage.getText());
}
public javax.swing.JTextPane getMessageBox(){
return textMessages;
}
private void initComponents(String toUser){
window = new JFrame();
window.setAlwaysOnTop(true);
window.setBounds(new java.awt.Rectangle(0, 0, 200, 400));
window.setMaximizedBounds(new java.awt.Rectangle(0, 0, 200, 12312312));
window.setMaximumSize(new java.awt.Dimension(200, 12312312));
window.setMinimumSize(new java.awt.Dimension(200, 375));
window.setTitle("Chat with " + toUser);

textMessages = new javax.swing.JTextPane();
jScrollPane1 = new javax.swing.JScrollPane(textMessages);
jLabel1 = new javax.swing.JLabel();
textNewMessage = new javax.swing.JTextField();
btnSendMessage = new javax.swing.JButton();

javax.swing.GroupLayout windowLayout = new javax.swing.GroupLayout(window.getContentPane());
window.getContentPane().setLayout(windowLayout);
windowLayout.setHorizontalGroup(
windowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(windowLayout.createSequentialGroup()
.addContainerGap()
.addGroup(windowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addComponent(textNewMessage)
.addGroup(windowLayout.createSequentialGroup()
.addGroup(windowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(btnSendMessage))
.addGap(0, 29, Short.MAX_VALUE)))
.addContainerGap())
);
windowLayout.setVerticalGroup(
windowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(windowLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textNewMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSendMessage)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
btnSendMessage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSendMessageActionPerformed(evt);
}
});
/** ADD COMPONENTS TO JFRAME ******************************************/
//window.getContentPane().add(jLabel1);
//window.getContentPane().add(jScrollPane1);
//window.getContentPane().add(textNewMessage);
//window.getContentPane().add(btnSendMessage);
//window.add(textMessages);
/**********************************************************************/

jScrollPane1.setViewportView(textMessages);

jLabel1.setText("Put your message her and then hit enter or something or press the button");

btnSendMessage.setText("Send Message");

jScrollPane1.setVisible(true);
textMessages.setVisible(true);
window.invalidate();
window.repaint();
window.pack();
window.revalidate();
window.setVisible(true);
}
}

所以现在正如我之前所说,唯一显示的是文本和滚动 Pane 应该所在的灰色轮廓。我在主 GUI 的设计编辑器中进行了 JFrame 设计,然后将自动生成的代码复制并粘贴到我的自定义类中...

底部的代码只是我在尝试让它工作。我可能可以将它切换到普通的文本字段或其他东西并让它工作,但我无法忍受不知道我做错了什么。非常感谢您的帮助。

最佳答案

window.setBounds(new java.awt.Rectangle(0, 0, 200, 400));
window.setMaximizedBounds(new java.awt.Rectangle(0, 0, 200, 12312312));
window.setMaximumSize(new java.awt.Dimension(200, 12312312));
window.setMinimumSize(new java.awt.Dimension(200, 375));

参见Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (是的。)

所以上面应该是:

window.pack();
window.setMinimumSize(window.getSize()); //this is a rare exception to the rule

请注意,框架将最终位于屏幕坐标 0,0(左上角)处。对于框架定位,您不能通过setLocationByPlatform(true)。请参阅this answer用于演示。

关于java - JTextPane 不显示在动态添加的 JFrame 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178845/

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