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();
}
}
答案 0 :(得分:2)
使用:
add(theGui);
或
setContentPane(theGui);
之前:
setVisible(true);