[英]Simple Java game crashes
The main problem that confused me is that i simply run the program it runs and Stucks on round3 i mean it simply doesn't work and shows error on line 136 but idk there are no compile errors but when you reach round 3 and try to click any of the buttons errors appear,and for some reason menubar doesn't appear here's the code 使我感到困惑的主要问题是,我只运行了它运行的程序,并在Round3上执行了Stucks操作,这意味着它根本无法正常工作,并在136行显示了错误,但idk没有编译错误,但是当您到达第3轮并尝试单击时出现任何按钮错误,并且由于某些原因菜单栏没有出现,这是代码
package com.tutorial.main;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Window extends JFrame{
private int round=1;
private int firstRand,rand2,rand3,rand4;
private JLabel winOrLose,or,firstLabel,secondLabel,thirdLabel,fourthLabel;
private JButton higher,lower;
private JMenuBar menubar;
private JMenu file;
private JMenuItem reset,exit;
public Window(){
firstRand=(int)(Math.random()*20+1);
Font font=new Font("Serif",Font.BOLD,16);
setLayout(new GridLayout(3,1));
menubar=new JMenuBar();
setJMenuBar(menubar);
file=new JMenu();
menubar.add(file);
reset=new JMenuItem("Restart");
file.add(reset);
exit=new JMenuItem("Quit");
file.add(exit);
systemClose s=new systemClose();
exit.addActionListener(s);
restartGame r=new restartGame();
reset.addActionListener(r);
Container pane=this.getContentPane();
// top panel setup
JPanel top=new JPanel();
top.setLayout(new GridLayout(1,4));
firstLabel=new JLabel(""+firstRand,SwingConstants.CENTER);
firstLabel.setFont(font);
top.add(firstLabel);
secondLabel=new JLabel("",SwingConstants.CENTER);
secondLabel.setFont(font);
top.add(secondLabel);
thirdLabel=new JLabel("",SwingConstants.CENTER);
thirdLabel.setFont(font);
top.add(thirdLabel);
secondLabel=new JLabel("",SwingConstants.CENTER);
secondLabel.setFont(font);
top.add(secondLabel);
pane.add(top);
// middle panel stup
JPanel middle=new JPanel();
middle.setLayout(new GridLayout(1,3));
higher=new JButton("HIGHER");
middle.add(higher);
or=new JLabel("OR",SwingConstants.CENTER);
middle.add(or);
lower=new JButton("LOWER");
middle.add(lower);
pane.add(middle);
event e=new event();
higher.addActionListener(e);
lower.addActionListener(e);
// Simplest one...bottom panel
JPanel bottom=new JPanel();
bottom.setLayout(new GridLayout(1,1));
winOrLose=new JLabel("",SwingConstants.CENTER);
winOrLose.setFont(font);
bottom.add(winOrLose);
pane.add(bottom);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent event){
String option=event.getActionCommand();
if(round==1){
rand2=(int)(Math.random()*20+1);
secondLabel.setText(""+rand2);
if(rand2>firstRand && option.equals("HIGHER")){
winOrLose.setText("Right,2 more!");
}else if(rand2<firstRand && option.equals("HIGHER")){
winOrLose.setText("You Lost!");
lower.setEnabled(false);
higher.setEnabled(false);
}else if(rand2>firstRand && option.equals("LOWER")){
winOrLose.setText("You Lost!");
lower.setEnabled(false);
higher.setEnabled(false);
}else if(rand2<firstRand&& option.equals("LOWER")){
winOrLose.setText("Right,2 more!");
}
// Start Round 2
round=2;
}else if(round==2){
rand3=(int)(Math.random()*20+1);
thirdLabel.setText(""+rand3);
if(rand3>rand2 && option.equals("HIGHER")){
winOrLose.setText("Right,1 more!");
}else if(rand3<rand2 && option.equals("HIGHER")){
winOrLose.setText("You Lost!");
lower.setEnabled(false);
higher.setEnabled(false);
}else if(rand3>rand2 && option.equals("LOWER")){
winOrLose.setText("You Lost!");
lower.setEnabled(false);
higher.setEnabled(false);
}else if(rand3<rand2&& option.equals("LOWER")){
winOrLose.setText("Right,1 more!");
}
// Start Round 3
round=3;
}else if(round==3){
rand4=(int)(Math.random()*20+1);
fourthLabel.setText(""+rand4);
if(rand4>rand3 && option.equals("HIGHER")){
winOrLose.setText("You Won The Game!");
}else if(rand4<rand3 && option.equals("HIGHER")){
winOrLose.setText("You Lost!");
lower.setEnabled(false);
higher.setEnabled(false);
}else if(rand4>rand3 && option.equals("LOWER")){
winOrLose.setText("You Lost!");
lower.setEnabled(false);
higher.setEnabled(false);
}else if(rand4<rand3&& option.equals("LOWER")){
winOrLose.setText("You Won The Game!");
}
}
}
}
public class systemClose implements ActionListener{
public void actionPerformed(ActionEvent event){
System.exit(0);
}
}
public class restartGame implements ActionListener{
public void actionPerformed(ActionEvent event){
firstRand=(int)(Math.random()*20+1);
round=1;
higher.setEnabled(true);
lower.setEnabled(true);
firstLabel.setText(""+firstRand);
secondLabel.setText("");
thirdLabel.setText("");
fourthLabel.setText("");
winOrLose.setText("");
}
}
}
and i have Window opening in other file (Game.java) 我在其他文件中打开了窗口(Game.java)
package com.tutorial.main;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
public class Game {
public static void main(String args[]){
Window win=new Window();
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setSize(300,150);
win.setBackground(Color.BLACK);
win.setVisible(true);
win.setResizable(false);
}
}
This is a simple typo: 这是一个简单的错字:
firstLabel=new JLabel(""+firstRand,SwingConstants.CENTER);
firstLabel.setFont(font);
top.add(firstLabel);
secondLabel=new JLabel("",SwingConstants.CENTER);
secondLabel.setFont(font);
top.add(secondLabel);
thirdLabel=new JLabel("",SwingConstants.CENTER);
thirdLabel.setFont(font);
top.add(thirdLabel);
secondLabel=new JLabel("",SwingConstants.CENTER); // OOPS
secondLabel.setFont(font); // OOPS
top.add(secondLabel); // OOPS
In the fourth group, you should be using fourthLabel
! 在第四组中,您应该使用
fourthLabel
! Otherwise, it is uninitialised, thus you will get the NullPointerException
you mention. 否则,它不会被初始化,因此您将获得提到的
NullPointerException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.