我應該在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
}
}
}
我會很感激的幫助:)。
不要在標題中加入「已解決」,只是接受解決您問題的答案。 – user1803551