如何在小型JFrame中显示大型JPanel的全部内容?

时间:2014-09-21 13:47:35

标签: java swing jframe jpanel

我有JPanel,尺寸为816 x 1056像素(标准打字纸尺寸)。有没有办法显示整个JPanel及其内容而不改变其实际大小,只需要JFrame小?有没有办法缩小它?

1 个答案:

答案 0 :(得分:0)

我无法想出一种简单的方法来缩放JPanel,但你可以通过双缓冲来实现:

private void paintPanel (Graphics2D g)
{
    // here goes the code of your paint() or paintComponent() method
}

/** Draw content into an Image, shrink it, and display it. */
protected void paintComponent (Graphics g)
{
    // paint image
    BufferedImage img = new BufferedImage(MAX_WIDTH, MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);
    paintPanel(img.createGraphics());

    // shrink image and paint it
    g.drawImage(img, 0,0, WIDTH,HEIGHT, 0,0, MAX_WIDTH,MAX_HEIGHT, null);
}

这将在BufferedImage上绘制尺寸为MAX_WIDTH x MAX_HEIGHT的所有内容。然后,图片将缩小为WIDTH x HEIGHT,然后显示。