简体   繁体   English

JFrame,JPanel和awt问题

[英]JFrame, JPanel, and awt problems

i am trying to make a simple java 2-d platformer. 我正在尝试制作一个简单的Java 2-D Platformer。 Java keeps giving me troubles some error "The serializable class Display does not declare a static final serialVersionUID field of type long" Can anybody help? Java不断给我带来一些错误“可序列化的类Display未声明类型为long的静态最终serialVersionUID字段”有人可以帮忙吗?

this is the error message I get when i try to run the program: http://i.imgur.com/H0Afv.png (on the website) 这是我尝试运行该程序时收到的错误消息: http : //i.imgur.com/H0Afv.png (在网站上)

Here is the code: 这是代码:

import javax.swing.*;

public class Main {

    public static Display f;
    public static int width = 800;
    public static int height = 600;

    public static void main(String[] args)
    {

        f = new Display();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(true);
        f.setSize(width, height);
        f.setLocationRelativeTo(null);
        f.setTitle("2-D Platformer");
    }


}
//This is my display class.

import java.awt.*;

import javax.swing.*;

/* this is where the error is
 *
 */           V 
public class Display extends JFrame{

    public Display p;
    public Display()
    {

        p = new Display();
        add(p);
        setLayout(new GridLayout(1, 1, 0 , 0));
    }
}
//and this is my display2 class
import java.awt.Color;

import javax.swing.*;

/* this is where the error is
 *too
 */            V 
public class Display2 extends JPanel{

    public Display2()
    {
        setBackground(Color.BLACK);
    }
}

It's not an error, it's a warning and it comes from your extending a serializable class (one of the GUI components that your class is extending) and not giving it final serialVersionUID field as the Serializable interface contract stipulates. 这不是错误,而是警告,它来自您扩展可序列化的类(您的类正在扩展的GUI组件之一),并且未根据Serializable接口协定的规定将其提供给最终的serialVersionUID字段。 This is not really important since it is very unlikely that you'll want to serialize objects of this class (store them to disk or transmit them), and so you can either safely ignore this warning or use an annotation, @SuppressWarnings(“serial”) , to tell the compiler to ignore it. 这并不是很重要,因为您不太可能希望序列化此类的对象(将它们存储到磁盘或传输它们),因此您可以安全地忽略此警告或使用批注@SuppressWarnings(“serial”) ,告诉编译器忽略它。 ie,

Edit: or you can give it a default serialVersionUID as bhuang3 states. 编辑:或者您可以给它一个默认的serialVersionUID作为bhuang3状态。 1+ to his answer! 他的答案加1+!

Edit 2: 编辑2:

@SuppressWarnings(“serial”)
public class Display extends JFrame {

   // .....

}

Edit 3: 编辑3:
Next we'll discuss why it's usually not necessary and in fact often not a good idea to create classes that extend JFrame or other components unless you are overriding some of the methods of the super class... 接下来,我们将讨论为什么创建扩展JFrame或其他组件的类通常不是必需的,实际上通常不是一个好主意,除非您要重写超类的某些方法...

I think you need to declare a serialVersionUID in your Display2 class. 我认为您需要在Display2类中声明一个serialVersionUID。 for example: private static final long serialVersionUID = 1L; 例如: private static final long serialVersionUID = 1L;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM