gpt4 book ai didi

java - JFrame JLabel 未完全显示

转载 作者:行者123 更新时间:2023-12-01 23:03:18 27 4
gpt4 key购买 nike

第一次使用 Java swing,我不明白我哪里出了问题。我正在创建一个 JFrame,然后创建两个 JPanel 添加到其中。每个 JPanel 都添加了一些 JLabelsJFrameJPanels 被指定为 FlowLayouts,这(据我理解)意味着事物只是按照它们添加的顺序显示,并且如果当前行没有空间,则添加到下一行。

这是我的问题:如果我运行当前代码并仅添加第一个面板 (infoPanel),我会得到如下所示的正确结果:this .

但是,如果我尝试添加两个面板,第一个面板会被覆盖,并且第一个面板的 JLabels 不会完全显示(每个面板上应该有比“Na...”更多的内容) ,这只是以“Name”开头的多行字符串的第一部分。我将最小尺寸指定为 100x100,但它们仍然只有几个字符。我尝试将它们设置为 400x400 和其他尺寸,但没有任何效果更改。它看起来像这样:

我的问题是:(1) 我是否对面板进行了错误的操作?带有 FlowLayoutJFrame 是否应该无法在不覆盖第一个添加的面板的情况下添加两个面板?(2) 为什么我的完整 JLabels 没有显示?我不明白我哪里出了问题,因为它们被指定为与我的 infoPanel JPanel 中的 JLabels 完全相同,而不是看起来一样...

感谢任何帮助,我的代码如下:

private void createView()
{
// Create the frame
frame = new JFrame("Shared Resources ");
frame.setSize(850, 750);
frame.setResizable(true);
frame.setLocation(0,0);;
frame.setVisible(true);

// Set the content
Container content = frame.getContentPane();
content.setBackground(Color.WHITE);

// Create our panels
JPanel infoPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel playerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

// Empty border template
Border emptyBorder = BorderFactory.createEmptyBorder();



// Shared resources information
// Palace Cards
palaceCards = new JLabel("Player Palace Cards");
/* TODO */
palaceCards.setHorizontalTextPosition(SwingConstants.CENTER);
palaceCards.setVerticalTextPosition(SwingConstants.BOTTOM);
palaceCards.setVerticalAlignment(SwingConstants.BOTTOM);
palaceCards.setBorder(emptyBorder);
palaceCards.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
palaceCards.setPreferredSize(new Dimension(350,25));
palaceCards.setMinimumSize(palaceCards.getPreferredSize());
infoPanel.add(palaceCards);

festivalCards = new JLabel("Festival Cards");

festivalCards.setBorder(emptyBorder);

festivalCards.setPreferredSize(new Dimension(350,25));
festivalCards.setMinimumSize(festivalCards.getPreferredSize());
festivalCards.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(festivalCards);

numThreeBlocks = newJLabel("Three Blocks: ");
numThreeBlocks.setText("");
numThreeBlocks.setPreferredSize(new Dimension(350,25));
numThreeBlocks.setHorizontalTextPosition(SwingConstants.CENTER);
numThreeBlocks.setVerticalTextPosition(SwingConstants.BOTTOM);
numThreeBlocks.setVerticalAlignment(SwingConstants.BOTTOM);
numThreeBlocks.setMinimumSize(numThreeBlocks.getPreferredSize());
numThreeBlocks.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numThreeBlocks);

numIrrigationTiles = newJLabel("Irrigation Tiles");
numIrrigationTiles.setPreferredSize(new Dimension(350,25));
numIrrigationTiles.setMinimumSize(numIrrigationTiles.getPreferredSize());
numIrrigationTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numIrrigationTiles);

numTwoPalaceTiles = newJLabel("Two Palace Tiles: ");
numTwoPalaceTiles.setPreferredSize(new Dimension(350,25));
numTwoPalaceTiles.setMinimumSize(numTwoPalaceTiles.getPreferredSize());
numTwoPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numTwoPalaceTiles);

numFourPalaceTiles = newJLabel("Four Palace Tiles: ");
numFourPalaceTiles.setPreferredSize(new Dimension(350,25));
numFourPalaceTiles.setMinimumSize(numFourPalaceTiles.getPreferredSize());
numFourPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numFourPalaceTiles);

numSixPalaceTiles = newJLabel("Six Palace Tiles: ");
numSixPalaceTiles.setPreferredSize(new Dimension(350,25));
numSixPalaceTiles.setMinimumSize(numSixPalaceTiles.getPreferredSize());
numSixPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numSixPalaceTiles);

numEightPalaceTiles = newJLabel("Eight Palace Tiles: ");
numEightPalaceTiles.setPreferredSize(new Dimension(350,25));
numEightPalaceTiles.setMinimumSize(numEightPalaceTiles.getPreferredSize());
numEightPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numEightPalaceTiles);

numTenPalaceTiles = newJLabel("Ten Palace Tiles: ");
numTenPalaceTiles.setPreferredSize(new Dimension(350,25));
numTenPalaceTiles.setMinimumSize(numTenPalaceTiles.getPreferredSize());
numTenPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
infoPanel.add(numTenPalaceTiles);

player1 = newJLabel("Player 1");
player1.setMinimumSize(new Dimension(400,400));
player1.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
playerPanel.add(player1);

player2 = newJLabel("PLayer 2");
player2.setMinimumSize(new Dimension(400,400));
player2.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
playerPanel.add(player2);

player3 = newJLabel("Player 3");
player3.setMinimumSize(new Dimension(400,400));
player3.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
playerPanel.add(player3);

player4 = newJLabel("Player 4");
player4.setMinimumSize(new Dimension(100,100));
player4.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
playerPanel.add(player4);

// Add panels
content.add(infoPanel);
content.add(playerPanel);
}

最佳答案

Should the JFrame with a FlowLayout not be able to add both panels without overwriting the first one that was added?

是的,但问题是 JFrame 使用 BorderLayout (不是 FlowLayout)作为默认布局管理器。如果您未指定约束,组件将添加到 CENTER,但您只能在 CENTER 中显示一个组件,因此仅显示最后一个组件。因此,如果您想将两个面板添加到框架中,那么您应该使用类似以下内容的内容:

content.add(infoPanel, BorderLayout.CENTER);
content.add(playerPanel, BorderLayout.SOUTH);

FlowLayouts, which (as I understand it) means that things are just displayed in the order they are added and added to the next line if there isn't room for them on the current line.

基本上是正确的,但对于 FlowLayout,首选大小是按单行组件计算的。仅当面板有额外可用空间时,“包裹”组件才会可见。为了演示这一更改,请使用上面的代码:

//content.add(infoPanel, BorderLayout.CENTER);
content.add(infoPanel, BorderLayout.NORTH);

并且您将看不到包装的组件。请参阅Wrap Layout了解更多信息和此问题的解决方案。

关于java - JFrame JLabel 未完全显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23188014/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com