根据窗口的屏幕分辨率调整图像 [英] Adjust the image as per screen resolution of window
问题描述
我在 JFrame
中添加了 JLabel
,并在其中添加了一个大小为1024x768的图像 JLabel
。
I have add a JLabel
in a JFrame
and one image of size 1024x768 is added in JLabel
.
当我在1024x768中设置窗口屏幕的分辨率并运行该应用程序时,图像显示在全视窗。但是,当我将窗口屏幕的分辨率设置为1280x768时-图像仅显示在窗口的三分之一中。
When I set the resolution of window screen in 1024x768 the and run the application the image is shown in full window. But when I set the resolution of window screen in 1280x768 - the image is shown in only one third portion of window.
如何调整或添加1024x768大小的图像这样在任何屏幕分辨率下图像都将显示在整个窗口上?换句话说,根据窗口的屏幕分辨率调整图像。
How can I adjust or add the image of size 1024x768 so that in any screen resolution the image will show covering the full window? In other words the image is adjusted as per the screen resolution of window.
推荐答案
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.net.URL;
import javax.imageio.ImageIO;
class ImagePanel extends JPanel {
Image image;
ImagePanel(Image image) {
this.image = image;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,getWidth(),getHeight(),this);
}
public static void main(String[] args) throws Exception {
URL url = new URL("http://pscode.org/media/stromlo2.jpg");
final Image image = ImageIO.read(url);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("Image");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationByPlatform(true);
ImagePanel imagePanel = new ImagePanel(image);
imagePanel.setLayout(new GridLayout(5,10,10,10));
imagePanel.setBorder(new EmptyBorder(20,20,20,20));
for (int ii=1; ii<51; ii++) {
imagePanel.add(new JButton("" + ii));
}
f.setContentPane(imagePanel);
f.pack();
f.setVisible(true);
}
});
}
}
使用的原始图像
Raw image used
这篇关于根据窗口的屏幕分辨率调整图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!