我正在嘗試為一個專案制作游戲,我有一個為影像修改的 JDesktopPane 作為基本窗格,然后我為每個玩家準備了另外 2 個 DesktopPanes。
在播放器窗格(我在代碼中稱為欄位)內,我需要顯示一些資訊并有一些按鈕供播放器互動。
問題是按鈕沒有顯示在欄位上。
這是我的代碼:
basic_panel.setBackground(new Color(49, 161, 36));
player1.setBounds(width - 265, 15, 100, 20);
money1.setBounds(width - 265, 50, 100, 20);
loan1.setBounds(width - 265, 70, 100, 20);
bills1.setBounds(width - 265, 90, 100, 20);
rollDiceButton1.setBounds(width - 265, 120, 100, 20);
getLoanButton1.setBounds(width - 265, 145, 100, 20);
player1Field.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.blue));
player1Field.setBounds(width - 270,10,250,200);
player1Field.add(player1);
player1Field.add(money1);
player1Field.add(loan1);
player1Field.add(bills1);
basic_panel.add(player1Field, JLayeredPane.DEFAULT_LAYER);
this.add(basic_panel);
this.setVisible(true);
結果...

欲望...

我還想指出,如果我將組件直接添加到 basic_panel(這是基本的 desktopPane),它們會成功顯示,但我需要它們位于 player1_field
uj5u.com熱心網友回復:
所以,我要做的第一件事就是解耦你的代碼。“播放資訊”應該封裝到它自己的類/面板中。這樣,您可以更輕松地專注于它的個人需求和要求,并隔離它的所有職責。
其次,玩家資訊面板應該使用某種布局管理器。這使得定義組件之間的關系以及它們應該如何布局變得更加容易,并在處理美妙的 GUI 世界時提供更靈活的體驗。
JDesktopPane 旨在允許更“動態”的布局,旨在允許用戶定位和調整內部框架的大小,這意味著,您需要接管布局管理器的責任(您不必這樣做,您可以使用布局管理器,但它有點失敗)。
至此,preferred/minimumSize在嘗試在桌面窗格中定位和調整子組件大小時,您應該考慮到子組件產生的提示。
例如...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(new Color(49, 161, 36));
desktopPane.setPreferredSize(new Dimension(400, 200));
Dimension desktopPaneSize = desktopPane.getPreferredSize();
PlayerInfoPane infoPane = new PlayerInfoPane();
Dimension infoPaneSize = infoPane.getPreferredSize();
infoPane.setBounds(desktopPaneSize.width - infoPaneSize.width - 16, 16, infoPaneSize.width, desktopPaneSize.height - 32);
desktopPane.add(infoPane);
JFrame frame = new JFrame();
frame.add(desktopPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PlayerInfoPane extends JPanel {
public PlayerInfoPane() {
setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.blue));
JLabel player1 = new JLabel("Player1");
JLabel money1 = new JLabel("Money: 35000");
JLabel loan1 = new JLabel("Loan: 0");
JLabel bills1 = new JLabel("Bills: 0");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(2, 2, 22, 2);
add(player1, gbc);
gbc.insets = new Insets(2, 2, 2, 2);
add(money1, gbc);
add(loan1, gbc);
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
add(bills1, gbc);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/404803.html
標籤:
