为什么只渲染顶层组件?

发布于 2024-12-12 18:17:18 字数 2590 浏览 2 评论 0原文

我正在创建一个在我的网站上使用的游戏小程序,但我讨厌 Swing 布局/渲染组件的方式。所以我想我可以设置一种安排,让我可以按照自己喜欢的方式做事(手动告诉窗口管理器何时渲染组件以及使用布局的绝对定位)。 这是我的代码,GameApp.java:

import javax.swing.JApplet;

public class GameApp extends JApplet {

    private Background background;
    private Foreground foreground;

    @Override
    public void init() {
        background = new Background();
        foreground = new Foreground();
    }

    @Override
    public void start() {
        background.render(this);
        foreground.render(this);
    }
}

Foreground.java:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;

public class Foreground extends UIElement {

    private String buttonText;
    private JButton startButton;

    @Override
    public void fetchDependencies() throws Exception {
        buttonText = "Play!";
        startButton = new JButton(buttonText);
    }

    @Override
    public void create() {
        setLayout(null);
        startButton.setBounds(350, 260, 100, 30);
        startButton.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        add(startButton);
    }
}

Background.java:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;

public class Background extends UIElement {

    private Image backdrop;

    @Override
    public void fetchDependencies() throws Exception {
        backdrop = Toolkit.getDefaultToolkit().getImage(new URL("mywebpage"));
    }

    @Override
    public void create() {
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(backdrop, 0, 0, parent);
    }
}

UIElement.java:

import javax.swing.JApplet;
import javax.swing.JComponent;

public abstract class UIElement extends JComponent {

    protected JApplet parent;

    public UIElement() {
        try {
            fetchDependencies();
        } catch(Exception e) {
            System.err.println(e);
        }
    }

    public abstract void fetchDependencies() throws Exception;
    public abstract void create();

    public void render(JApplet p) {
        addParent(p);
        create();
        p.getContentPane().add(this);
    }

    private void addParent(JApplet ja) {
        parent = ja;
    }
}

当我运行这个小程序时,它只会显示最近添加的UIElement。这是为什么?

I'm creating a game applet for use on my website and I hate Swing's way of laying out/rendering components. So I thought I could set up an arrangement that allowed me to do things, how I like to do them (manually tell the window manager when to render a component as well as using absolute positioning for the layout).
Here's my code, GameApp.java:

import javax.swing.JApplet;

public class GameApp extends JApplet {

    private Background background;
    private Foreground foreground;

    @Override
    public void init() {
        background = new Background();
        foreground = new Foreground();
    }

    @Override
    public void start() {
        background.render(this);
        foreground.render(this);
    }
}

Foreground.java:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;

public class Foreground extends UIElement {

    private String buttonText;
    private JButton startButton;

    @Override
    public void fetchDependencies() throws Exception {
        buttonText = "Play!";
        startButton = new JButton(buttonText);
    }

    @Override
    public void create() {
        setLayout(null);
        startButton.setBounds(350, 260, 100, 30);
        startButton.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        add(startButton);
    }
}

Background.java:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;

public class Background extends UIElement {

    private Image backdrop;

    @Override
    public void fetchDependencies() throws Exception {
        backdrop = Toolkit.getDefaultToolkit().getImage(new URL("mywebpage"));
    }

    @Override
    public void create() {
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(backdrop, 0, 0, parent);
    }
}

UIElement.java:

import javax.swing.JApplet;
import javax.swing.JComponent;

public abstract class UIElement extends JComponent {

    protected JApplet parent;

    public UIElement() {
        try {
            fetchDependencies();
        } catch(Exception e) {
            System.err.println(e);
        }
    }

    public abstract void fetchDependencies() throws Exception;
    public abstract void create();

    public void render(JApplet p) {
        addParent(p);
        create();
        p.getContentPane().add(this);
    }

    private void addParent(JApplet ja) {
        parent = ja;
    }
}

When I run this applet it will only display the most recently added UIElement. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2024-12-19 18:17:18

顶级容器(如 JApplet、JFrame)使用 BorderLayout。因此,当您将组件添加到小程序的内容窗格时,默认情况下该组件会添加到“CENTER”。因此,由于所有组件占据相同的区域,因此仅显示最后添加的组件。

我建议您学习如何使用 Swing 作品,并且不要尝试创建您自己的自定义渲染。

您不能只告诉窗口何时渲染组件。有时,组件的绘制是在您不知情的情况下自动完成的,这就是 Swing 具有明确定义的绘制机制的原因。

Top level containers (like JApplet, JFrame) use a BorderLayout. So when you add a component to the content pane of the applet the component gets added to the "CENTER" by default. So only the last component added is displayed since all components occupy the same area.

I suggest you learn how to use Swing works and don't attempt to create your own custom rendering.

You can't just tell the window when to render components. There are different times the painting of a component is done automatically without your knowing about it that is why Swing has a well defined paint mechanism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文