在public void actionPerformed(ActionEvent e)中非法开始表达

时间:2015-04-11 06:27:45

标签: java swing compiler-errors

我在java中制作一个简单的游戏。但我得到错误"非法开始表达" in public void actionPerformed(ActionEvent e)。帮助提供解决方案。

 public class Game extends JFrame{
    CardLayout cl = new CardLayout();
    JPanel dasar = new JPanel(cl);

    public Game() {
        dasar.add(new level1(),"Level 1");
        add(dasar,BorderLayout.CENTER);
    }

    public static void main(String [] args){
        Game G = new Game();
        G.setTitle("PuzzleGame");
        G.setSize(500,500);
        G.setLocationRelativeTo(null);
        G.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        G.setVisible(true);
    }
}

卡片布局类

class level1 extends JPanel implements ActionListener{
public level1(){
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,sample;
Icon star;
star = b9.getIcon();

Icon ic0 = new ImageIcon("Sample.jpg");
Icon ic1 = new ImageIcon("1.jpg");
Icon ic2 = new ImageIcon("2.jpg");
Icon ic3 = new ImageIcon("3.jpg");
Icon ic4 = new ImageIcon("4.jpg");
Icon ic5 = new ImageIcon("5.jpg");
Icon ic6 = new ImageIcon("6.jpg");
Icon ic7 = new ImageIcon("7.jpg");
Icon ic8 = new ImageIcon("8.jpg");
Icon ic9 = new ImageIcon("*.jpg");

    setLayout(new GridBagLayout());
    GridBagConstraints gb = new GridBagConstraints();

    gb.fill = GridBagConstraints.BOTH;
    gb.anchor = GridBagConstraints.CENTER;

    gb.gridx = 0;
    gb.gridy = 0;
    gb.gridwidth = 1;
    gb.gridheight = 1;
    gb.weightx = 1;
    gb.weighty = 1;
    b1 = new JButton(ic2);
    add(b1,gb);

    gb.gridx = 1;
    b2 = new JButton(ic1);
    add(b2,gb);

    gb.gridx = 2;
    b3 = new JButton(ic4);
    add(b3,gb);

    gb.gridx = 0;
    gb.gridy = 1;
    gb.gridwidth = 1;
    gb.gridheight = 1;
    gb.weightx = 1;
    gb.weighty = 1;
    b4 = new JButton(ic3);
    add(b4,gb);

    gb.gridx = 1;
    b5 = new JButton(ic8);
    add(b5,gb);

    gb.gridx = 2;
    b6 = new JButton(ic5);
    add(b6,gb);

    gb.gridx = 0;
    gb.gridy = 2;
    gb.gridwidth = 1;
    gb.gridheight = 1;
    gb.weightx = 1;
    gb.weighty = 1;
    b7 = new JButton(ic7);

    gb.gridx = 1;
    b8 = new JButton(ic6);
    add(b8,gb);

    gb.gridx = 2;
    b9 = new JButton(ic9);
    add(b9,gb);

    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);

}

}

这是我在公共关卡1中提出的动作事件

public void actionPerformed (ActionEvent e)
{
        if (e.getSource() == b1){
            Icon s1 = b1.getIcon();
            if(b2.getIcon() == star){
                b2.setIcon(s1);
                b1.setIcon(star);
            }
            else if (b4.getIcon() == star){
                b4.setIcon(s1);
                b1.setIcon(star);
            }
        }
        if (e.getSource() == b2){
            Icon s1 = b2.getIcon();
            if(b1.getIcon() == star){
                b1.setIcon(s1);
                b2.setIcon(star);
            }
            else if (b5.getIcon() == star){
                b5.setIcon(s1);
                b2.setIcon(star);
            }
            else if (b3.getIcon() == star){
                b3.setIcon(s1);
                b2.setIcon(star);
            }
        }
        if (e.getSource() == b3){
            Icon s1 = b3.getIcon();
            if (b2.getIcon() == star){
                b2.setIcon(s1);
                b3.setIcon(star);
            }
            else if (b6.getIcon() == star){
                b6.setIcon(s1);
                b3.setIcon(star);
            }
        }
        if (e.getSource() == b4){
            Icon s1 = b4.getIcon();
            if (b1.getIcon() == star){
                b1.setIcon(s1);
                b4.setIcon(star);
            }
            else if (b5.getIcon() == star){
                b5.setIcon(s1);
                b4.setIcon(star);
            }
            else if (b7.getIcon() == star){
                b7.setIcon(s1);
                b4.setIcon(star);
            }
        }
        if (e.getSource() == b5){
            Icon s1 = b5.getIcon();
            if (b2.getIcon() == star){
                b2.setIcon(s1);
                b5.setIcon(star);
            }
            else if (b6.getIcon() == star){
                b6.setIcon(s1);
                b5.setIcon(star);
            }
            else if (b4.getIcon() == star){
                b4.setIcon(s1);
                b5.setIcon(star);
            }
            else if (b8.getIcon() == star){
                b8.setIcon(s1);
                b5.setIcon(star);
            }
        }
        if (e.getSource() == b6){
            Icon s1 = b6.getIcon();
            if (b3.getIcon() == star){
                b3.setIcon(s1);
                b6.setIcon(star);
            }
            else if (b5.getIcon() == star){
                b5.setIcon(s1);
                b6.setIcon(star);
            }
            else if (b9.getIcon() == star){
                b9.setIcon(s1);
                b6.setIcon(star);
            }
        }
        if (e.getSource() == b7){
            Icon s1 = b7.getIcon();
            if (b4.getIcon() == star){
                b4.setIcon(s1);
                b7.setIcon(star);
            }
            else if (b8.getIcon() == star){
                b8.setIcon(s1);
                b7.setIcon(star);
            }
        }
        if (e.getSource() == b8){
            Icon s1 = b8.getIcon();
            if(b5.getIcon() == star){
                b5.setIcon(s1);
                b8.setIcon(star);
            }
            else if (b7.getIcon() == star){
                b7.setIcon(s1);
                b8.setIcon(star);
            }
            else if (b9.getIcon() == star){
                b9.setIcon(s1);
                b8.setIcon(star);
            }
        }
        if (e.getSource() == b9){
            Icon s1 = b9.getIcon();
            if(b6.getIcon() == star){
                b6.setIcon(s1);
                b9.setIcon(star);
            }
            else if (b8.getIcon() == star){
                b8.setIcon(s1);
                b9.setIcon(star);
            }
        }
    }

请给我解决方案

1 个答案:

答案 0 :(得分:0)

starb1b2b3仅存在于level1构造函数的范围内。如果以后需要它们(即在动作处理程序中进行比较),请在类级别定义它们:

class level1 extends JPanel implements ActionListener{
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,sample;
    Icon star;
    [...]
    public level1(){
        this.b1 = createButton(1);
        [...]

actionPerformed方法中,您可以像这样访问它们:

    if (e.getSource() == this.b1){
        [...]