java - JFrame JLabel 未完全显示

标签 java swing jpanel jlabel layout-manager

第一次使用 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/

相关文章:

java - 为什么Java中的Z3优化会出现段错误?

java - 分割paintComponent方法

java - CardLayout性能?

java - 套接字输入和输出的独立线程

java - 为什么数到 2^24 执行得很快,但数到 2^25 却要花更长的时间?

java - R.array是一个类但不是一个对象?

java - 如何在 JPanel 中放置文本区域

java - 在java中,如何获取 slider 变化的值?

Java Swing CardLayout 多个文件

JPanel 中的 Java 代码绘制