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
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