簡體   English   中英

Java Swing圖像未顯示

[英]Java Swing Image doesn't show

這是我在這里的第一篇文章,我的英語不太好,所以我希望你們明白我有什么問題,也希望我在這里沒有做錯任何事情。

我的問題:

我正在學習atm Swing及其工作方式,但是我總是遇到一些圖片無法顯示的問題。 也許我不了解Swing的某些部分,所以希望您能向我解釋為什么圖片無法加載,以便我可以學習並做得更好:)

我嘗試了很多變形,但實際上我只是失敗了,我也不知道為什么。 我也嘗試了圖形。

我的程序:

JFrame-> JPanel-> JLabel(具有圖片,應該將其放在JPanel上,或者也許在JPanel上有直接方法)

test2.jpg在我的包文件夾中,eclipse 不要大喊錯誤。

我也將JPanel放在像這樣的單獨類中,並且不將JFrame擴展到Gui類。

這是我的3個課程:

開始:

package verwaltungssoftware;

public class Start

{

    //Start der Applikation

    public static void main(String[] args)

    {
        System.out.println("Willkommen bei der Verwaltungssoftware fuer die Jobsuche");
        new Gui();
    }

}

桂:

package verwaltungssoftware;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;


public class Gui
{
    //Importiert Auflösung des Bildschirms
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();

    //Setzt Variablen für die Auflösung
    public int aufloesungBreite = screenSize.width;
    public int aufloesungHoehe = screenSize.height;

    //Setzt die Berechnung des JFrame hauptfenster Location
    private int breite = aufloesungBreite/2 - 640;
    private int hoehe = aufloesungHoehe/2 - 400;


    public Gui()

    {
        JFrame hauptfenster = new JFrame("Verwaltungssoftware fuer die Jobsuche");
        hauptfenster.setDefaultCloseOperation(hauptfenster.EXIT_ON_CLOSE);
        hauptfenster.setResizable(false);
        hauptfenster.setLocation(breite, hoehe);
        hauptfenster.setSize(1280,800);
        hauptfenster.setLayout(new BorderLayout(5,5));

        //Addet hauptpanel zum JFrame
        Panel hauptpanel = new Panel();
        hauptfenster.add(hauptpanel);   
        hauptpanel.setVisible(true);
        hauptfenster.setVisible(true);
    }


}

和面板:

package verwaltungssoftware;
import java.awt.BorderLayout;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Panel extends JPanel
{
    private static final long serialVersionUID = 6769810448979262470L;

    //Variablen

    Image icon1;


    //Konstruktor
    public Panel()

    {
        try 
        {
            icon1  = ImageIO.read(getClass().getResource("test2.jpg"));
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

        JPanel panelhauptfenster = new JPanel();
        panelhauptfenster.setLayout(new BorderLayout (5,5));
        panelhauptfenster.setSize(1280,800);
        panelhauptfenster.setLocation(0,0);
        panelhauptfenster.setVisible(true);

        JLabel myLabel=new JLabel();
        myLabel.setLocation(0,0);
        myLabel.setSize(panelhauptfenster.getWidth(),panelhauptfenster.getHeight());
        myLabel.setIcon(new ImageIcon(icon1));
        myLabel.setVisible(true);

        panelhauptfenster.add(myLabel);
    }
}

預先非常感謝您的幫助。

下面的源工作。 更改包括:

  • 返回合理的首選尺寸。 刪除對setSize(..)所有調用。
  • 分解出要添加圖像的面板,然后直接將其添加到Panel實例。
  • 但是Panel被重命名為ImagePanel因此與現有的AWT類名稱不同!
  • 刪除對setVisible(..)的調用。 唯一適用的是頂級容器,例如JFrameJDialog 對於其余部分,將它們添加到本身可見的容器中。

import java.awt.*;
import javax.swing.*;
import java.net.URL;
import javax.imageio.ImageIO;

class Gui {
    //Importiert Auflösung des Bildschirms
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();

    //Setzt Variablen für die Auflösung
    public int aufloesungBreite = screenSize.width;
    public int aufloesungHoehe = screenSize.height;

    //Setzt die Berechnung des JFrame hauptfenster Location
    private int breite = aufloesungBreite/2 - 640;
    private int hoehe = aufloesungHoehe/2 - 400;


    public Gui() {
        JFrame hauptfenster = new JFrame("Verwaltungssoftware fuer die Jobsuche");
        hauptfenster.setDefaultCloseOperation(hauptfenster.EXIT_ON_CLOSE);
        hauptfenster.setResizable(false);
        hauptfenster.setLocation(breite, hoehe);
        hauptfenster.setSize(1280,800);
        hauptfenster.setLayout(new BorderLayout(5,5));

        //Addet hauptpanel zum JFrame
        ImagePanel hauptpanel = new ImagePanel();
        hauptfenster.add(hauptpanel);   
        hauptpanel.setVisible(true);
        hauptfenster.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                new Gui();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class ImagePanel extends JPanel {
    //Variablen
    Image icon1;

    //Konstruktor
    public ImagePanel() {
        try  {
            URL url = new URL("http://i.stack.imgur.com/7bI1Y.jpg");
            icon1  = ImageIO.read(url);
        } catch (Exception e)  {
            e.printStackTrace();
        }

        setLayout(new BorderLayout (5,5));
        JLabel myLabel=new JLabel(new ImageIcon(icon1));
        add(myLabel);
    }

    // very important!
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(icon1.getWidth(this), icon1.getHeight(this));
    }
}

嘗試這樣。 icon1 =新的ImageIcon(getClass()。getResource(“ / test2.jpg”))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM