JPanel不会出现在Frame上

时间:2016-05-24 13:22:40

标签: java swing jpanel

JPanel不会在JFrame上展示,我不明白为什么。看起来JPanel并没有以某种方式添加到JFrame中。有什么建议吗?

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

public class LeaseItGUI extends JPanel{
    private int width=600,height=600;

    public void paintComponenet(Graphics g){
        super.paintComponent(g);
        g.fillRect(0, 0, width, height);
    }
}

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

public class LeaseItMain extends JFrame{
    private int width=600,height=600;

    public LeaseItMain(){
        setSize(width,height);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        LeaseItGUI theGui = new LeaseItGUI();
        setVisible(true);
        add(theGui);
    }


    public static void main(String[] args){
        LeaseItMain LIM = new LeaseItMain();
    }
}

1 个答案:

答案 0 :(得分:2)

使用:

add(theGui);

setContentPane(theGui);

之前:

setVisible(true);