首页 > 解决方案 > JPanel 在另一个 JPanel 的函数中的大小

问题描述

我想调整JPanel包含它的另一个面板的功能的大小。

当我运行我的代码时,面板很小并且位于内容面板的顶部。

所以这是我的代码:

package test;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class CodeForStackOverFlow {
    public static void main(String[] args) {
        Frame frame = new Frame();
    }
}

class Frame extends JFrame {

    private class Panel extends JPanel {

        private JPanel content = new JPanel();

        Panel() {
            content.setBackground(Color.red);
            this.add(content);
        }

        void adjustSize() {
            this.content.setSize(new Dimension(this.getHeight(), this.getHeight()));
        }
    }

    private Panel contentPane = new Panel();

    Frame() {
        this.setSize(new Dimension(500, 400));
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.setContentPane(contentPane);
        contentPane.adjustSize();

        this.setVisible(true);
    }
}

标签: javaswinguser-interfacejpanellayout-manager

解决方案


推荐阅读