我正在尝试为我的即时通讯程序创建一个自定义聊天对话窗口。对话框由一个带有JFrame和一些额外属性的单独类组成。我可以创建JFrame和几个组件(发送消息按钮,用于键入消息的文本字段,以及应该包含我的JTextPane的滚动窗格)。不幸的是,JTextPane不会出现,只有滚动窗格的灰色框(我假设它是什么)。
以下是我的自定义对话窗口的代码。 包裹instantmessenger;
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);
}
}
所以现在就像我之前所说的那样,唯一显示的是灰色轮廓,其中文本和滚动窗格应该去。我在主GUI中的设计编辑器中创建了JFrame设计,然后将自动生成的代码复制并粘贴到我的自定义类中......
底部的代码只是让我一直在努力让它发挥作用。我本来可以把它改成普通的文本字段或其他东西并让它起作用,但我不能不知道我做错了什么。非常感谢帮助。
答案 0 :(得分:1)
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。
答案 1 :(得分:0)