我怎样才能添加带有 swing 的图像以及如何使它的大小达到我的 window?
How can i add an image with swing and how do i make it the size of my window?
我试图将图像添加到我的 window 并使其与 window 的大小相同,但是项目不会 运行 并且没有图像出现,当我让图像在它不会是屏幕大小之前工作,甚至认为我使用 WIDTH
和 HEIGHT
,这是我用于 window 的。
import javax.swing.*;
public class Main {
public static int WIDTH = 1000;
public static int HEIGHT = 368;
public static JFrame window = new JFrame();
public static void main(String[] args) {
CreateWindow();
}
public static void CreateWindow() {
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setSize(WIDTH, HEIGHT);
BackgroundImage();
window.setVisible(true);
}
public static void BackgroundImage() {
ImageIcon image = new ImageIcon("C:\Users\SamBr\Pictures\image.png");
window.add(image)
image.setSize(WIDTH, HEIGHT);
}
}
使用 JLabel
显示您的图片,使用 getScaledInstance()
方法您可以调整它的大小。
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
public class Main {
public static int WIDTH = 1000;
public static int HEIGHT = 368;
public static JFrame window = new JFrame();
public static void main(String[] args) {
CreateWindow();
}
public static void CreateWindow() {
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setSize(WIDTH, HEIGHT);
BackgroundImage();
window.pack();
window.setVisible(true);
}
public static void BackgroundImage() {
ImageIcon imageIcon = new ImageIcon("C:\Users\SamBr\Pictures\image.png");
ImageIcon scaledImage = new ImageIcon(
imageIcon.getImage().getScaledInstance(WIDTH, HEIGHT, Image.SCALE_SMOOTH));
JLabel label = new JLabel();
label.setIcon(scaledImage);
window.add(label);
}
}
我试图将图像添加到我的 window 并使其与 window 的大小相同,但是项目不会 运行 并且没有图像出现,当我让图像在它不会是屏幕大小之前工作,甚至认为我使用 WIDTH
和 HEIGHT
,这是我用于 window 的。
import javax.swing.*;
public class Main {
public static int WIDTH = 1000;
public static int HEIGHT = 368;
public static JFrame window = new JFrame();
public static void main(String[] args) {
CreateWindow();
}
public static void CreateWindow() {
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setSize(WIDTH, HEIGHT);
BackgroundImage();
window.setVisible(true);
}
public static void BackgroundImage() {
ImageIcon image = new ImageIcon("C:\Users\SamBr\Pictures\image.png");
window.add(image)
image.setSize(WIDTH, HEIGHT);
}
}
使用 JLabel
显示您的图片,使用 getScaledInstance()
方法您可以调整它的大小。
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
public class Main {
public static int WIDTH = 1000;
public static int HEIGHT = 368;
public static JFrame window = new JFrame();
public static void main(String[] args) {
CreateWindow();
}
public static void CreateWindow() {
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setSize(WIDTH, HEIGHT);
BackgroundImage();
window.pack();
window.setVisible(true);
}
public static void BackgroundImage() {
ImageIcon imageIcon = new ImageIcon("C:\Users\SamBr\Pictures\image.png");
ImageIcon scaledImage = new ImageIcon(
imageIcon.getImage().getScaledInstance(WIDTH, HEIGHT, Image.SCALE_SMOOTH));
JLabel label = new JLabel();
label.setIcon(scaledImage);
window.add(label);
}
}