[英]Set JPanel's size doesn't depend on JFrame 's size
My project have an JPanel and JFrame, (JPanel in JFrame). 我的项目有一个JPanel和JFrame(JFrame中的JPanel)。 I set JFrame ' s size is (400,400).
我将JFrame的大小设置为(400,400)。 And the size of JPanel is (150,150).
JPanel的大小为(150,150)。 I add JPanel to JFrame and run it, when it displayed, the size of JPanel not is 150, it's size is same the size of JFrame.
我将JPanel添加到JFrame并运行它,当它显示时,JPanel的大小不是150,它的大小与JFrame的大小相同。 I don't know how to fix it :(
我不知道如何解决它:(
How to set JPanel's size doesn't depend on JFrame 's size? 如何设置JPanel的大小不取决于JFrame的大小?
Here my code: 这是我的代码:
public class Draw_JPanel extends JFrame{
Load_image panel_im = new Load_image();
public Draw_JPanel()
{
this.setSize(400,400);
this.add(panel_im);
}
public static void main(String[] args){
Draw_JPanel abc = new Draw_JPanel();
abc.setVisible(true);
}
}
and my Load_image class: 和我的Load_image类:
public class Load_image extends JPanel{
public Load_image()
{
setSize(30,30);
this.setBackground(Color.RED);
}
}
You add your panel directly to frame which has BorderLayout as default , it resizes your panel. 您直接将面板添加到默认为BorderLayout的框架中,从而调整面板的大小。 Try to use this.getContentPane().add(panel_im);
尝试使用this.getContentPane()。add(panel_im); instead of this.add(panel_im);
而不是this.add(panel_im); , because contentPane has flowLayout as default.
,因为contentPane具有默认的flowLayout。 Also read more about LayoutManager .
另请阅读有关LayoutManager的更多信息。 tutorial .
教程 。
Also use setPreferedSize() instead of setSize() ( difference here ) 还要使用setPreferedSize()而不是setSize() ( 此处的区别 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.