【发布时间】:2020-02-01 20:14:02
【问题描述】:
我创建了一个 Board 类,我在其中使用 ImageIcon 导入图像,而不是制作具有指定分辨率的图片,我希望它对于任何类型的显示器都是全屏的。
public Board() {
p = new Dude();
addKeyListener(new AL());
setFocusable(true);
ImageIcon i = new ImageIcon("C:/test.jpg");
img = i.getImage();
time = new Timer(5, this);
time.start();
}
我以前用过这段代码:
public class MainMenu2 {
MainMenu2() throws IOException{
JFrame Main_Menu = new JFrame("Main Menu");
Main_Menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final int widthScreen = screenSize.width;
final int heightScreen = screenSize.height;
BufferedImage backgroundImage = ImageIO.read(new File("P://My Pictures//background1.jpg"));
JLabel background = new JLabel(new ImageIcon(backgroundImage));
Image scaleBackground = backgroundImage.getScaledInstance(widthScreen, heightScreen, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(scaleBackground);
Main_Menu.setContentPane(new JLabel(imageIcon));
使用缩放平滑,将图像设置为屏幕宽度和高度,但我不知道如何将其应用于我当前的代码。两者的区别在于,Board 类扩展了 JPanel,而 MainMenu2 只是一个普通类,其中创建了 JFrame 等。请帮助!谢谢。
【问题讨论】:
-
您可以使用Stretch Icon。它将根据可用空间动态调整其大小。
标签: java image swing imageicon