场景:在JScrollPane的视口中有多个JLayeredPane。每个JLayeredPane至少有一个带有图像的JPanel (由paintComponent设置)。
问题:很难在没有看到的情况下解释(下面的代码):当滚动JScrollPane时,不完全在JScrollPane区域内的JLayeredPane中的图像不会被绘制出来。
如果我继续滚动,最终JLayeredPane将完全位于JScrollPane区域,并绘制图像。
为什么我认为问题在JLayeredPane**?** 中,如果我用JPane替换JLayeredPane,那么问题就消失了。所提供的代码可以显示这两种情况。设置公共类的第一个静态变量:public static boolean forceProblem = true来控制它。
问题:,我做错了什么或者该做些什么来解决这个问题?我需要继续使用JLayeredPane (或者其他可以这样做的东西)。
复制问题:
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);
}
}使用的图像:

发布于 2016-11-09 23:16:28
在向SetBounds中添加JPanel之后,使用JLayeredPane解决了这个问题:
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 ));
}
}https://stackoverflow.com/questions/40512043
复制相似问题