gpt4 book ai didi

java - JFrame打不开

转载 作者:行者123 更新时间:2023-12-01 10:46:29 24 4
gpt4 key购买 nike

我应该在 Driver 中调用 Frame 构造函数,并且 JFrame 应该打开。但是,我收到一条错误消息,指出游戏的局部变量(我的 Frame 对象)未使用。有谁知道为什么会发生这种情况?另外,我收到一条错误消息,说我需要序列化我的 Frame 类。那是什么?

这是我的驱动程序类:

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

}

}

这是我的框架类:

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
}
}
}

非常感谢您的帮助:)。

最佳答案

I am getting an error saying the local variable of game, my Frame object, is not used.

Also I am getting an error saying that I need to serialize my Frame class.

这不是错误,而是 warnings ,忽略它们即可。

不强制使用frame,因为您只是用它来显示。

如果您不打算将对象保存在某处,则添加 SerialID 也不是强制性的。

我还建议您在添加所有组件后在构造函数的末尾编写 setVisible(true)

关于java - JFrame打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34140609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com