java - 将 JPanel 添加到主面板

标签 java swing frames panels

所以我创建了三个类,主方法类、Frame类和JPanel类。在 JPanel 类中,我想再添加三个 JPanel,一个位于 JFrame 的顶部,一个位于中间,一个位于底部。我的 JPanel 和 JFrame 类的代码如下: J面板:

public class ConcertPanel extends JPanel
{
public ConcertPanel() 
{
    JPanel ConcertPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

    JPanel Panel1 = new JPanel();  
    Panel1.setSize(800,200);
    Panel1.setBackground(Color.BLUE);

    JPanel Panel2 = new JPanel(); 
    Panel2.setSize(800,400);
    Panel1.setBackground(Color.GREEN);

    JPanel Panel3 = new JPanel(); 
    Panel3.setSize(800,200);
    Panel1.setBackground(Color.GRAY);

    this.add(Panel1);
    this.add(Panel2);
    this.add(Panel3);
}        
}

public class ConcertFrame extends JFrame 
{
private ConcertPanel controlPane;
// The constructor
public ConcertFrame()
{
    controlPane = new ConcertPanel() ;
    this.add(controlPane);
....

当这个项目运行时,没有显示任何错误,但是当 JFrame 弹出时,它没有给我提供其中的三个不同颜色的面板,而只在顶部提供一个小灰色框。谁能告诉我原因或帮忙吗?

最佳答案

一个主要问题是代码没有考虑 preferredSize 或布局管理器。

所有彩色 JPanel 的首选尺寸是 0,0,设置尺寸不会影响这一点。由于它们被添加到默认布局管理器是 FlowLayout 的 JPanel 中,因此布局不会增加它们的大小。因此,由于大多数布局管理器都遵循首选大小而不是实际大小,并且 FlowLayout 不会更改这一点,因此 JPanel 会被添加,但永远不会被看到。

相反,请考虑对主容器使用其他布局,例如,如果所有组件都具有相同的大小,则使用 GridLayout;如果所有组件都放置在行或列中,则考虑使用 BoxLayout。考虑重写 getPreferredSize 方法,但要小心,并且仅在需要时才进行。

一个“作弊”的解决方案是将 setSize(...) 更改为 setPreferredSize(new Dimensnion(...)),但这是不受欢迎的。

例如:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;

@SuppressWarnings("serial")
public class ColorPanels extends JPanel {
    public ColorPanels() {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        add(new ColorPanel(Color.BLUE, 800, 200));
        add(new ColorPanel(Color.GREEN, 800, 400));
        add(new ColorPanel(Color.GRAY, 800, 200));
    }

    private static class ColorPanel extends JPanel {

        private int w;
        private int h;

        public ColorPanel(Color color, int w, int h) {
            this.w = w;
            this.h = h;
            setBackground(color);
        }

        @Override
        public Dimension getPreferredSize() {
            if (isPreferredSizeSet()) {
                return super.getPreferredSize();
            }
            return new Dimension(w, h);
        }

    }

    private static void createAndShowGui() {
        ColorPanels mainPanel = new ColorPanels();

        JFrame frame = new JFrame("ColorPanels");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

关于java - 将 JPanel 添加到主面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40835644/

相关文章:

Java 将 ArrayList 传递给另一个 JFrame

java - IExecutorService 提交可运行订单

java - libGDX:如何检测滚动 Pane 是否在最大 Y 处滚动?

JavaScript 重定向不起作用

javascript - 在浏览网站时保持与服务器的永久连接

Java n 节点树

使用 Swing 的 Java 聊天应用程序(概念)

java - 我可以向 JFrame 添加 CSS 文件吗?

java - 无法更改背景 JFrame

带有框架的javascript没有值(value)