2017-04-06 72 views
0

讓我困惑的主要問題是,我只是運行它運行的程序和第3輪上的Stucks,我的意思是它根本不起作用,並顯示第136行的錯誤,但是idk沒有編譯錯誤,但是當你到達第3輪和嘗試點擊任何錯誤出現的按鈕,出於某種原因,菜單欄這裏沒有出現的代碼簡單的Java遊戲崩潰

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

和我在其他文件窗口中打開(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); 

    } 
} 
+1

如果您向我們展示第136行,將會更好。 – Jiahao

+1

錯誤是什麼? –

+0

您能否向我們提供您指定的錯誤和行? –

回答

2

這是一個簡單的錯字:

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 

在第四組中,您應該使用fourthLabel!否則,它是未初始化的,因此你會得到你提到的NullPointerException

+0

該死,我感到非常感謝幫助,第3輪的問題現在已經解決,但爲什麼菜單欄不顯示? – DummyTarget

+0

我對'JFrame'不熟悉,但是我認爲你應該在菜單項添加到'file'之後,而不是之前,'setJMenuBar(菜單欄);'菜單欄之後''menubar.add(文件);' 。新增(文件);'?請參閱[本示例呼叫順序](http://stackoverflow.com/a/29062045/1270789)。 –

+0

感謝您的幫助兄弟,我真的很糟糕的調試,我是初學者在Java,我不是自信,問題是與文件=新JMenu();因爲在菜單欄上應該出現名爲「File」的東西,所以我沒有設置名稱 它應該是file = new JMenu(「File」);非常感謝您的幫助! – DummyTarget