谢谢版主,我增加了pnlLeft.setPreferredSize(new Dimension(300, 200));pnlRight.setPreferredSize(new Dimension(300, 200));这样两个panel是大小控制住了,但是滚动条还是没显示出来。

解决方案 »

  1.   

    你给JScrollPane 加上这两个方法看看
    setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
      

  2.   

    调整了一下解决了
    /**
     * 主面板
     */
    public void mainPanel() {
    Container contentPane = this.getContentPane();
    JPanel title = new JPanel();
    JLabel lblTitle = new JLabel("图片压缩前后效果比对", JLabel.CENTER);
    title.setBackground(Color.WHITE);
    title.add(lblTitle);
    contentPane.add(title, BorderLayout.NORTH);
    // 主面板
    JPanel main = new JPanel();
    main.setBackground(Color.WHITE);
    main.setLayout(new BoxLayout(main, BoxLayout.X_AXIS));
    main.setVisible(true);
    contentPane.add(main);

    // 左边面板
    JPanel pnlLeft = new JPanel();
    // 左边原始图片
    JLabel lblSource = new JLabel();
    lblSource.setIcon(getIcon("F:\\MyDocument\\RainBoy\\Desktop\\11.jpg"));
    // 左边滚动条
    final JScrollPane scrollLeftPane = new JScrollPane(lblSource);
    pnlLeft.add(scrollLeftPane);
    scrollLeftPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollLeftPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 
    scrollLeftPane.setPreferredSize(new Dimension(WIDTH/2, HEIGHT/2));
    main.add(scrollLeftPane);
    // 右侧面板
    JPanel pnlRight = new JPanel();

    // 右边处理图片
    JLabel lblTaget = new JLabel();
    lblTaget.setIcon(getIcon("F:\\MyDocument\\RainBoy\\Desktop\\Thumb122.jpg"));
    // 右边滚动条
    final JScrollPane scrollRightPane = new JScrollPane(lblTaget);
    pnlRight.add(scrollRightPane);
    scrollRightPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollRightPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 
    scrollRightPane.setPreferredSize(new Dimension(WIDTH/2, HEIGHT/2));
    main.add(scrollRightPane);
    scrollLeftPane.addAncestorListener(new AncestorListener(){
    public void ancestorMoved(AncestorEvent event) {
    JScrollBar jScrollBarLeft = scrollLeftPane.getHorizontalScrollBar();
    int i = jScrollBarLeft.getValue();
    JScrollBar jScrollBarRight = scrollRightPane.getHorizontalScrollBar();
    jScrollBarRight.setValue(jScrollBarLeft.getValue());
    } public void ancestorAdded(AncestorEvent event) {
    } public void ancestorRemoved(AncestorEvent event) {
    }
    }); }但现在有个问题。两边的滚动条无法同步滚动。即我拖动左边的滚动条,想让右边的也进行滚动