Problem mit Bild neu laden

Status
Nicht offen für weitere Antworten.

castor

Mitglied
Hallo,
ich habe folgendes Problem:

Mein Programm hat in der Oberfläche ein "JLabel" in dem ich Bilder(icons) reinlade.

Klappt hier auch ganz gut:

[HIGHLIGHT="Java"]
public void initGUI() {
try {
GridBagLayout thisLayout = new GridBagLayout();
this.setPreferredSize(new java.awt.Dimension(1086, 683));
thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
thisLayout.rowHeights = new int[] {36, 400, 16, 60, 5, 60, 5, 60};
thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.1};
thisLayout.columnWidths = new int[] {25, 500, 30, 500, 7};
this.setLayout(thisLayout);
{

{
//Label für rechtes Bild
rechtesBild = new JLabel();
this.add(rechtesBild, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
rechtesBild.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
}
{
jButton_Dilation = new JButton();
this.add(jButton_Dilation, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
jButton_Dilation.setText("Dilation");
jButton_Dilation.setPreferredSize(new java.awt.Dimension(100, 25));

jButton_Dilation.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String iteration = jTextField_Dilation.getText();
int it = Integer.parseInt(iteration);

dj.dilateBerechnung(it);

BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\temp.png")); }
catch (IOException ee)
{
ee.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);

rechtesBild.setIcon(icon);

image = null;
icon = null;
}
}
);
}
[/HIGHLIGHT]


Wenn ich da den Button "Diation" drücke, dann wird das neue Image im rechten Label angezeigt.

Jetzt kommt das Problem: im linken Label möchte ich mir ein neues Image reinladen, genau wie im rechten. Nur das ich kein Button habe, sondern mir über einen FileDialog eine Datei auswähle.

Das auswählen klappt auch, nur wird mir das Bild nicht angezeigt.

Klappt da was mit dem Übergeben nicht, oder muss ich das GUI neuladen?

[HIGHLIGHT="Java"]
public static void openFile()
{
JFileChooser fc = new JFileChooser();
Oberflaeche of = new Oberflaeche();

fc.setFileFilter(new FileFilter()
{
public boolean accept(File f)
{
return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg");
}
public String getDescription()
{
return "Bilder";
}
});

int returnVal = fc.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
// System.out.println(file.getName());

datei = file.getName();

BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\" + file.getName())); }
catch (IOException e)
{
e.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);
of.linkesBild.setIcon(icon);
}
}
[/HIGHLIGHT]

Ich hoffe ich habe den Quellcode nicht zu unübersichtlich reinkopiert.

Vielen Dank,
Jens
 
S

SlaterB

Gast
werden die Bilder geladen?
kannst du Höhe/ Breite ausgeben und sind das Werte > 0?

linker/ rechter Button/ Label habe ich nicht so genau verstanden, kann es sein dass das linke Label generell nicht zu sehen ist wegen eines Layout-Problems?
wenn du am Anfang überall Bilder setzt, klappt es dann?

ein vollständiges Programm wäre besser, kann ja nicht mehr viel fehlen wenn du nur die Label und Button in einen JFrame hast
 

castor

Mitglied
Hi,
Danke für die Antwort.

Leider ist noch ein bischen mehr in der Klasse an Code. (Ich poste trozdem mal)

Wenn ich das Bild (Icon) am Anfang setze, wird es Angezeigt. Nur wenn ich später ein neues Bild über die Dateiauswahl auswählen möchte, dann wird es nicht aktualisiert.

Selbst wenn ich am anfang kein Bild festlege, und es aktualisieren(Dateiauswahl) möchte, wird es auch nicht angezeigt.

(Wenn ich ein Testbutton (in der Methode initGUI()) einfüge, und dort statisch ein Bild (test.jpg) festlege, so das es im linken Label angezeigt wird und diesen zur Laufzeit drücke, wird es auch angezeigt.)

- In Zeile 102 - 118 erzeuge ich ein neues Label und zeige das "Startbild" an.
- Und ab 367 möchte ich, mit dem Dateiauswahlsystem, das Bild aktualisieren.


[HIGHLIGHT="Java"]
public class Oberflaeche extends javax.swing.JPanel {
public JPanel jPanel1;
public JLabel linkesBild;
private JSlider jSlider1_threshold;
public JButton Thresholds;
public JLabel rechtesBild;
public JPanel jPanel2;
private int thresholdValue = 180;
private JTextField jTextField_Erosion;
private JLabel jLabel_output;
private JLabel jLabel_input;
private JLabel jLabel_objekt = new JLabel();;
private JButton jButton_zaehlen;
private JPanel jPanel3;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel1;
private JTextField jTextField_Dilation;
private JButton jButton_Erosion;
private JButton jButton_Dilation;
private JLabel jLabel_ThresholdText;

Threshold th = new Threshold();
DilationJ dj = new DilationJ();
ErosionJ ej = new ErosionJ();
FloodFill ff = new FloodFill();

/**
* Auto-generated main method to display this
* JPanel inside a new JFrame.
*/
public static void main(String[] args)
{
JMenuBar mbar;
JMenu menuDatei; //Menüelement "Datei"
JMenuItem item;



//Menübar mit Elementen versehen
mbar = new JMenuBar();
menuDatei = Menueleiste.addMenuBarItem(mbar, "_Datei");

ActionListener actionProcCmd = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openFile();
}
};

//Elemente der Menüleiste mit Sub-Elementen versehen
item = Menueleiste.addMenuItem(menuDatei, "_Bild laden", 'O', actionProcCmd);
Menueleiste.addMenuItem(menuDatei, "-");

// menuDatei.addSeparator();

item = Menueleiste.addMenuItem(menuDatei, "_Beenden", 'B');
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});


JFrame frame = new JFrame();
frame.getContentPane().add(new Oberflaeche());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setJMenuBar(mbar);
frame.pack();
frame.setVisible(true);
}

public Oberflaeche() {
super();
initGUI();
}

//Getter und Setter für Threshold value
private void setThresholdvalue(int value)
{
thresholdValue = value;
}
private int getThresholdvalue()
{
return thresholdValue;
}


public void initGUI() {
try {
GridBagLayout thisLayout = new GridBagLayout();
this.setPreferredSize(new java.awt.Dimension(1086, 683));
thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
thisLayout.rowHeights = new int[] {36, 400, 16, 60, 5, 60, 5, 60};
thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.1};
thisLayout.columnWidths = new int[] {25, 500, 30, 500, 7};
this.setLayout(thisLayout);
{
//Label für linkes Bild
BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\Surirelasublinearis.jpg")); }
catch (IOException e)
{
e.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);


linkesBild = new JLabel(icon);
this.add(linkesBild, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
linkesBild.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
}
{
//Label für rechtes Bild
rechtesBild = new JLabel();
this.add(rechtesBild, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
rechtesBild.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
}
{
Thresholds = new JButton();
this.add(Thresholds, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
Thresholds.setText("Threshold");
Thresholds.setPreferredSize(new java.awt.Dimension(100, 25));
Thresholds.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

th.makeThreshold("D:\\TEMP\\Surirelasublinearis.jpg", getThresholdvalue());

// GraustufenWandler gw = new GraustufenWandler();
// try {
// gw.make();
// } catch (IOException e2) {
// // TODO Auto-generated catch block
// e2.printStackTrace();
// }

BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\temp.png")); }
catch (IOException ee)
{
ee.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);

rechtesBild.setIcon(icon);

image = null;
icon = null;
}
}
);


}
{
jSlider1_threshold = new JSlider();
this.add(jSlider1_threshold, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
jSlider1_threshold.setPreferredSize(new java.awt.Dimension(150, 40));
jSlider1_threshold.setMinimum(0);
jSlider1_threshold.setMaximum(360);
jSlider1_threshold.setPaintLabels(true);
jSlider1_threshold.setMajorTickSpacing(180);
// jSlider1_threshold.setMinimumSize(new java.awt.Dimension(36, 16));
jSlider1_threshold.setDoubleBuffered(true);
jSlider1_threshold.setValue(180);

jSlider1_threshold.addChangeListener(new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent ce) {
// TODO Auto-generated method stub
JSlider s = (JSlider)ce.getSource();
setThresholdvalue(s.getValue());
jLabel_ThresholdText.setText("" + s.getValue());
// th.makeThreshold("D:\\DSC00002.jpg", getThresholdvalue());
}
});
}
{
jLabel_ThresholdText = new JLabel();
this.add(jLabel_ThresholdText, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 280, 0, 0), 0, 0));
jLabel_ThresholdText.setForeground(new java.awt.Color(192,192,192));
jLabel_ThresholdText.setText("180");
}
{
jButton_Dilation = new JButton();
this.add(jButton_Dilation, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
jButton_Dilation.setText("Dilation");
jButton_Dilation.setPreferredSize(new java.awt.Dimension(100, 25));

jButton_Dilation.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String iteration = jTextField_Dilation.getText();
int it = Integer.parseInt(iteration);

System.out.println(it);

dj.dilateBerechnung(it);

BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\temp.png")); }
catch (IOException ee)
{
ee.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);

rechtesBild.setIcon(icon);

image = null;
icon = null;
}
}
);
}
{
jButton_Erosion = new JButton();
this.add(jButton_Erosion, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
jButton_Erosion.setText("Erosion");
jButton_Erosion.setPreferredSize(new java.awt.Dimension(100, 25));

jButton_Erosion.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String iteration = jTextField_Erosion.getText();
int it = Integer.parseInt(iteration);

ej.erosionBerechnung(it);

BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\temp.png")); }
catch (IOException ee)
{
ee.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);

rechtesBild.setIcon(icon);

image = null;
icon = null;
}
}
);
}
{
jTextField_Dilation = new JTextField();
this.add(jTextField_Dilation, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 10, 0, 200), 0, 0));
jTextField_Dilation.setText("0");
jTextField_Dilation.setPreferredSize(new java.awt.Dimension(30, 22));
}
{
jTextField_Erosion = new JTextField();
this.add(jTextField_Erosion, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 190), 0, 0));
jTextField_Erosion.setText("0");
jTextField_Erosion.setPreferredSize(new java.awt.Dimension(30, 22));
}
{
jLabel1 = new JLabel();
this.add(jLabel1, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
jLabel1.setBorder(BorderFactory.createTitledBorder(""));
}
{
jLabel3 = new JLabel();
this.add(jLabel3, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
jLabel3.setBorder(BorderFactory.createTitledBorder(""));
}
{
jLabel2 = new JLabel();
this.add(jLabel2, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
jLabel2.setBorder(BorderFactory.createTitledBorder(""));
}
{
jPanel3 = new JPanel();
GridBagLayout jPanel3Layout = new GridBagLayout();
jPanel3Layout.rowWeights = new double[] {0.1};
jPanel3Layout.rowHeights = new int[] {7};
jPanel3Layout.columnWeights = new double[] {0.0, 0.1};
jPanel3Layout.columnWidths = new int[] {295, 7};
jPanel3.setLayout(jPanel3Layout);
this.add(jPanel3, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
{
jButton_zaehlen = new JButton();
jPanel3.add(jButton_zaehlen, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
jButton_zaehlen.setText("Objekt zählen");

jButton_zaehlen.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

jPanel3.add(jLabel_objekt, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 20), 0, 0));
jLabel_objekt.setText("");

ff.regionLabeling();
ff.getLabelImage();

jLabel_objekt.setText(ff.getLabel() + " Objekt gefunden!");

ff.setLabel();

BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\temp_label.png")); }
catch (IOException ee)
{
ee.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);

rechtesBild.setIcon(icon);

image = null;
icon = null;
}
}
);
}

}
{
jLabel_input = new JLabel();
this.add(jLabel_input, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
jLabel_input.setText("Input:");
}
{
jLabel_output = new JLabel();
this.add(jLabel_output, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
jLabel_output.setText("Output:");
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void openFile()
{
JFileChooser fc = new JFileChooser();
Oberflaeche of = new Oberflaeche();

fc.setFileFilter(new FileFilter()
{
public boolean accept(File f)
{
return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg");
}
public String getDescription()
{
return "Bilder";
}
});

int returnVal = fc.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
// System.out.println(file.getName());

// of.datei = file.getName();

BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\" + file.getName())); }
catch (IOException e)
{
e.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);
of.linkesBild.setIcon(icon);
of.linkesBild.validate();
}
}

}
[/HIGHLIGHT]
 

Marco13

Top Contributor
Mit "Vollständig" war gemeint: Ein Programm, das nicht mehr als 100 Zeilen hat, und das eigentständig compilier- und ausführbar ist, und wo man nur einen Button klickt und dann das Problem sieht.
 
S

SlaterB

Gast
man kann ja auch großzügig streichen, aber wenn das Programm so verzahnt ist, dass etwa die Öffnen-Aktion in einer MenüBar steckt,
die ohne die fehlende Klasse MenueLeiste nicht zu kompilieren ist, dann gebe selbst ich auf ;)

ist
> Threshold th = new Threshold();
> DilationJ dj = new DilationJ();
> ErosionJ ej = new ErosionJ();
> FloodFill ff = new FloodFill();
wirklich zur Lösung des Problems nötig?
es ist gewiss nicht verboten, 5 Min. lang 90% des Programms einfach rauszulöschen,
wenn du hier eh Tage auf die Antwort warten musst, dann hast du auch die Zeit dafür

also auch von mir nochmal die Aufforderung:
einfaches Programm, wenige Buttons für die Aktionen, wenige Label, deren Layout, deren Bilder, fertig
kein Menu, kein Treshhold, keine Slider usw

mit etwas Pech tritt in dem gekürzten Programm das Problem gar nicht mehr so auf wie zuvor,
das kann man andererseits auch als Glück oder 'erfolgreicher Schritt in der Fehlersuche' bezeichnen ;)
 

castor

Mitglied
Also erst mal sry, ich habe das falsch Verstanden, ihr habt natürchlich Recht mit dem überflüssigen Code.

Dennoch würde ich die Menüleiste gerne drin lassen, denn gerade über die soll ja ein Bild geöffnet werden können und genau dort, tritt meiner Meinung nach das Problem auf.

Ich habe noch einen test-button eingefügt, wenn man auf den drückt, wird das Bild auch gewechselt...Nur eben nicht über die Menüleiste:

Klasse Oberflaeche:

[HIGHLIGHT="Java"]

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Oberflaeche extends javax.swing.JPanel {
public JPanel jPanel1;
public JLabel linkesBild;
public JButton test;
public JLabel rechtesBild;
public JPanel jPanel2;

/**
* Auto-generated main method to display this
* JPanel inside a new JFrame.
*/
public static void main(String[] args)
{
JMenuBar mbar;
JMenu menuDatei; //Menüelement "Datei"
JMenuItem item;

//Menübar mit Elementen versehen
mbar = new JMenuBar();
menuDatei = Menueleiste.addMenuBarItem(mbar, "_Datei");

ActionListener actionProcCmd = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openFile();
}
};

//Elemente der Menüleiste mit Sub-Elementen versehen
item = Menueleiste.addMenuItem(menuDatei, "_Bild laden", 'O', actionProcCmd);
Menueleiste.addMenuItem(menuDatei, "-");

// menuDatei.addSeparator();

item = Menueleiste.addMenuItem(menuDatei, "_Beenden", 'B');
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});


JFrame frame = new JFrame();
frame.getContentPane().add(new Oberflaeche());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setJMenuBar(mbar);
frame.pack();
frame.setVisible(true);
}

public Oberflaeche() {
super();
initGUI();
}


public void initGUI() {
try {
GridBagLayout thisLayout = new GridBagLayout();
this.setPreferredSize(new java.awt.Dimension(1086, 683));
thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
thisLayout.rowHeights = new int[] {36, 400, 16, 60, 5, 60, 5, 60};
thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.1};
thisLayout.columnWidths = new int[] {25, 500, 30, 500, 7};
this.setLayout(thisLayout);
{
//Label für linkes Bild
BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\DSC00002.JPG")); }
catch (IOException e)
{
e.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);


linkesBild = new JLabel(icon);
this.add(linkesBild, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
linkesBild.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
}
{
test = new JButton();
this.add(test, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
test.setText("Drück mich");
test.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\DSC00011.JPG")); }
catch (IOException ee)
{
ee.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);

linkesBild.setIcon(icon);

image = null;
icon = null;
}
}
);
}

} catch (Exception e) {
e.printStackTrace();
}
}

public static void openFile()
{
JFileChooser fc = new JFileChooser();
Oberflaeche of = new Oberflaeche();

fc.setFileFilter(new FileFilter()
{
public boolean accept(File f)
{
return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg");
}
public String getDescription()
{
return "Bilder";
}
});

int returnVal = fc.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
System.out.println(file.getName());

BufferedImage image;
try { image = ImageIO.read(new File("D:\\TEMP\\" + file.getName())); }
catch (IOException e)
{
e.printStackTrace();
throw new RuntimeException();
}

Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

ImageIcon icon = new ImageIcon(scaled1);
of.linkesBild.setIcon(icon);
of.linkesBild.validate();
}
}

}
[/HIGHLIGHT]


Klasse Menueleiste:

[HIGHLIGHT="Java"]

import java.io.*;
import java.awt.event.*;
import javax.swing.*;

public class Menueleiste
{
public static JMenu addMenuBarItem(JMenuBar menuBar, String s)
{
JMenu menu;

if(s.indexOf("_") > -1)
{
int pos = s.indexOf("_");
char c = s.charAt(pos+1);
StringBuffer sb = new StringBuffer(s).delete(pos, pos+1);
menu = new JMenu(sb.toString());
menu.setMnemonic(c);
}
else
menu = new JMenu(s);

menuBar.add(menu);

return menu;
}

private static JMenuItem processMnemonic(String s)
{
if(s.indexOf("_") > -1)
{
int pos = s.indexOf("_");
char c = s.charAt(pos+1);
StringBuffer sb = new StringBuffer(s).delete(pos, pos+1);
return new JMenuItem(sb.toString(),c);
}
else
return new JMenuItem(s);
}

public static JMenuItem addMenuItem(JMenu m, String s, char keyChar, ActionListener al)
{
if(s.startsWith("-"))
{
m.addSeparator();
return null;
}

JMenuItem menuItem = processMnemonic(s);

m.add(menuItem);

if(keyChar != 0)
menuItem.setAccelerator(KeyStroke.getKeyStroke(keyChar, InputEvent.CTRL_MASK));

if(al != null)
menuItem.addActionListener(al);

return menuItem;
}

public static JMenuItem addMenuItem(JMenu m, String s, char c)
{
return addMenuItem(m, s, c, null);
}

public static JMenuItem addMenuItem(JMenu m, String s)
{
return addMenuItem(m, s, (char)0, null);
}

public static JMenuItem addMenuItm(JMenu m, String s, ActionListener al)
{
return addMenuItem(m, s, (char)0, al);
}
}

[/HIGHLIGHT]


Vielen Dank
 

Marco13

Top Contributor
*zusammenkopier*
*pfade anpass*
Code:
public static void openFile()
          {
              JFileChooser fc = new JFileChooser();
              Oberflaeche of = new Oberflaeche();
Da wird eine neue Oberflaeche angelegt. Die hat mit der, die auf dem Bildschirm zu sehen ist, nichts zu tun.

Abhilfe: Methode nicht-static machen, und das Objekt, auf dem die Methode aufgerufen werden soll, vorher erstellen
Code:
// Von http://www.java-forum.org/awt-swing-swt/80786-problem-mit-bild-neu-laden.html

      import java.io.*;
      import java.awt.event.*;
      import javax.swing.*;

      import java.awt.GridBagConstraints;
      import java.awt.GridBagLayout;
      import java.awt.Image;
      import java.awt.Insets;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.IOException;

      import javax.imageio.ImageIO;
      import javax.swing.BorderFactory;

      import javax.swing.ImageIcon;
      import javax.swing.JButton;
      import javax.swing.JFileChooser;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.JPanel;
      import javax.swing.WindowConstants;
      import javax.swing.border.BevelBorder;
      import javax.swing.filechooser.FileFilter;
      import javax.swing.JFrame;
      import javax.swing.JLabel;


      public class Oberflaeche extends javax.swing.JPanel {
          public JPanel jPanel1;
          public JLabel linkesBild;
          public JButton test;
          public JLabel rechtesBild;
          public JPanel jPanel2;

          /**
          * Auto-generated main method to display this
          * JPanel inside a new JFrame.
          */
          public static void main(String[] args)
          {

              final Oberflaeche of = new Oberflaeche();

              JMenuBar mbar;
              JMenu menuDatei;    //Menüelement "Datei"
              JMenuItem item;

              //Menübar mit Elementen versehen
              mbar = new JMenuBar();
              menuDatei = Menueleiste.addMenuBarItem(mbar, "_Datei");

              ActionListener actionProcCmd = new ActionListener()
              {
                  public void actionPerformed(ActionEvent e)
                  {
                      of.openFile();
                  }
              };

              //Elemente der Menüleiste mit Sub-Elementen versehen
              item = Menueleiste.addMenuItem(menuDatei, "_Bild laden", 'O', actionProcCmd);
              Menueleiste.addMenuItem(menuDatei, "-");

      //      menuDatei.addSeparator();

              item = Menueleiste.addMenuItem(menuDatei, "_Beenden", 'B');
              item.addActionListener(new ActionListener()
              {
                  public void actionPerformed(ActionEvent e)
                  {
                      System.exit(0);
                  }
              });


              JFrame frame = new JFrame();
              frame.getContentPane().add(of);
              frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
              frame.setJMenuBar(mbar);
              frame.pack();
              frame.setVisible(true);
          }

          public Oberflaeche() {
              super();
              initGUI();
          }


          public void initGUI() {
              try {
                  GridBagLayout thisLayout = new GridBagLayout();
                  this.setPreferredSize(new java.awt.Dimension(1086, 683));
                  thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
                  thisLayout.rowHeights = new int[] {36, 400, 16, 60, 5, 60, 5, 60};
                  thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.1};
                  thisLayout.columnWidths = new int[] {25, 500, 30, 500, 7};
                  this.setLayout(thisLayout);
                  {
                      //Label für linkes Bild
                      BufferedImage image;
                      try { image = ImageIO.read(new File("bild.png")); }
                      catch (IOException e)
                      {
                          e.printStackTrace();
                          throw new RuntimeException();
                      }

                      Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

                      ImageIcon icon = new ImageIcon(scaled1);


                      linkesBild = new JLabel(icon);
                      this.add(linkesBild, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                      linkesBild.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
                  }
                  {
                      test = new JButton();
                      this.add(test, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
                      test.setText("Drück mich");
                      test.addActionListener(
                              new ActionListener()
                              {
                                  public void actionPerformed(ActionEvent e)
                                  {
                                      BufferedImage image;
                                      try { image = ImageIO.read(new File("bildC.jpg")); }
                                      catch (IOException ee)
                                      {
                                          ee.printStackTrace();
                                          throw new RuntimeException();
                                      }

                                      Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

                                      ImageIcon icon = new ImageIcon(scaled1);

                                      linkesBild.setIcon(icon);

                                      image = null;
                                      icon = null;
                                  }
                              }
                          );
                  }

              } catch (Exception e) {
                  e.printStackTrace();
              }
          }

          public void openFile()
          {
              JFileChooser fc = new JFileChooser();
              //Oberflaeche of = new Oberflaeche();

              fc.setFileFilter(new FileFilter()
              {
                  public boolean accept(File f)
                  {
                      return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg");
                  }
                  public String getDescription()
                  {
                      return "Bilder";
                  }
              });

              int returnVal = fc.showOpenDialog(null);

              if(returnVal == JFileChooser.APPROVE_OPTION)
              {
                  File file = fc.getSelectedFile();
                  System.out.println(file.getName());

                  BufferedImage image;
                  try { image = ImageIO.read(new File(file.getName())); }
                  catch (IOException e)
                  {
                      e.printStackTrace();
                      throw new RuntimeException();
                  }

                  Image scaled1 = image.getScaledInstance(500, 400, Image.SCALE_SMOOTH );

                  ImageIcon icon = new ImageIcon(scaled1);
                  linkesBild.setIcon(icon);
                  linkesBild.validate();
              }
          }

}


class Menueleiste
      {
          public static JMenu addMenuBarItem(JMenuBar menuBar, String s)
          {
              JMenu menu;

              if(s.indexOf("_") > -1)
              {
                  int pos = s.indexOf("_");
                  char c = s.charAt(pos+1);
                  StringBuffer sb = new StringBuffer(s).delete(pos, pos+1);
                  menu = new JMenu(sb.toString());
                  menu.setMnemonic(c);
              }
              else
                  menu = new JMenu(s);

              menuBar.add(menu);

              return menu;
          }

          private static JMenuItem processMnemonic(String s)
          {
              if(s.indexOf("_") > -1)
              {
                  int pos = s.indexOf("_");
                  char c = s.charAt(pos+1);
                  StringBuffer sb = new StringBuffer(s).delete(pos, pos+1);
                  return new JMenuItem(sb.toString(),c);
              }
              else
                  return new JMenuItem(s);
          }

          public static JMenuItem addMenuItem(JMenu m, String s, char keyChar, ActionListener al)
          {
              if(s.startsWith("-"))
              {
                  m.addSeparator();
                  return null;
              }

              JMenuItem menuItem = processMnemonic(s);

              m.add(menuItem);

              if(keyChar != 0)
                  menuItem.setAccelerator(KeyStroke.getKeyStroke(keyChar, InputEvent.CTRL_MASK));

              if(al != null)
                  menuItem.addActionListener(al);

              return menuItem;
          }

          public static JMenuItem addMenuItem(JMenu m, String s, char c)
          {
              return addMenuItem(m, s, c, null);
          }

          public static JMenuItem addMenuItem(JMenu m, String s)
          {
              return addMenuItem(m, s, (char)0, null);
          }

          public static JMenuItem addMenuItm(JMenu m, String s, ActionListener al)
          {
              return addMenuItem(m, s, (char)0, al);
          }
}
 

castor

Mitglied
Also vielen, vielen Dank!!! :)


Ich bin echt beeindruckt :toll:

(Und danke das IHR euch die Zeit genommen habt!)


Klasse Forum hier^^

Mit freundlichen Grüßen,
jens
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
B Problem beim laden von Bild mit getResource() AWT, Swing, JavaFX & SWT 3
L Problem mit paint Methode und Bild AWT, Swing, JavaFX & SWT 7
V Bild wird nicht auf JPanel gezeichnet; Java Problem AWT, Swing, JavaFX & SWT 10
N Problem mit Bild AWT, Swing, JavaFX & SWT 4
A Problem mit Hintergrunds-Bild AWT, Swing, JavaFX & SWT 2
S Problem mit Scrollpane & Bild AWT, Swing, JavaFX & SWT 3
G Problem mit der Anzeige von jLabel. Unlesbar wenn der Text geändert wird. AWT, Swing, JavaFX & SWT 28
H 2D-Grafik Problem mit Paint AWT, Swing, JavaFX & SWT 1
S Layout - Problem AWT, Swing, JavaFX & SWT 1
Tassos JavaFX/Problem mit der Maussteuerung in Stackpane AWT, Swing, JavaFX & SWT 7
sserio Java Fx - Problem AWT, Swing, JavaFX & SWT 3
A Problem Spiel auf Panel der GUI zu bringen AWT, Swing, JavaFX & SWT 1
A JavaFX Controller Problem AWT, Swing, JavaFX & SWT 1
TheWhiteShadow JavaFX ListView Problem beim Entfernen von Elementen AWT, Swing, JavaFX & SWT 1
E LayoutManager Welcher Layout-Mix löst mein Problem? AWT, Swing, JavaFX & SWT 3
Umb3rus JavaFX Problem mit PropertyValueFactory: can not read from unreadable property AWT, Swing, JavaFX & SWT 1
T Problem mit paintComponent() AWT, Swing, JavaFX & SWT 17
AmsananKING Java Menü-Problem AWT, Swing, JavaFX & SWT 1
K JavaFX Resizing-Problem beim BorderLayout (Center Component) beim Arbeiten mit mehreren FXMLs AWT, Swing, JavaFX & SWT 2
G Instance OF Problem AWT, Swing, JavaFX & SWT 9
FrittenFritze Ein Problem mit der CSSBox, die Größe wird nicht angepasst AWT, Swing, JavaFX & SWT 5
M Problem mit dem Anzeigen von Frames im Vordergrund AWT, Swing, JavaFX & SWT 5
Badebay Problem mit JButton AWT, Swing, JavaFX & SWT 2
newJavaGeek Grid-Layout problem AWT, Swing, JavaFX & SWT 7
J JavaFX Löschen im Tabelview macht Problem AWT, Swing, JavaFX & SWT 15
JavaTalksToMe JavaFx ExekutorService Problem AWT, Swing, JavaFX & SWT 2
Zrebna Problem bei Eventhandling (Value soll nach jedem erneutem Klick gelöscht werden) AWT, Swing, JavaFX & SWT 4
B Problem mit JavaFX AWT, Swing, JavaFX & SWT 5
J css Problem AWT, Swing, JavaFX & SWT 5
B JavaFX habe mein Problem fett markiert AWT, Swing, JavaFX & SWT 2
A Swing Filter-Problem AWT, Swing, JavaFX & SWT 1
temi JavaFX Problem mit IntelliJ und JavaFx 11 unter XUbuntu AWT, Swing, JavaFX & SWT 3
L Java FX Problem mit Ubuntu 18 und JavaFx AWT, Swing, JavaFX & SWT 27
H JTable TableCellEditor-Problem AWT, Swing, JavaFX & SWT 0
kodela Swing Problem mit Warten-Dialog AWT, Swing, JavaFX & SWT 16
B JavaFx Scene Builder Problem AWT, Swing, JavaFX & SWT 2
B [Problem] Java öffnet Word-Datein nicht AWT, Swing, JavaFX & SWT 14
T DataBinding Problem AWT, Swing, JavaFX & SWT 5
Blender3D Problem mit € Symbol Font Gotham Windows 10 Swing AWT, Swing, JavaFX & SWT 11
T Problem mit JTable Sortierung AWT, Swing, JavaFX & SWT 2
J Problem mit Platfrom run later AWT, Swing, JavaFX & SWT 15
J Problem mit Platfrom run later AWT, Swing, JavaFX & SWT 0
D Swing SwingUtils / Thread Problem AWT, Swing, JavaFX & SWT 3
L JavaFX Problem beim Aufrufen einer Methode AWT, Swing, JavaFX & SWT 5
T Swing Problem mit Datum und FormattedTextField AWT, Swing, JavaFX & SWT 2
S AWT Java print dialog Problem AWT, Swing, JavaFX & SWT 0
olfibits JavaFX Problem mit HTMLEditor AWT, Swing, JavaFX & SWT 0
W SWT hover-background-problem with first column in TreeViewer AWT, Swing, JavaFX & SWT 0
M Problem mit Add JScrollPane AWT, Swing, JavaFX & SWT 25
Mario1409 Swing JTextArea scroll Problem AWT, Swing, JavaFX & SWT 0
N Swing Problem mit loop AWT, Swing, JavaFX & SWT 2
S Swing Problem mit Button und ActionListener AWT, Swing, JavaFX & SWT 5
S Swing & Clean und build Problem AWT, Swing, JavaFX & SWT 12
S JLabel setText() Problem AWT, Swing, JavaFX & SWT 6
I 2D-Grafik Problem beim Ändern der Farbe eine 2d Objekts AWT, Swing, JavaFX & SWT 3
G Swing Splitpane Problem AWT, Swing, JavaFX & SWT 1
F Problem mit der FXML Rectangle Shape AWT, Swing, JavaFX & SWT 2
N JavaFX Stranges Problem mit der Autoscroll-Eigenschaft von Textareas AWT, Swing, JavaFX & SWT 0
E Java FX FXML Problem mit html Scriptausführung AWT, Swing, JavaFX & SWT 2
J JavaFX Intersect Problem mit Shapes AWT, Swing, JavaFX & SWT 10
R JavaFX MediaPlayer AVI-Problem AWT, Swing, JavaFX & SWT 1
M Swing Problem mit ListCellRenderer AWT, Swing, JavaFX & SWT 7
D Problem mit JTable AWT, Swing, JavaFX & SWT 1
F GUI Auflösung ändern - Koordianten und Proportions Problem AWT, Swing, JavaFX & SWT 21
J Problem mit Button darstellung AWT, Swing, JavaFX & SWT 23
M Problem mit Layoutmanagern... Hilfe wäre sehr nett. AWT, Swing, JavaFX & SWT 2
S 2D-Grafik Problem mit Variablen AWT, Swing, JavaFX & SWT 4
7 JavaFX Problem beim Zeichnen eines Dreiecks in einem GUI AWT, Swing, JavaFX & SWT 6
M Swing AttributiveCellTableModel addRow() Problem AWT, Swing, JavaFX & SWT 1
J Swing Problem mit Graphics Methode AWT, Swing, JavaFX & SWT 4
N JavaFX Problem mit table multiple selection AWT, Swing, JavaFX & SWT 5
K CheckBox Problem AWT, Swing, JavaFX & SWT 5
Grevak DisplayMode Problem seit Windows 10 AWT, Swing, JavaFX & SWT 2
S Swing Eigene JComboBox Problem! AWT, Swing, JavaFX & SWT 1
B Swing Problem mit Bildpfad AWT, Swing, JavaFX & SWT 4
N Swing Problem beim Scrollen mit JScrollPane AWT, Swing, JavaFX & SWT 6
V Graphics g - drawOval problem mit background AWT, Swing, JavaFX & SWT 1
C AWT Problem mit Protokol Fenster AWT, Swing, JavaFX & SWT 0
M Swing pack() Problem mit Taskleiste AWT, Swing, JavaFX & SWT 4
N Swing Choice- Problem! AWT, Swing, JavaFX & SWT 8
Q "AWT-EventQueue-0" Exception Problem AWT, Swing, JavaFX & SWT 4
D jButton Problem, ein Rieser Button bedeckt das ganze frame AWT, Swing, JavaFX & SWT 1
A Problem: repaint() - Schleife AWT, Swing, JavaFX & SWT 3
J Anfänger GUI Problem bei der Ausführung eines sehr einfachen Programms AWT, Swing, JavaFX & SWT 2
P AWT Problem mit Platzierung (GridBagLayout) AWT, Swing, JavaFX & SWT 2
N Swing JTree Problem beim erstellen der Knoten AWT, Swing, JavaFX & SWT 0
N Swing CardLayout: Problem beim Wechsel zwischen den JPanels AWT, Swing, JavaFX & SWT 3
A Mini-Menu-Schriften. Ein Problem bei hohen DPI Zahlen AWT, Swing, JavaFX & SWT 2
Z Canvas in Frame einfügen. Problem mit 4-Gewinnt AWT, Swing, JavaFX & SWT 1
C Thread-/ Simulations- Problem AWT, Swing, JavaFX & SWT 18
G Swing Setvisible problem AWT, Swing, JavaFX & SWT 1
J JTabbedPane: close Button Problem AWT, Swing, JavaFX & SWT 2
Tom299 JavaFX -> fxmlLoader -> getResourceAsStream Problem AWT, Swing, JavaFX & SWT 1
T Problem: ComboBox und addItem AWT, Swing, JavaFX & SWT 5
M JTextArea wird nicht aktualisiert (ActionListener-Problem) AWT, Swing, JavaFX & SWT 1
T LayoutManager LookAndFeel-Problem AWT, Swing, JavaFX & SWT 4
F Problem mit Implementierung von Kollisionsabfrage AWT, Swing, JavaFX & SWT 5
vodkaz (javafx) Image Problem AWT, Swing, JavaFX & SWT 2
T Problem beim Zeichnen von Rechteck AWT, Swing, JavaFX & SWT 3
B JavaFX Problem bei Kamera / Group, gesamte Scene bewegt sich mit AWT, Swing, JavaFX & SWT 0

Ähnliche Java Themen

Neue Themen


Oben