2015-12-07 68 views
0

我應該在Driver中調用Frame構造函數,JFrame應該打開。但是,我收到一個錯誤,說沒有使用遊戲的局部變量,即我的Frame對象。有誰知道這是爲什麼發生?另外我得到一個錯誤,說我需要序列化我的Frame類。那是什麼?JFrame沒有打開

這是我的驅動程序類:

public class Driver { 
    public static void main(String[] args) { 
     Player create[] = new Player[2]; 
     create[0] = new Player(); 
     create[1] = new Enemy(); 

     for (int x = 0; x < 2; x++) { 
      create[x].CharacterCreate(); 
      //System.out.println(create[x].Print()); 
     } 
     Frame game = new Frame(); 

} 

} 

這是我的Frame類:

import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

import java.awt.GridLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class Frame extends JFrame 
{ 

    public Frame() 
    { 
     super(); 
     //Create Grid Layout 
     setSize(300, 200); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); //makes window visible 

     //Creates Grid Layout 
     GridLayout gl = new GridLayout(2,3); 
     setLayout(gl); 
     setTitle("HW11"); 
     //Creates panel 
     JPanel j = new JPanel(); 
     add (j); 

     //Creates Labels 
     playerLabel = new JLabel("Player Stats"); 
     add (playerLabel); 
     JLabel space1 = new JLabel(" "); 
     add (space1); 
     enemeyLabel = new JLabel("EnemyStats"); 
     add(enemeyLabel); 
     JLabel space2 = new JLabel(" "); 
     add (space2); 


     //Create attack button 
     JButton attackButton = new JButton("Attack"); 
     EndingListener updateStats = new EndingListener(); 
     attackButton.addActionListener(updateStats); 
     add(attackButton); //adds button to window 

    } 

    class EndingListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      update.Attack(update); 
      playerLabel.setText(update.Print()); 
      enemeyLabel.setText(update.Print()); 
      // if (Player.gethealth() == 0) 
       //System.exit(0); //terminates the program 
      // if (Enemy.gethealth()==0) 
      // System.exit(0); //terminates the program 
     } 
    } 
} 

我會很感激的幫助:)。

+0

不要在標題中加入「已解決」,只是接受解決您問題的答案。 – user1803551

回答

0

你不必擔心你所看到的信息。這些不是導致程序崩潰的錯誤,它們只是警告Eclipse句柄用來提醒用戶未使用的變量。你可以忽略你的情況。序列號警告在同一條船上。至於JFrame,請嘗試在構造函數中的其他每個命令後面輸入setVisible(true),以確保所添加的JFrame的所有元素均可見。另外,在你的主要方法中,嘗試在玩家之前創建框架。

+0

感謝您的幫助! – starlight

1

我收到一個錯誤,說沒有使用遊戲的局部變量,我的Frame 對象。

此外,我得到一個錯誤,說我需要序列化我的幀 類。

這不是錯誤,而是warnings,只是忽略它們。

使用frame不是強制性的,因爲您只是使用它來顯示。

如果您不打算將對象保存在某個地方,那麼添加SerialID也不是必需的。

我也建議你在添加所有組件後在構造函數的末尾寫上setVisible(true)

+0

好的,謝謝你的幫助! – starlight

+0

@starlight沒問題,不要忘記標記答案爲接受:)。 –