Java Swing - 动态JPanel GroupLayout调整问题

时间:2014-02-17 11:31:33

标签: java swing resize jtabbedpane grouplayout

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

initialized frame

当我展开并再次缩小应用程序框架时,会发生这种情况(chatPanel的大小不会缩小,导致不可见的溢出区域)..

expanded and again shrinked frame

这是扩展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);
}
}

这就是我将这些chatPanel添加到chatTabbedPane

的方法
chatPanel newChat = new chatPanel(chatRef);
chatTabbedPane.addTab("title", null, newChat, null);

我希望缩小chatPanel的大小,缩小应用程序框架的大小。

1 个答案:

答案 0 :(得分:1)

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

为了回答你的问题,换行正在搞乱。

textArea.setLineWrap(true);

注释掉线并再次测试以查看它将按预期的窗口/框架调整大小。

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