【问题标题】:IN JAVA PROGRAMMING [closed]在 JAVA 编程中[关闭]
【发布时间】:2013-04-23 03:27:45
【问题描述】:

我想创建一个技能程序,例如在魔兽世界中,,, 我的问题是:动作监听器想要结束整数 sp ...但是当我结束整数 sp 时,它说不能改变最终整数的值......

这是代码,请尝试运行它:

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

}

【问题讨论】:

  • 英文“cannot change the value of a final integer”看不懂可以用google翻译一下
  • 你应该让 sp 变量成为类成员 private int sp;。此外,开始使用new ... 并将您的代码拆分为几个方法,而不是在main 中包含所有内容。最后,考虑使用 LayoutManager,而不是到处设置 null
  • 您应该更好地重新措辞和格式化您的问题,并将代码缩减到重点。
  • mam/先生..你能复制我的代码并运行它吗?^^..给我一些解决方案,,,因为我是一个初学者^^抱歉给您带来不便..

标签: java swing


【解决方案1】:

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

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

它不适用于 ++ 的原因是它实际上是重新分配 (sp = sp + 1)

编辑

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

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

【讨论】:

  • mam/ 先生..你能复制我的代码并运行它吗?^^..并给我一些解决方案,,,因为我是一个初学者^^抱歉给您带来不便..
  • 您已经测试了该解决方案。只是不要宣布它是最终的。
  • 但是当我没有声明它为final时,它说我需要最终int,,,,当我最终int,它说不能改变,,,
  • 我也试试这个 (sp = sp + 1) 但它不起作用..
  • 另一种解决方案是单元素最终数组。
猜你喜欢
  • 2011-03-24
  • 2015-05-27
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
  • 2011-03-10
相关资源
最近更新 更多