-9

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 ++;
        }
    });
   //...................................................................................................................................
}

}

4

1 回答 1

2

一个final变量是,嗯,final。这意味着它的值在初始化后不能更改(或者,在对象类型的情况下,它的引用不能更改)。

参考:http ://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4

它不起作用的原因++是它实际上是重新分配(sp = sp + 1

编辑

此外,不将其声明为 final不起作用的原因是您在匿名类中使用它。为了能够在匿名类中使用它,必须声明它final,因为编译器需要确保引用实际上是它期望的引用。如果它可以改变运行时,匿名类本质上将操作错误的变量。

但是,由于您试图在这个 anymous 类中重新分配它,所以它不能是最终的......所以你想要做的事情是不可能的。我建议你创建一个新的类来跟踪你的sp,然后increment()在这个对象上创建一个方法。

于 2013-04-29T12:53:22.930 回答