Hallo Leute!
Ich habe ein fertiges Programm welches einen Fehler beinhaltet welcher bewirkt, dass das Programm nicht funktioniert. Ich hoffe hier kann mir schnell jemand helfen!!
[code=Java]
package Galerie;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.SwingWorker;
public class BilderAlbumGUI extends javax.swing.JFrame
{
private static final long serialVersionUID = 3924944672470552800L;
private String[] supportetTypes = new String[]{"png","gif","jpg","jpeg"};
private File file;
private List<BufferedImage>list = new ArrayList<BufferedImage>();
private int index=0;
public BilderAlbumGUI()
{
initComponents();
setLocationRelativeTo(null);
bar.setMinimum(0);
}
private void loadImages()
{
new Worker().execute();
}
private boolean isSupportet(String name)
{
for(String end:supportetTypes)
{
if(name.toLowerCase().endsWith(end.toLowerCase()))
{
System.out.println("File " + name + " loaded");
return true;
}
}
System.out.println("File " + name + " is not an Image");
return false;
}
private void openFileChooser()
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = chooser.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION)
{
file = chooser.getSelectedFile();
System.out.println(file.toString());
loadImages();
}
}
class ImageLabel extends JLabel
{
private static final long serialVersionUID = -2292257639281450634L;
private BufferedImage img;
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(img != null)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.drawImage(img, 0, 0, this.getWidth(), this.getHeight(),this);
}
}
public BufferedImage getImg()
{
return img;
}
public void setImg(BufferedImage img)
{
this.img = img;
repaint();
}
}
class Worker extends SwingWorker<Integer, String>
{
protected Integer doInBackground() throws Exception
{
bar.setVisible(true);
bar.setMaximum(file.listFiles().length);
bar.setValue(0);
index = 0;
list.clear();
lblImages.setImg(null);
for(File f:file.listFiles())
{
if(isSupportet(f.getName()))
{
try
{
list.add(ImageIO.read(f));
}
catch (IOException e)
{
e.printStackTrace();
}
}
bar.setValue(bar.getValue() + 1);
}
return 1;
}
protected void done()
{
bar.setVisible(false);
if(list.size() > 0)
{
lblImages.setImg(list.get(0));
// lblImages.setImg(list.get(index));
}
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ImagePanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
lblImages = new javax.swing.JLabel();
bar = new javax.swing.JProgressBar();
javax.swing.GroupLayout ImagePanelLayout = new javax.swing.GroupLayout(ImagePanel);
ImagePanel.setLayout(ImagePanelLayout);
ImagePanelLayout.setHorizontalGroup(
ImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
ImagePanelLayout.setVerticalGroup(
ImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("<");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onbtPrev(evt);
}
});
jButton3.setText(">");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onbtNext(evt);
}
});
jButton2.setText("Ordner auswählen");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onbtChoose(evt);
}
});
lblImages.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblImages.setText("Kein Bild geladen");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblImages, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
.addComponent(bar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(90, 90, 90)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblImages, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bar, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void onbtPrev(java.awt.event.ActionEvent evt) {
if(list!=null&&list.size()!=0)
{
index = index > 0 ? index - 1 : list.size() - 1;
lblImages.setImg(list.get(index));
}
}
private void onbtNext(java.awt.event.ActionEvent evt) {
if(list!=null&&list.size()!=0)
{
index = list.size() > index + 1 ? index + 1 : 0;
lblImages.setImg(list.get(index));
}
}
private void onbtChoose(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = chooser.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION)
{
file = chooser.getSelectedFile();
System.out.println(file.toString());
loadImages();
}
}
public static void main(String args[])
{
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(BilderAlbumGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BilderAlbumGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BilderAlbumGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BilderAlbumGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
loadStyle();
BilderAlbumGUI frame = new BilderAlbumGUI();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
private static void loadStyle()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException| IllegalAccessException | UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
}
// Variables declaration - do not modify
private javax.swing.JPanel ImagePanel;
private javax.swing.JProgressBar bar;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel lblImages;
// End of variables declaration
}
[/code]
Jeder Aufruf von .setImg() wird als falsch markiert und ich habe Null Ahnung warum...
Ps: Es kann durch aus sein dass ich einfach zu dumm bin den Fehler zu sehen als entschuldigt mcih in diesem Fall 
Grüße Tausendsassa