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

Abstract 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;
}
}
我在 Game Board 建構式中初始化 Page 物件,然后嘗試在 GameBoard 類的 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;
}
}
當視窗出現時,頁面元素不顯示,如下面的截圖所示。主類代碼:
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? */
}

uj5u.com熱心網友回復:
所以我通過洗掉容器解決了這個問題:
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;
}
}
現在看起來像這樣:

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/515875.html
標籤:爪哇摇摆用户界面框架面板
