簡體   English   中英

如何解決在 Java Swing 顯示 JPanel 元素的問題?

[英]How to fix the problem with showing JPanel element in Java Swing?

我在顯示位於 JFrame 中另一個 JPanel 元素中的 JPanel (Page) 元素時遇到問題。

在此處輸入圖像描述

摘要 Class Page 正在擴展 JPanel 並為其提供一些新參數。

public abstract class Page extends JPanel implements KeyListener {
    protected String pageName;
    protected Color backgroundColor;

    public Page(String name, Color backgroundColor) {
        this.pageName = name;
        this.backgroundColor = backgroundColor;

        addKeyListener(this);
        this.setFocusable(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setSize((int) PAGE_WIDTH, (int) PAGE_HEIGHT);
        this.setBackgroundColor(backgroundColor);
    }

    /* keyTyped method is not needed in usage, so it is empty */
    @Override
    public void keyTyped(KeyEvent e) {}

    /* keyTyped method is not needed in usage, so it is empty */
    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public abstract void keyPressed(KeyEvent e);

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String name) {
        this.pageName = name;
    }

    public Color getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }
}

我在游戲板構造函數中初始化頁面 object,然后嘗試在游戲板 class 的 printComponent() 方法中使用它。

public class GameBoard extends JPanel {
    private static GameBoard instance = null;
    private Page currentPage = new MainMenuPage();
    private JPanel dialog = Dialog.getInstance();

    private GameBoard() {}

    public static GameBoard getInstance() {
        if (instance == null) {
            instance = new GameBoard();
        }
        return instance;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setBackground(Color.WHITE);

        JPanel container = new JPanel();
        container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
        container.setSize(WINDOW_WIDTH, (int) PAGE_HEIGHT);
        container.setBackground(Color.decode("#333333"));

        container.add(currentPage);
        container.add(dialog);
        this.add(container);
    }

    public Page getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Page currentPage) {
        this.currentPage = currentPage;
    }

    public JPanel getDialog() {
        return dialog;
    }

    public void setDialog(JPanel dialog) {
        this.dialog = dialog;
    }
}

當 window 出現時,頁面元素未顯示,如下面的屏幕截圖所示。 總機class代碼:

public class GameApplication {
    private static JFrame window;
    private static GameBoard gameBoard;

    public static void main( String[] args ) {
        setupWindow();
        createGameBoard();
    }

    private static void setupWindow() {
        System.out.println("[GameApplication]: Creating window");
        window = new JFrame("Dungeon Master");
        window.setVisible(true);
        window.setResizable(false);
        window.setBounds(200, 80, WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private static void createGameBoard() {
        System.out.println("[GameApplication]: Creating GameBoard");
        gameBoard = GameBoard.getInstance();
        window.add(gameBoard);
        gameBoard.requestFocusInWindow();
    }

    /* Game Init Method? */
}

在此處輸入圖像描述

所以我通過刪除容器解決了這個問題:

public class GameBoard extends JPanel {
    private static GameBoard instance = null;
    private Page currentPage = new MainMenuPage();
    private final JPanel dialog = Dialog.getInstance();

    private final JPanel keyInfo = new JPanel();
    private final JLabel keyInfoLabel = new JLabel("Q - quit the game | S - go to start menu | C - clear dialog");

    private GameBoard() {}

    public static GameBoard getInstance() {
        if (instance == null) {
            instance = new GameBoard();
        }
        return instance;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        currentPage.setBounds(0, 0, PAGE_WIDTH, PAGE_HEIGHT);
        currentPage.setBackground(Color.decode("#121212"));

        dialog.setBounds(PAGE_WIDTH, 0, DIALOG_WIDTH, PAGE_HEIGHT);
        dialog.setBackground(Color.decode("#333333"));

        keyInfo.setBounds(0, PAGE_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT);
        keyInfo.setBackground(Color.decode("#f6f9f9"));
        keyInfo.setLayout(new BoxLayout(keyInfo, BoxLayout.X_AXIS));
        
        keyInfoLabel.setForeground(Color.decode("#121212"));
        keyInfo.add(keyInfoLabel);

        this.add(currentPage);
        this.add(dialog);
        this.add(keyInfo);
    }

    public Page getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Page currentPage) {
        this.currentPage = currentPage;
    }

    public JPanel getDialog() {
        return dialog;
    }
}

現在看起來像這樣: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM