Credo che faccia meglio a mostrarvi tutto il codice:
codice:
public class PROVA {
public static void YourBuilds() {
Dimension d=new Dimension(710,737);
Champs k=new Champs();
String s[]=k.Champs();
double calc=s.length/7.0;
double calcF=Math.ceil(calc);
final String path=k.dir;
JFrame jfSaves = new JFrame("Your Builds");
JButton jb1;
jfSaves.setSize(d);
jfSaves.setLocationRelativeTo(null);
jfSaves.setVisible(true);
jfSaves.setResizable(true);
jfSaves.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = jfSaves.getContentPane();
jfSaves.setContentPane(c);
JPanel jp=new JPanel(new GridLayout(20,20,(int)calcF,7));
JScrollPane jsp = new JScrollPane(jp,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
ActionListener al=new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton source=(JButton)e.getSource();
if (source.equals(jb1)){ //Qui mi dà errore
System.out.println("*");
}
}
};
for(int i=0;i<s.length;i++) {
jb1=new JButton(new ImageIcon(((new ImageIcon(""+path+""+s[i]+".png",""+s[i])).getImage()).getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH)));
jb1.setText(s[i]);
jp.add(jb1);
jb1.addActionListener(al);
}
c.add(jsp);
jsp.setViewportView(jp);
c.revalidate();
//Ciclo infinito per adattare il jsp al frame
boolean g=true;
try{
while (g){
Thread m=new Thread();
m.wait(1000);
Dimension p=jfSaves.getSize();
jsp.setSize(p);
c.repaint();
c.revalidate();
}
}catch(InterruptedException ex){
System.err.println(ex.getMessage());
}
}
}
Questa classa poi la faccio eseguire nel main. L'errore me lo dà nella riga "if(source.equals(jb1))....." dicendomi "local variable jb1 is accessed from within inner class; needs to be declared final".Come detto prima, non posso definire i JButton final. Se ho fatto degli errori stupidi perdonatemi perfavore, ho ricominciato da poco a programmare in Java
.