i would like to create a skill program, like in warcraft,,, my question is : the action listener is wants to final the integer sp ...but when i final the integer sp, it says that cannot change the value of a final integer....
Heres the code and please try to run it:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Skills
{
public static void main(String[] args)
{
//DECLARATION
int sp = 0;
JFrame skillframe = new JFrame("Skills");
final JPanel panel1 = new JPanel();
final JPanel panel2 = new JPanel();
ImageIcon s1 = new ImageIcon("skill1.jpg");
ImageIcon s2 = new ImageIcon("skill2.jpg");
ImageIcon s3 = new ImageIcon("skill3.jpg");
ImageIcon s4 = new ImageIcon("skill4.jpg");
ImageIcon s5 = new ImageIcon("stats.gif");
final JButton skills = new JButton("Skills");
JButton skill1 = new JButton(s1);
JButton skill2 = new JButton(s2);
JButton skill3 = new JButton(s3);
JButton skill4 = new JButton(s4);
JButton stats = new JButton(s5);
final JButton back = new JButton("BacK");
final JButton levelup = new JButton("Level UP!");
JLabel points = new JLabel("Skill Points : " + sp );
///SETTINGS :
// Skills
panel1.add(skills);
skills.setBounds(226, 82, 64, 64);
skills.setBackground(Color.black);
skills.setForeground(Color.white);
// Level UP!
panel1.add(levelup);
levelup.setBounds(10, 82, 100, 64);
levelup.setBackground(Color.black);
levelup.setForeground(Color.white);
// Skill 1
panel2.add(skill1);
skill1.setBounds(10, 10, 64, 64);
// Skill 2
panel2.add(skill2);
skill2.setBounds(82, 10, 64, 64);
// Skill 3
panel2.add(skill3);
skill3.setBounds(154, 10, 64,64);
// Skill 4
panel2.add(skill4);
skill4.setBounds(226, 10, 64, 64);
// Stats
panel2.add(stats);
stats.setBounds(10, 82, 64, 64);
// Back
panel2.add(back);
back.setBounds(226, 82, 64, 64);
back.setBackground(Color.black);
back.setForeground(Color.white);
// Points
panel2.add(points);
points.setBounds(100, 82, 100, 64);
points.setForeground(Color.white);
// Frame
skillframe.setSize(310,195);
panel1.setSize(310,195);
panel2.setSize(310,195);
skillframe.add(panel1);
skillframe.add(panel2);
panel1.setBackground(Color.black);
panel2.setBackground(Color.black);
panel1.setLayout(null);
panel2.setLayout(null);
panel2.setVisible(false);
skillframe.setVisible(true);
// ACTIONLISTENER
skills.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
panel1.setVisible(false);
panel2.setVisible(true);
//.......................................................................................................................................................
back.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
panel1.setVisible(true);
panel2.setVisible(false);
}
});
//.......................................................................................................................................................
}
});
//...........................................................................................................................................................
levelup.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sp ++;
}
});
//...................................................................................................................................
}
}