简体   繁体   English

一个简单的Java游戏出错

[英]Error on a simple java game

I am working on a mole game. 我正在从事痣游戏。 You know, there are buttons with a mole picture and if you clck on the button with the mole you get points. 您知道,有一些带有痣图片的按钮,如果您点击带有痣的按钮,您将获得积分。 I haven't finished it, i am just trying to make the first button visible but i am getting the java.lang.reflect.invocationtargetexception error when i try to execute. 我还没有完成它,我只是想使第一个按钮可见,但是当我尝试执行时却遇到了java.lang.reflect.invocationtargetexception错误。 Any ideas will be highly appreciated. 任何想法将不胜感激。

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();  

}

    }

This code solves both immediate problems (the Frame and JApplet appear), but corrects none of the many other problems. 该代码同时解决了眼前的问题(的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();
    }
}

Update 更新资料

This code corrects many other problems in the source. 这段代码纠正了源代码中的许多其他问题。

  1. It allows the code to be run as either an applet or application by creating the GUI in a panel (which is then added to either). 通过在面板中创建GUI(然后将其添加到任一面板),它允许将代码作为applet或应用程序运行。 This is typically called an hybrid application/applet. 这通常称为混合应用程序/小程序。
  2. Instead of trying to set a size for the frame (which does not account for the frame decorations), this code sets the preferred size of the game itself. 而不是尝试为框架设置大小(不考虑框架装饰),此代码设置了游戏本身的首选大小。 The applet would specify a width/height in HTML. 小程序将在HTML中指定宽度/高度。
  3. It uses Swing based components exclusively. 它仅使用基于Swing的组件。

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM