[英]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. 该代码同时解决了眼前的问题(的
Frame
和JApplet
出现),但没有校正的许多其他问题。
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();
}
}
This code corrects many other problems in the source. 这段代码纠正了源代码中的许多其他问题。
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.