JDesktopPane 内的组件未显示
Components inside JDesktopPane not showing
我正在尝试为一个项目制作游戏,我有一个 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(这是基本的桌面窗格),它们会成功显示,但我需要它们位于 player1_field
所以,我要做的第一件事就是解耦您的代码。 “播放信息”应该封装到它自己的 class/panel 中。这样,您可以更轻松地专注于它的个人需求和要求,并分离它的所有职责。
其次,玩家信息面板应该使用某种布局管理器。这使得定义组件之间的关系以及它们应该如何布局变得更加容易,并且在处理 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);
}
}
}
我正在尝试为一个项目制作游戏,我有一个 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(这是基本的桌面窗格),它们会成功显示,但我需要它们位于 player1_field
所以,我要做的第一件事就是解耦您的代码。 “播放信息”应该封装到它自己的 class/panel 中。这样,您可以更轻松地专注于它的个人需求和要求,并分离它的所有职责。
其次,玩家信息面板应该使用某种布局管理器。这使得定义组件之间的关系以及它们应该如何布局变得更加容易,并且在处理 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);
}
}
}