Java:JFrame和子类

时间:2017-09-21 22:27:39

标签: java jframe polymorphism jlayeredpane

好吧,基本上我是在尝试使用JFrame在java中制作基于文本的游戏,最后是其他一些简单的组件。我的主要问题是我创建了一些子类,Text就是其中之一。我需要Text作为子类,因为它需要直接访问JLayeredPane(层)。我的问题是,每次我创建一个新的Text或任何其他子类,并尝试运行该程序时,会反复弹出一堆窗口。现在我已经意识到这可能是因为使用Text调用Launcher并开始这个粘性循环。现在我知道我没有java向导这就是为什么我很好奇是否有人有任何类型的解决方案?

我的父班:

package Game;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;

public class Launcher
{   
    public JFrame frame = new JFrame ("Game");
    public JLayeredPane layer = frame.getLayeredPane ();

    public Launcher ()
    {
        frame.setTitle ("Game");
        frame.setVisible (true);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setSize (906, 629);
        frame.setResizable (false);//To be changed at a later point when I feel like adding the functionality
        frame.setLocationRelativeTo (null);

        Text test = new Text ("This here will cause an error", 0, 0);//It doesnt matter where you put this because even if it were to go into a subclass (which is where it would go), even a call to that would perpetuate this evil loop
    }

    public static void main (String [] args)
    {
        Launcher run = new Launcher ();
    }
}

我的子课程:

package Game;

import javax.swing.JLabel;
import java.awt.Font;

public class Text extends Panel
{
    private JLabel text;
    private int x, y;
    private int across;
    private int tall;

    private String font;
    private int style;
    private int size;

    private boolean updated;

    public Text (String text, int x, int y, int across, int tall, String font, int style, int size)//More or less for buttons
    {   
        this.text = new JLabel (text);
        this.x = x;
        this.y = y;
        this.across = across;
        this.tall = tall;

        this.font = font;
        this.style = style;
        this.size = size;

        updated = true;
    }

    public Text (String text, int x, int y, String font, int style, int size)
    {
        this (text, x, y, 900, 600, font, style, size);
    }

    public Text (String text, int x, int y)
    {
        this (text, x, y, 900, 600, "Bakerville Old Font", Font.PLAIN, 16);
    }

    public void add ()
    {
        text.setFont (new Font (font, style, size));
        text.setBounds (x, y, across, tall);
        layer.add (text, new Integer (3));

        layer.repaint();

        updated = true;
    }

    public void remove ()
    {
        layer.remove (text);
    }
}

0 个答案:

没有答案