首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JLayeredPane + JScrollPane裁剪问题

JLayeredPane + JScrollPane裁剪问题
EN

Stack Overflow用户
提问于 2016-11-09 16:46:56
回答 1查看 90关注 0票数 1

场景:JScrollPane的视口中有多个JLayeredPane。每个JLayeredPane至少有一个带有图像的JPanel (由paintComponent设置)。

问题:很难在没有看到的情况下解释(下面的代码):当滚动JScrollPane时,不完全在JScrollPane区域内的JLayeredPane中的图像不会被绘制出来。

如果我继续滚动,最终JLayeredPane将完全位于JScrollPane区域,并绘制图像。

为什么我认为问题在JLayeredPane**?** 中,如果我用JPane替换JLayeredPane,那么问题就消失了。所提供的代码可以显示这两种情况。设置公共类的第一个静态变量:public static boolean forceProblem = true来控制它。

问题:,我做错了什么或者该做些什么来解决这个问题?我需要继续使用JLayeredPane (或者其他可以这样做的东西)。

复制问题:

  1. 运行下面的代码。
  2. 向下滚动垂直条。
  3. 向右滚动水平条:问题:图像的上线没有加载
  4. 慢慢滚动垂直条向上:图像加载时,完全进入卷轴区域。
代码语言:javascript
复制
import java.awt.*;
import javax.swing.*;

class MYimagePanel extends JPanel {
    public Image image; 

    public MYimagePanel( Image img ) {
        this.image = img;
        this.setLayout( null );

        this.setBounds(0, 0, 1, 1); 
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.setSize( 100 , 100 );
        this.setPreferredSize( new Dimension( 100 , 100 ));     

        g.drawImage( this.image , 0 , 0 , 100 , 100 , null );
    }
}

class MYcomposedImagePanel extends JLayeredPane {
    public MYcomposedImagePanel( Image img ) {
        this.setLayout( null );

        MYimagePanel myImgPane = new MYimagePanel( img );

        this.add( myImgPane );
        this.setLayer( myImgPane , 1 );

        this.setBounds( 0, 0 , 100 , 100 );
        //this.setPreferredSize( new Dimension( 100 , 100 ));  
    }
}






public class ClippingProblem extends JFrame {
    public static boolean forceProblem = true;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() { 
                // Creating Frame
                JFrame frame = new ClippingProblem();

                Container contentPane = frame.getContentPane();
                contentPane.setLayout( null );

                // ScrollPane viewport
                JLayeredPane imagesPane = new JLayeredPane();
                imagesPane.setLayout( null );
                imagesPane.setLocation(0, 0);
                imagesPane.setPreferredSize( new Dimension(2000,2000));

                // ScrollPane
                JScrollPane scrollPane = new JScrollPane( imagesPane  ); 
                scrollPane.setBounds(0, 0, 1000 , 700 );
                scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);

                contentPane.add( scrollPane );

                // Add Images
                int offset = 0;
                MYcomposedImagePanel composedImage;
                MYimagePanel myImagePanel;
                ImageIcon icon = new ImageIcon( "image.png" );

                for( int y = 0 ; y < 1900 ; y = y + 100 ) {
                    for( int x = 0 ; x < 1900 ; x = x + 100 ) {
                        if( forceProblem == true ) {
                            composedImage = new MYcomposedImagePanel( icon.getImage() );
                            composedImage.setBounds( x + offset , y , 100 , 100 );
                        imagesPane.add( composedImage );
                    } else {
                        myImagePanel = new MYimagePanel( icon.getImage()  );
                        myImagePanel.setBounds( x + offset , y , 100 , 100 );
                        imagesPane.add( myImagePanel );
                    }
                    offset += 10;
                }
                // Set visible
                frame.setVisible(true);
            }
        } ) ;
    }


    public ClippingProblem() {
        setSize(1024, 768);

        setTitle("Clipping Problem");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
}

使用的图像:

EN

回答 1

Stack Overflow用户

发布于 2016-11-09 23:16:28

在向SetBounds中添加JPanel之后,使用JLayeredPane解决了这个问题:

代码语言:javascript
复制
 class MYcomposedImagePanel extends JLayeredPane {
    public MYcomposedImagePanel( Image img ) {
        this.setLayout( null );

        MYimagePanel myImgPane = new MYimagePanel( img );

        this.add( myImgPane );
        this.setLayer( myImgPane , 1 );

        myImgPane.setBounds( 0, 0 , 100 , 100 ); // <<--- NEW LINE ----

        this.setBounds( 0, 0 , 100 , 100 );
        //this.setPreferredSize( new Dimension( 100 , 100 ));  
    }
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40512043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档