Swing GridBagLayout in Größe anpassen

olimeck

Mitglied
Hallo,
wir wollen ein Spielbrett als grafische Oberfläche anlegen und dazu das GridBagLayout verwenden. Soweit klappt alles. Allerdings liegen unsere grafischen Komponenten viel zu weit auseinander. Wie kann man das Gitter so skalieren, damit unsere JLabels (runde Spielfelder) proportional im richtigen Abstand zueinander angezeigt werden? Der Abstand sollte so ca. die Hälfte der Spielfeldgröße betragen....

Code:
import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class Spielbrett extends JFrame{
    private ImageIcon icon, icon2;
    
    public static void main (String[]args){
        Spielbrett sb = new Spielbrett();  //Erzeugung des JFrames "Spielbrett"
        sb.setLocation(10,10); //Ort angeben, (in Pixel), an dem das erzeugte Spielbrett auf dem Fenster erscheint
        sb.setSize(300,300); //Größe des Spielbretts
        sb.setVisible(true); //Spielbrett sichtbar machen
    }
    public Spielbrett(){
        super();//Aufruf des Konstruktors der Superklasse "JFrame"
        icon = new ImageIcon("Kreis.png"); //Erzeugung eines neuen ImageIcon-Objekts
        GridBagLayout gbl = new GridBagLayout();//Erzeugen eines Objekts vom Typ GridBagLayout
        GridBagConstraints gbc;//Objekt vom Typ GridBagConstraints
        this.setLayout(gbl);//Zuweisen des GridBagLayouts
        Spielfeld sf1 = new Spielfeld(icon);//Erzeugung eines Spielfeld-Objekts
        gbc = makegbc(0,0,0,0);//Zuweisung der Werte zu dem GridBagConstraints-Objekt mit der Methode "makegbc"
        gbc.weightx = 100; //Horizontale Raumverteilung
        gbc.weighty = 100;//Vertikale Raumverteilung
        gbc.fill = GridBagConstraints.BOTH;//Skalierung der Größe der grafischen Komponente
        gbl.setConstraints(sf1, gbc);//
        this.add(sf1);
        
        //ImageIcon icon2 = new ImageIcon("Kreis.png");
        Spielfeld sf2 = new Spielfeld(icon);
        gbc = makegbc(0,1,1,0);
        gbc.weightx = 100;
        gbc.weighty = 100;
        gbc.fill = GridBagConstraints.BOTH;
        gbl.setConstraints(sf2, gbc);
        this.add(sf2);
        
        Spielfeld sf3= new Spielfeld(icon);
        gbc = makegbc(1,1,1,0);
        gbc.weightx = 100;
        gbc.weighty = 100;
        gbc.fill = GridBagConstraints.BOTH;
        gbl.setConstraints(sf3, gbc);
        this.add(sf3);
        
        Spielfeld sf4 = new Spielfeld(icon);
        gbc = makegbc(1,0,0,1);
        gbc.weightx = 100;
        gbc.weighty = 100;
        gbc.fill = GridBagConstraints.BOTH;
        gbl.setConstraints(sf4, gbc);
        this.add(sf4);
    }
    /**
     * Methode zum Speichern der Werte für das
     * GridBagConstraint-Objekts
     */
    private GridBagConstraints makegbc(int x, int y, int width, int height){
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.insets = new Insets(1,1,1,1);
        return gbc;
    
    }
}
import javax.swing.*;

public class Spielfeld extends JLabel{
    private boolean istBesetzt;
    private String color;
    
    public Spielfeld(ImageIcon icon){
        super(icon);
        istBesetzt = false;
        color = "";
    }
 

KonradN

Super-Moderator
Mitarbeiter
Ich bin im Augenblick etwas verwirrt, was Du da alles setzt.

Zum einen hast Du bei dem Fenster eine feste Größe (300x300) die du setzt.
Dann hast Du das Grid Layout

Nun setzt für alle Felder:
  • wo im Grid die zu finden sind (4 Felder mit: 0,0; 0,1; 1,0; 1,1)
  • die Größe, wobei Du Höhe / Breite teilweise auf 0 setzt? Der Punkt ist für mich unverständlich....
  • dann gibst Du ein Insets an - dass da halt jeweils 1 Pixel oben, rechts, unten und links sein sollen.
  • Zusätzlich setzt Du aber allen noch ein weightx / weighty von 100. Das sorgt nun aber dafür, dass jedes Grid den vollen Anteil vom nicht belegten Platz bekommt. Bei einem Grid von 2x2 bedeutet das, dass jedes Feld 150x150 hat um das 300x300 zu füllen.

Hier ist daher wirklich die Frage, was Du genau erreichen willst. Wenn du eine bestimmte Größe beim Spielfeld haben willst, dann könntest Du das Fenster genau auf die benötigte Größe setzen.
Oder - was üblicher ist: Du packst da noch mehr Elemente ineinander. Dann ist das Spielfeld in einem JPanel und das kann dann eine feste Größe haben und in einem Fenster zentriert angezeigt werden.

Das sind nur zwei der einfachen Ideen, die mir durch den Kopf gehen. Da kann man natürlich auf viele Ideen kommen, aber die zwei dürften mit die einfachsten Lösungen sein denke ich mir (Oder ich habe jetzt auf Anhieb eine andere Lösung übersehen. Evtl. weightx/weighty auf 0 setzen? Aber das wäre nicht, wie ich gewohnt bin, so UIs zu bauen.)
 

olimeck

Mitglied
Ich bin im Augenblick etwas verwirrt, was Du da alles setzt.

Zum einen hast Du bei dem Fenster eine feste Größe (300x300) die du setzt.
Dann hast Du das Grid Layout

Nun setzt für alle Felder:
  • wo im Grid die zu finden sind (4 Felder mit: 0,0; 0,1; 1,0; 1,1)
  • die Größe, wobei Du Höhe / Breite teilweise auf 0 setzt? Der Punkt ist für mich unverständlich....
  • dann gibst Du ein Insets an - dass da halt jeweils 1 Pixel oben, rechts, unten und links sein sollen.
  • Zusätzlich setzt Du aber allen noch ein weightx / weighty von 100. Das sorgt nun aber dafür, dass jedes Grid den vollen Anteil vom nicht belegten Platz bekommt. Bei einem Grid von 2x2 bedeutet das, dass jedes Feld 150x150 hat um das 300x300 zu füllen.

Hier ist daher wirklich die Frage, was Du genau erreichen willst. Wenn du eine bestimmte Größe beim Spielfeld haben willst, dann könntest Du das Fenster genau auf die benötigte Größe setzen.
Oder - was üblicher ist: Du packst da noch mehr Elemente ineinander. Dann ist das Spielfeld in einem JPanel und das kann dann eine feste Größe haben und in einem Fenster zentriert angezeigt werden.

Das sind nur zwei der einfachen Ideen, die mir durch den Kopf gehen. Da kann man natürlich auf viele Ideen kommen, aber die zwei dürften mit die einfachsten Lösungen sein denke ich mir (Oder ich habe jetzt auf Anhieb eine andere Lösung übersehen. Evtl. weightx/weighty auf 0 setzen? Aber das wäre nicht, wie ich gewohnt bin, so UIs zu bauen.)
Herzlichen Dank,
in Bezug auf GUIs sind wir blutige Anfänger! Ich werde Deine Hinweise in den nächsten Tagen anwenden und sehen, wie wir weiterkommen! Noch einmal vielen Dank schon einmal! Herzlichst, Oliver
 

olimeck

Mitglied
Hallo,
die letzte Antwort hat uns sehr geholfen. Wir haben die einzelnen Teile unseres Codes noch einmal überdacht und... es hat funktioniert!
Jetzt wollen wir die farbigen Icons in unserem Programmcode durch eine "Kreis.png" Grafik ersetzen, indem wir unseren JLabels ein ImageIcon-Objekt zuordnen, entsprechend dem Konstruktor der Klasse JLabel "JLabel(Icon icon)" doch wir haben leider keine Ahnung, warum es mit unserem Ansatz nicht funktioniert. Die Grafik hat den Namen Kreis.png und liegt mit im Programmordner, sodass man über den Namen der Datei diese laden können sollte. Gibt es jemand, der unseren Fehler sieht und uns weiterhelfen könnte? Das wäre super!
Herzlichen Dank schon jetzt!


Code:
import java.awt.*;;
//import java.awt.Dimension;
//import java.awt.GridBagLayout;
//import java.awt.GridBagConstraints;
import javax.swing.*;
//import javax.swing.JLabel;
//import javax.swing.JPanel;
//import javax.swing.SwingUtilities;
//import javax.swing.ImageIcon;


public class Spielbrett{
    ImageIcon icon;
    public Spielbrett(){
        init();
    }
    
    private void init(){
        JPanel panel = new JPanel();
        createLayout(panel);
        JFrame frame = new JFrame();
        frame.add(panel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setTitle("Mensch ärgere Dich nicht!");
        frame.setVisible(true);
        ImageIcon icon = new ImageIcon("Kreis.png");
    }
    private void createLayout(JPanel panel){
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        gbl.setConstraints(panel, gbc);
        panel.setLayout(gbl);
        JLabel l = null;
        
        //gbc.gridx = 0;
        //gbc.gridy = 0;
        gbc.insets = new Insets(10,10,10,10);
        //l.setPreferredSize(new Dimension(30,30));
        //panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 1;
        //l.setPreferredSize(new Dimension(30,30));
        panel.add(createLabel(l, Color.YELLOW,"Gelb"),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 1;
        panel.add(createLabel(l, Color.YELLOW,"Gelb"),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 2;
        panel.add(createLabel(l, Color.YELLOW,"Gelb"),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 2;
        panel.add(createLabel(l, Color.YELLOW,"Gelb"),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.YELLOW,"Gelb"),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 3;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 4;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.YELLOW,"Gelb"),gbc);
        
        gbc.gridx = 3;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.YELLOW,"Gelb"),gbc);
        
        gbc.gridx = 4;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.YELLOW,"Gelb"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.YELLOW,"Gelb"),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 3;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 4;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 8;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 9;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 10;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 11;
        panel.add(createLabel(l, Color.GREEN,"Grün"),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 10;
        panel.add(createLabel(l, Color.GREEN,"Grün"),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 11;
        panel.add(createLabel(l, Color.GREEN,"Grün"),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 10;
        panel.add(createLabel(l, Color.GREEN,"Grün"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 11;
        panel.add(createLabel(l, Color.GREEN,"Grün"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 10;
        panel.add(createLabel(l, Color.GREEN,"Grün"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 9;
        panel.add(createLabel(l, Color.GREEN,"Grün"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 8;
        panel.add(createLabel(l, Color.GREEN,"Grün"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.GREEN,"Grün"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 11;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 11;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 10;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 9;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 8;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 8;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 9;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 7;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 11;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 10;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 11;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 10;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 9;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 8;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
    
        gbc.gridx = 11;
        gbc.gridy = 6;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);

        
        gbc.gridx = 9;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);

        gbc.gridx = 8;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 4;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 3;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 2;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 1;
        panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 1;
        panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 1;
        panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 2;
        panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 2;
        panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 2;
        panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 3;
        panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 4;
        panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.RED,"Rot"),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 1;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 1;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 2;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 3;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 4;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 5;
        panel.add(createLabel(l, Color.BLACK,"Schwarz"),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 11;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 10;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 11;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 10;
        panel.add(createLabel(l, Color.BLUE,"Blau"),gbc);
        


        //panel.setBorder(new EmptyBorder(0,100,100,100));
        
    }
    
    private JLabel createLabel(JLabel l, Color c, String txt){
        JLabel label = new JLabel(icon);
        label.setPreferredSize(new Dimension(30,30));
        label.setOpaque(true);
        label.setBackground(c);
        label.setText(txt);
        return label;
    }
    
    public static void main(String[]args){
        SwingUtilities.invokeLater(() -> new Spielbrett());
    }
}
 

KonradN

Super-Moderator
Mitarbeiter
Der Ansatz, so ein Bild per File zu laden, ist schon nicht gut. Statt dessen sollte die Datei als Ressource mit eingebunden werden und dann als Ressource geladen werden.

  • Die Datei gehört also zu den Ressourcen. (In Eclipse wäre es direkt im src Ordner, bei Maven / Gradle wäre es src/main/resources/)
  • Dann wird mit getClass().getResource("/Kreis.png") eine URL geholt.
  • Diese URL kannst Du dann ImageIcon im Konstruktor übergeben.

Das hat dann den Vorteil, dass das Arbeitsverzeichnis nicht gesetzt sein muss und so. Je nachdem, wie Du das Programm startest, ist dies aber nicht der Fall. So ein Problem vermute ich. Aber wir haben nicht genug Informationen so dass dies nur eine Vermutung ist.
 

olimeck

Mitglied
Java:
package MenschÄrgereDichNicht;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Spielbrett extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private ImageIcon icon;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Spielbrett frame = new Spielbrett();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Spielbrett() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(1000, 1000, 513, 474);
        contentPane = new JPanel();
        createLayout(contentPane);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setTitle("Mensch ärgere Dich nicht!");;
        this.setVisible(true);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        //icon = new ImageIcon(getClass().getResource("Kreis.png"));
        setContentPane(contentPane);
    }

    private void createLayout(JPanel panel) {
        // TODO Auto-generated method stub
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        gbl.setConstraints(panel, gbc);
        contentPane.setLayout(gbl);
        JLabel l = null;
        gbc.insets = new Insets(10,10,10,10);
        gbc.gridx = 1;
        gbc.gridy = 1;
        contentPane.add(createLabel(l, new ImageIcon(getClass().getResource("Kreis.png"))),gbc);
        gbc.gridx = 2;
        gbc.gridy = 1;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 2;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 2;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 3;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 4;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 3;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 4;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 3;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 4;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 8;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 9;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 10;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 11;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 10;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 11;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 2;
        gbc.gridy = 10;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 11;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 10;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 9;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 8;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 11;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 11;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 10;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 9;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 8;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 8;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 9;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 7;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 11;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 10;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 11;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 10;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 9;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 8;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
    
        gbc.gridx = 11;
        gbc.gridy = 6;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);

        
        gbc.gridx = 9;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);

        gbc.gridx = 8;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 4;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 3;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 7;
        gbc.gridy = 2;
        contentPane.add(createLabel(l, icon),gbc);       
        gbc.gridx = 7;
        gbc.gridy = 1;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 1;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 1;
        contentPane.add(createLabel(l, icon),gbc);       
        gbc.gridx = 10;
        gbc.gridy = 2;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 2;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 2;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 3;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 4;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 6;
        gbc.gridy = 1;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 1;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 2;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 3;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 4;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 5;
        gbc.gridy = 5;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 11;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 10;
        gbc.gridy = 10;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 11;
        contentPane.add(createLabel(l, icon),gbc);
        
        gbc.gridx = 11;
        gbc.gridy = 10;
        contentPane.add(createLabel(l, icon),gbc);
        

        
    }
    private JLabel createLabel(JLabel l, ImageIcon ii) {
        JLabel label = new JLabel(ii);
        label.setPreferredSize(new Dimension(30,30));       
        return label;
    }

}
Herzlichen Dank wieder einmal! Wir haben die Datei "Kreis.png" nun in Eclipse in den Source-Ordner gelegt. Allerdings erscheint das Icon trotzdem noch nicht auf den Labels. Eigentlich müsste es doch ausreichen, so habe ich es recherchiert, dann nur den Namen des PNG-Bildes in Anführungszeichen zu nennen... aber es klappt nicht. Auch der vorgeschlagene Code mit "getClass().getResource("/Kreis.png"); gab noch kein Resultat... Woran kann das liegen? Den ursprünglichen Code haben wir in BlueJ geschrieben, über das Ablegen von Icons in BlueJ haben wir nichts gefunden, also sind wir wieder zu Eclipse zurück...
 

KonradN

Super-Moderator
Mitarbeiter
Also erst einmal zur Erläuterung: Der "Pfad" muss dem Classloader angeben, wo auf dem Classpath die Datei zu finden ist. Bei einem "Kreis.png" würde im Classpath bei der Klasse geschaut und die ist im Namespace MenschÄrgereDichNicht. Daher ist "/Kreis.png" besser, denn dann ist klar: Die Datei liegt direkt auf dem Classpath.

Wenn ich Deinen Code anschaue, dann fällt mir auf den ersten Blick erst einmal auf, dass du (auskommentiert) im Konstruktor die Variable icon initialisierst aber die Variable bereits früher genutzt wird. Aber in Zeile 65 hast Du ja einen Aufruf und da sollte das Bild dann geladen worden sein. Was Du da schauen solltest:
a) Wirklich mit führenden Slash, also "/Kreis.png"nutzen.
b) Gross-/Kleinschreibung genau beachten! Die Datei darf also nicht kreis.png heissen sondern muss Kreis.png heissen.
c) Prüf einmal, ob Eclipse das Bild kopiert hat. Die Java Dateien werden ja zu .class Dateien übersetzt und da muss dann auch die Kreis.png liegen. Falls das nicht der Fall sein sollte, dann müsstet Du das Projekt mal komplett bauen.
 


Schreibe deine Antwort... und nutze den </> Button, wenn du Code posten möchtest...
Ähnliche Java Themen
  Titel Forum Antworten Datum
K GridBagLayout verändert die größe? AWT, Swing, JavaFX & SWT 1
D Swing Größe einer JComboBox im GridBagLayout aufgrund der maximalen Länge der enthaltenen Daten AWT, Swing, JavaFX & SWT 7
M LayoutManager GridBagLayout passt seine größe nicht an AWT, Swing, JavaFX & SWT 3
W GridBagLayout Größe geben AWT, Swing, JavaFX & SWT 1
R Größe eines Labels bei GridBagLayout festlegen AWT, Swing, JavaFX & SWT 9
N LayoutManager GridBagLayout Größe fixieren AWT, Swing, JavaFX & SWT 3
R Swing Button-Größe in JPanel mit GridBagLayout nicht änderbar AWT, Swing, JavaFX & SWT 3
P GridBagLayout - zwei Spalten mit gleicher Größe AWT, Swing, JavaFX & SWT 11
U LayoutManager GridBagLayout statische Größe AWT, Swing, JavaFX & SWT 2
hdi LayoutManager GridBagLayout : Komponente hat falsche Größe? AWT, Swing, JavaFX & SWT 2
J Dynamisches füllen von GridBagLayout: Größe der Komponenten AWT, Swing, JavaFX & SWT 4
P JTextArea veränder Größe während Laufzeit; GridBagLayout AWT, Swing, JavaFX & SWT 8
V JComboBox-Größe verändert sich im GridBagLayout AWT, Swing, JavaFX & SWT 4
C GridBagLayout - absolute Größe AWT, Swing, JavaFX & SWT 3
S GridBagLayout Felder formatieren AWT, Swing, JavaFX & SWT 1
S GridBagLayout - Probleme mit Bilderanzeige AWT, Swing, JavaFX & SWT 3
C GridbagLayout verstehen lernen AWT, Swing, JavaFX & SWT 1
H GridBagLayout macht mich wahnsinnig :-( AWT, Swing, JavaFX & SWT 5
BabySuna darstellungsprobleme mit JTabbedPane und GridBagLayout AWT, Swing, JavaFX & SWT 8
CptK Positionieren von Elementen in GridBagLayout AWT, Swing, JavaFX & SWT 4
A Probleme mit gridheight (GridBagLayout) AWT, Swing, JavaFX & SWT 6
Mario1409 AWT GridBagLayout AWT, Swing, JavaFX & SWT 5
J LayoutManager GridBagLayout, probleme mit Anordnung von Objekten AWT, Swing, JavaFX & SWT 6
DaCrazyJavaExpert Swing Komponenten in GridBagLayout werden Falsch angeordnet AWT, Swing, JavaFX & SWT 1
T LayoutManager Anordnen der Elemente im GridBagLayout AWT, Swing, JavaFX & SWT 11
K GridBagLayout mit reponsive Design AWT, Swing, JavaFX & SWT 2
B LayoutManager GridBagLayout und JScrollPane AWT, Swing, JavaFX & SWT 5
T GridBagLayout Anfängerprobleme AWT, Swing, JavaFX & SWT 3
Sin137 LayoutManager GridBagLayout Probleme AWT, Swing, JavaFX & SWT 6
L GridBagLayout Anordnung AWT, Swing, JavaFX & SWT 3
M Gridbaglayout Spaltenbreite AWT, Swing, JavaFX & SWT 3
V GridBagLayout AWT, Swing, JavaFX & SWT 4
N LayoutManager GridBagLayout - Grundlagen AWT, Swing, JavaFX & SWT 6
Neumi5694 Swing Gridbaglayout - automatische Anpassung verhindern AWT, Swing, JavaFX & SWT 1
P AWT Problem mit Platzierung (GridBagLayout) AWT, Swing, JavaFX & SWT 2
F Breite beim GridBagLayout festlegen AWT, Swing, JavaFX & SWT 2
M Swing GridBagLayout Komponentengröße festsetzen AWT, Swing, JavaFX & SWT 1
J GridBagLayout mit Hilfe einer For-Schleife befüllen AWT, Swing, JavaFX & SWT 1
HarleyDavidson Swing Seltsames Verhalten GridBagLayout AWT, Swing, JavaFX & SWT 11
W GridBagLayout mit fester Zellgrösse AWT, Swing, JavaFX & SWT 2
N Swing GridBagLayout: Ein Pixel Versatz AWT, Swing, JavaFX & SWT 2
B Swing Gridbaglayout unterschiedliche Zeilenhöhe AWT, Swing, JavaFX & SWT 6
H LayoutManager GridBagLayout AWT, Swing, JavaFX & SWT 1
N GridBagLayout - was fehlt? AWT, Swing, JavaFX & SWT 8
S Swing rowHeight und rowWeight im GridBagLayout AWT, Swing, JavaFX & SWT 1
N Swing GUI mit GridBagLayout AWT, Swing, JavaFX & SWT 4
A jpanel mit gridbaglayout auf hintergrundbild AWT, Swing, JavaFX & SWT 7
S GridBagLayout-Frage AWT, Swing, JavaFX & SWT 1
G GridBagLayout AWT, Swing, JavaFX & SWT 6
S GridBagLayout "links-rechts-layouten" AWT, Swing, JavaFX & SWT 7
T LayoutManager GridBagLayout / erwartetes Raster fehlt AWT, Swing, JavaFX & SWT 3
X Gridbaglayout gridx + gridy auslesen? AWT, Swing, JavaFX & SWT 7
H GridBagLayout macht Probleme... AWT, Swing, JavaFX & SWT 4
N GridBagLayout - Zeitplan AWT, Swing, JavaFX & SWT 13
N Swing GridbagLayout AWT, Swing, JavaFX & SWT 4
S Swing gridbaglayout AWT, Swing, JavaFX & SWT 8
G GridBagLayout Problem AWT, Swing, JavaFX & SWT 4
Java-Insel LayoutManager Ein GridBagLayout-Objekt für mehrere Panels? AWT, Swing, JavaFX & SWT 2
X LayoutManager gridBagLayout wird nicht richtig Dargestellt AWT, Swing, JavaFX & SWT 5
das-mo Probleme mit GridBagLayout AWT, Swing, JavaFX & SWT 6
T LayoutManager GridBagLayout - zwei jTable mit unterschiedlicher Höhe AWT, Swing, JavaFX & SWT 2
N LayoutManager GridBagLayout schummeln erlaubt ? AWT, Swing, JavaFX & SWT 2
D GridBagLayout AWT, Swing, JavaFX & SWT 9
A Swing GridBagLayout - constraints.anchor scheint nicht korrekt zu funktionieren? AWT, Swing, JavaFX & SWT 7
J Swing Terminkalender Wochenansicht mit Gridbaglayout oder JTable AWT, Swing, JavaFX & SWT 16
C LayoutManager GridBagLayout - Anfängerfrage AWT, Swing, JavaFX & SWT 5
Asamandra LayoutManager GridBagLayout - Komponenten (mit fill?) vergrößern aber Proportionen dabei erhalten? AWT, Swing, JavaFX & SWT 3
R GridBagLayout in GridBagLayout AWT, Swing, JavaFX & SWT 2
H Positionierungsprobleme beim GridBagLayout AWT, Swing, JavaFX & SWT 16
Furtano AWT GridBagLayout macht mir Sorgen AWT, Swing, JavaFX & SWT 3
A GridbagLayout positionierungsproblem AWT, Swing, JavaFX & SWT 4
earlgrey_tea GridBagLayout Componenten proportional vergößern AWT, Swing, JavaFX & SWT 12
D JTable im GridBagLayout -> gridwidth AWT, Swing, JavaFX & SWT 6
T GridBagLayout Problem AWT, Swing, JavaFX & SWT 3
D Probleme mit GridBagLayout AWT, Swing, JavaFX & SWT 8
J Swing GridBagLayout: Links-nach-rechts Orientierung statt zentriert AWT, Swing, JavaFX & SWT 12
B GridBagLayout Problem AWT, Swing, JavaFX & SWT 3
M LayoutManager GridBagLayout AWT, Swing, JavaFX & SWT 11
E LayoutManager GridBagLayout in BorderLayout - Abstand bei Resizing AWT, Swing, JavaFX & SWT 2
Y LayoutManager Keine vollständige Darstellung der Tabelle mit GridBagLayout AWT, Swing, JavaFX & SWT 3
L LayoutManager GridBagLayout leere Zeilen AWT, Swing, JavaFX & SWT 4
H LayoutManager Layout mit GridBagLayout machbar? AWT, Swing, JavaFX & SWT 6
N GridBagLayout Problem AWT, Swing, JavaFX & SWT 6
C Swing JTable "zerstört" GridBagLayout AWT, Swing, JavaFX & SWT 9
M GridBagLayout AWT, Swing, JavaFX & SWT 7
V Swing Gridbaglayout Leeres Fenster AWT, Swing, JavaFX & SWT 2
R LayoutManager GridBagLayout Fragen AWT, Swing, JavaFX & SWT 10
P LayoutManager Verständnis-Frage GridBagLayout AWT, Swing, JavaFX & SWT 7
M LayoutManager Einige Fragen zum GridBagLayout AWT, Swing, JavaFX & SWT 13
N GridBagLayout AWT, Swing, JavaFX & SWT 11
D Swing Problem mit Gridbaglayout bzw. Größenanpassung JPanels AWT, Swing, JavaFX & SWT 7
Y Swing GridbagLayout JTextfield zu klein AWT, Swing, JavaFX & SWT 5
L LayoutManager GridBagLayout spielt verrückt AWT, Swing, JavaFX & SWT 9
T LayoutManager GridBagLayout an Fenstergröße anpassen AWT, Swing, JavaFX & SWT 2
J Java GUI mit GridBagLayout AWT, Swing, JavaFX & SWT 3
Y LayoutManager Problem mit Gridbaglayout AWT, Swing, JavaFX & SWT 8
hdi LayoutManager GridBagLayout AWT, Swing, JavaFX & SWT 9
W GridBagLayout Falsche Größenanpassung AWT, Swing, JavaFX & SWT 6
E Problem mit meiner GUI/GridbagLayout AWT, Swing, JavaFX & SWT 2
M LayoutManager GradientPaint auf GridBagLayout AWT, Swing, JavaFX & SWT 5

Ähnliche Java Themen

Neue Themen


Oben