2016-03-20 3 views
0

Ich habe versucht, ein Bild zu meinem Fenster hinzuzufügen und es so groß wie das Fenster zu machen, aber das Projekt wird nicht ausgeführt und kein Bild wird angezeigt, wenn das Bild funktionierte, bevor es nicht die Größe des Bildschirms hatte sogar gedacht, dass ich und HEIGHT verwendet habe, was ich für das Fenster verwendet habe.Wie kann ich ein Bild mit Schwung hinzufügen und wie mache ich es so groß wie mein Fenster?

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

} 

Antwort

2

Verwenden JLabel Ihr Bild zu zeigen und mit getScaledInstance() Methode können Sie es ändern.

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

} 
Verwandte Themen