Nima uchun faqat yuqori darajadagi komponent mavjud?

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

Ushbu dasturni ishga tushirganimda, u faqat so'nggi qo'shilgan UIElementni ko'rsatadi. Nima sababdan?

1

1 javoblar

Yuqori darajadagi konteynerlar (JApplet, JFrame kabi) BorderLayout foydalanadi. Shuning uchun, ilovaning tarkib qismiga komponent qo'shsangiz, buzilishiga sukut bo'yicha "CENTER" ga qo'shiladi. Shunday qilib, barcha tarkibiy qismlar bir xil maydonni egallaganligi sababli, faqat oxirgi komponentni qo'shiladi.

Men sizga Swing asarlarini qanday ishlatishni o'rganib chiqishni taklif qilaman va o'zingizni maxsus yaratishni yaratishga urinmang.

Siz tarkibiy qismlarni ko'rsatish vaqtida faqatgina derazani aytib bo'lmaydi. Komponentni chizish turli vaqtlar sizning bilishingizsiz avtomatik ravishda amalga oshiriladi, shuning uchun, Swing yaxshi aniqlangan bo'yoq mexanizmiga ega.

4
qo'shib qo'ydi
Joylashuv menejeridan foydalaning. Men sizning savolingizda boshqa hech narsani ko'rmaydigan narsani ko'rmayapman. Muammoingiz tartib mudiri emas. Muammo shundaki, siz ikkita komponentni bir-birining ustiga joylashtirishga harakat qilmoqdasiz, bu uning tarkibiy qismlari 2 o'lchovli tartibga solingan tartibga menejerlari bilan bog'liq emas, balki 3D formatini emas. Ehtimol, siz JLayeredPane-dan foydalanishingiz yoki ehtimol, Background Panel .
qo'shib qo'ydi muallif camickr, manba
@Benjamin Komponentlarni o'zingiz xohlagan tarzda joylashtirish imkonini beruvchi LayoutManager ni bilib olaman. NullLayout , albatta, menimcha, bir variant emas. GridBagLayout , GridLayout va BoxLayout ga qarashingiz mumkin. Boshqa ba'zi narsalar sizga kerakli bo'lishi mumkin.
qo'shib qo'ydi muallif Laf, manba
Yaxshiyamki, tizim repinatsiyalari va dasturlarni qayta tiklashlari haqida bilaman va men tizimning yangiligi haqida hech narsa qilolmayman. Faqat manzarani biroz nazorat qilishni xohlayman. Xo'sh, siz nima deb o'ylaysiz? Jepletning tartibini bo'shatishim kerakmi?
qo'shib qo'ydi muallif Benjamin, manba