0

这些图像显示了一个需要使用应用程序框架调整大小的聊天框。我正在使用 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);

我想通过缩小应用程序框架的大小来缩小聊天面板的大小。

4

1 回答 1

1

我也遇到了这个问题,像“只使用另一个布局管理器”这样的答案不是我愿意接受的答案。

为了回答你的问题,换行符把事情搞砸了。

textArea.setLineWrap(true);

注释掉该行并再次测试以查看它是否会像预期的那样随着窗口/框架缩小。

但是,副作用显然是没有换行。不太明显的是,当您的文本长度变得大于 textarea 的宽度时,当您调整窗口大小时,textarea 的宽度将锁定为文本的长度。我还没有找到解决这个新问题的方法。

于 2014-04-14T05:46:33.730 回答