我正试图为一个项目制作一个游戏,我已经为图像修改了一个JDesktopPane作为基本窗格,然后我为每个玩家提供了另外两个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中。
发布于 2022-01-01 23:15:12
所以,我要做的第一件事就是解耦您的代码。"play info“应该封装在它自己的类/面板中。这样,您就可以更容易地专注于它的个人需求和需求,并将它的所有责任分离开来。
其次,播放器信息面板应该使用某种类型的布局管理器。这使得定义组件之间的关系变得更加容易,因为它们应该如何布局,并允许在处理奇妙的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://stackoverflow.com/questions/70552413
复制相似问题