Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用swing添加图像,以及如何使其与窗口大小相同?_Java_Swing_Window_Size_Imageicon - Fatal编程技术网

Java 如何使用swing添加图像,以及如何使其与窗口大小相同?

Java 如何使用swing添加图像,以及如何使其与窗口大小相同?,java,swing,window,size,imageicon,Java,Swing,Window,Size,Imageicon,我尝试将图像添加到我的窗口,使其与窗口大小相同,但项目不会运行,也不会出现图像,当我在使用图像之前,它不会是屏幕的大小,即使我使用了窗口所用的WIDTH和HEIGHT import javax.swing.*; public class Main { public static int WIDTH = 1000; public static int HEIGHT = 368; public static JFrame window = new JFrame();

我尝试将图像添加到我的窗口,使其与窗口大小相同,但项目不会运行,也不会出现图像,当我在使用图像之前,它不会是屏幕的大小,即使我使用了窗口所用的
WIDTH
HEIGHT

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);
    }

}

使用
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);
    }

}