Reputation: 34
It's my first post here and my English isn't pretty well so i hope you guy's understand what problem i have and also i hope i do nothing wrong here.
My Problem:
I'm learning atm Swing and how it works, but i have always some problems with picture which doesnt show up. Maybe i dont understand some part of Swing so i hope you can explain me why the picture doesnt loading so i can learn it and do a better work : )
i tried much variatons but i really only failed and i dont know why. i tried it also with graphics.
My Program:
JFrame -> JPanel -> JLabel (which have the picture and should put it on JPanel or maybe there is a direct way on JPanel)
test2.jpg is in my package folder and eclipse dont shout an error.
also i would JPanel in an separate class like it is and dont would extend JFrame to Gui class.
Here are my 3 classes:
Start:
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();
}
}
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);
}
}
and Panel:
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);
}
}
Thank you very much in advance for your help.
Upvotes: 1
Views: 1600
Reputation: 168815
The source below works. Changes include:
setSize(..)
.Panel
instance.Panel
was renamed ImagePanel
so that is not the same name as an existing AWT class!setVisible(..)
. The only thing that applies to is top level containers like JFrame
or JDialog
. For the rest, add them to a container that is itself made visible.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("https://i.sstatic.net/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));
}
}
Upvotes: 1
Reputation: 673
try like this. icon1= new ImageIcon(getClass().getResource("/test2.jpg"))
Upvotes: 0