简单的java游戏出错

时间:2013-02-22 20:58:42

标签: java swing

我正在研究鼹鼠游戏。你知道,有一些带有痣图片的按钮,如果你用痣按下按钮就可以获得积分。我还没有完成它,我只是想让第一个按钮可见但是当我尝试执行时我收到java.lang.reflect.invocationtargetexception错误。任何想法都将受到高度赞赏。

import java.awt.event.*;    
import java.awt.*;
import java.applet.*;
import javax.swing.*;

public class topos extends JApplet implements ActionListener{
       JLabel puntaje;
       JButton topo;
       Container c;
       int contador=0;

public topos(){
    Frame f = new Frame ("El famoso juego de los topos");
    f.add("center", this);
    f.setSize (900,300);
    f.setVisible(true);
    }
public void init(){
    c = getContentPane();
    topo = new JButton (new ImageIcon("topo.jpg"));
    puntaje = new JLabel("0");
    topo.addActionListener(this);
    c.add("center",topo);
}
public void actionPerformed(ActionEvent e){
    JButton b = (JButton)e.getSource();
    try {
        if (b == topo){
        contador = contador + 1;
        puntaje.setText(" " + contador );
        }

    }
    catch (Exception f){
        f.printStackTrace();
    }
    }


public static void main (String s[]){
    topos t = new topos();
    t.init();
    t.start();  

}

    }

1 个答案:

答案 0 :(得分:2)

此代码解决了两个直接问题(显示FrameJApplet),但未纠正 许多 其他问题。

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;

public class topos extends JApplet implements ActionListener{
    JLabel puntaje;
    JButton topo;
    Container c;
    int contador=0;

    public topos(){
        Frame f = new Frame ("El famoso juego de los topos");
        f.add(this, BorderLayout.CENTER);
        f.setSize (900,300);
        f.setVisible(true);
    }

    public void init(){
        c = getContentPane();
        topo = new JButton (new ImageIcon("topo.jpg"));
        puntaje = new JLabel("0");
        topo.addActionListener(this);
        c.add(topo, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e){
        JButton b = (JButton)e.getSource();
        try {
            if (b == topo){
                contador = contador + 1;
                puntaje.setText(" " + contador );
            }
        }
        catch (Exception f){
            f.printStackTrace();
        }
    }

    public static void main (String s[]){
        topos t = new topos();
        t.init();
        t.start();
    }
}

更新

此代码更正了源中的许多其他问题。

  1. 它允许代码作为applet或应用程序运行,方法是在面板中创建GUI(然后将其添加到其中)。这通常称为混合应用程序/小程序。
  2. 此代码不是设置框架的大小(不考虑框架装饰),而是设置游戏本身的首选大小。小程序将以HTML格式指定宽度/高度。
  3. 它仅使用基于Swing的组件。

  4. import java.awt.event.*;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Color;
    import java.applet.*;
    import javax.swing.*;
    
    public class topos extends JApplet {
    
        public void init(){
            getContentPane().add(new WhackAMoleGUI(), BorderLayout.CENTER);
        }
    
        public static void main (String s[]){
            JFrame f = new JFrame ("El famoso juego de los topos");
            f.add( new WhackAMoleGUI(), BorderLayout.CENTER );
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.pack();
            f.setLocationByPlatform(true);
            f.setVisible(true);
        }
    }
    
    class WhackAMoleGUI extends JPanel implements ActionListener {
    
        final Dimension preferredSize = new Dimension(400, 200);
        JLabel puntaje;
        JButton topo;
        int contador=0;
    
        WhackAMoleGUI() {
            setLayout(new FlowLayout());
            topo = new JButton (new ImageIcon("topo.jpg"));
            add(topo);
            puntaje = new JLabel("0");
            add(puntaje);
            topo.addActionListener(this);
            setBackground(Color.YELLOW);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return preferredSize;
        }
    
        public void actionPerformed(ActionEvent e){
            JButton b = (JButton)e.getSource();
            try {
                if (b == topo){
                    contador = contador + 1;
                    puntaje.setText(" " + contador );
                }
            }
            catch (Exception f){
                f.printStackTrace();
            }
        }
    }