这些图像显示了一个需要使用应用程序框架调整大小的聊天框。我正在使用 GroupLayout 这样做。框架的层次结构是这样的
chatDiv(JPanel, GroupLayout)->chatTabbedPane(JTabbedPane, GroupLayout)
->chatPanel(extended JPanel, GroupLayout)
但是,这些单独的 chat_tabs(chatPanel) 会在应用程序运行时动态添加到 chatTabbedPane。
Problem is: These chatPanel(s) are expanding with the expansion of application
frame but they are not shrinking with the shrink in width of application frame.
当我展开并再次缩小应用程序框架时,会发生这种情况(chatPanel 的大小不会缩小导致不可见的溢出区域)..
这是扩展 JPanel 的 chatPanel 类:
class chatPanel extends JPanel{
private static final long serialVersionUID = 1L;
GroupLayout gl_panel;
JTextArea textArea;
JTextField textField;
JButton sendTextButton;
JButton closeChatButton;
int replyRef;
public chatPanel(int ref){
//this.setLayout(null);
closeChatButton = new JButton("");
sendTextButton = new JButton("SEND");
textField = new JTextField();
textArea = new JTextArea();
replyRef = ref;
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setLineWrap(true);
//textArea.setBounds(0, 0, 370, 99);
this.add(textArea);
//textField.setBounds(0, 101, 290, 20);
//textField.setColumns(10);
this.add(textField);
sendTextButton.setFont(new Font("Tahoma", Font.PLAIN, 10));
//sendTextButton.setBounds(290, 101, 60, 19);
this.add(sendTextButton);
closeChatButton.setIcon(new ImageIcon("files/close.png"));
closeChatButton.setSelectedIcon(null);
closeChatButton.setToolTipText("CLOSE");
closeChatButton.setFont(new Font("Tahoma", Font.PLAIN, 5));
//closeChatButton.setBounds(350, 101, 20, 19);
this.add(closeChatButton);
gl_panel = new GroupLayout(this);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(1)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
.addGap(1)
.addComponent(sendTextButton, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
.addGap(1)
.addComponent(closeChatButton, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addGap(1))
.addComponent(textArea, GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(1)
.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(sendTextButton, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(closeChatButton, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(gl_panel.createSequentialGroup()
.addGap(1)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(1))))
);
this.setLayout(gl_panel);
}
}
这就是我将这些聊天面板添加到 chatTabbedPane 的方式
chatPanel newChat = new chatPanel(chatRef);
chatTabbedPane.addTab("title", null, newChat, null);
我想通过缩小应用程序框架的大小来缩小聊天面板的大小。