解决方案 »

  1.   

    简化后的问题代码如下
    private void initialize() {

    setBackground(new Color(240, 248, 255));
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{350, 0};
    gridBagLayout.rowHeights = new int[]{500, 0};
    gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
    setLayout(gridBagLayout);

    JScrollPane speOutputWindow = new JScrollPane();
    speOutputWindow.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    GridBagConstraints gbc_speOutputWindow = new GridBagConstraints();
    gbc_speOutputWindow.fill = GridBagConstraints.BOTH;
    gbc_speOutputWindow.gridx = 0;
    gbc_speOutputWindow.gridy = 0;
    add(speOutputWindow, gbc_speOutputWindow);
    speOutputWindow.setAlignmentY(Component.TOP_ALIGNMENT);
    speOutputWindow.setAlignmentX(Component.LEFT_ALIGNMENT);

    JPanel panel = new JPanel();
    panel.setBackground(new Color(240, 248, 255));
    speOutputWindow.setViewportView(panel);
    GridBagLayout gbl_plTotal = new GridBagLayout();
    gbl_plTotal.columnWidths = new int[] { 10, 200, 10};
    gbl_plTotal.rowHeights = new int[] { 0};
    gbl_plTotal.columnWeights = new double[] { 0.0, 1.0, 0.0 };
    gbl_plTotal.rowWeights = new double[] { 1.0};
    panel.setLayout(gbl_plTotal);
    ArrayList<String> message = new ArrayList<String>();

    message.add("test1");
    message.add("test2");
    message.add("test3");
    message.add("test4");
    message.add("test5");
    message.add("test6");
    message.add("test7");
    message.add("test8");
    message.add("test9");
    message.add("test10");
    // for (int i = 0; i < 5; i++) {
    int i = 0;
    OtherChatPanel plOtherBoard = new OtherChatPanel("other", message.get(2*i), "2014/01/10 9:14:30");
    GridBagConstraints gbc_plOtherBoard = new GridBagConstraints();
    //gbc_plOtherBoard.insets = new Insets(0, 0, 5, 5);
    gbc_plOtherBoard.fill = GridBagConstraints.HORIZONTAL;
    gbc_plOtherBoard.anchor = GridBagConstraints.LAST_LINE_START;
    gbc_plOtherBoard.gridx = 1;
    gbc_plOtherBoard.gridy = 2*i;
    //gbc_plOtherBoard.
    panel.add(plOtherBoard, gbc_plOtherBoard);
    MyChatPanel plMyBoard = new MyChatPanel("self", message.get(2*i+1),
    "2014/01/10 9:14:50");
    GridBagConstraints gbc_plMyBoard = new GridBagConstraints();
    //gbc_plMyBoard.insets = new Insets(0, 0, 5, 5);
    gbc_plMyBoard.fill = GridBagConstraints.HORIZONTAL;
    gbc_plMyBoard.anchor = GridBagConstraints.LAST_LINE_END;
    gbc_plMyBoard.gridx = 1;
    gbc_plMyBoard.gridy = 2*i + 1;
    panel.add(plMyBoard, gbc_plMyBoard);
    }
      

  2.   

    现在用swing很有前途么,我写过一个小例子,后来再以没碰了,因为他们说,swing现在用的人不是那么多
      

  3.   


    我没试楼主给的代码。(代码不全,不好调试)。不过,可以肯定的是,GridBagLayout 是可以自适应!