如果你用frame.add(panel)有可能会出现panel把整个frame铺满的问题,无论你设置panel的大小和位置如图:
import java.awt.*;
public class TextGUI {
public Frame setWindowse(String Titie1,int x,int y,int height,int width,Color color)
{
Frame frame1 = new Frame(Titie1);
//设置可见性
frame1.setVisible(true);
//设置窗口大小
frame1.setSize(width,height);
//设置颜色
frame1.setBackground(color);
//弹出的初始位置
frame1.setLocation(x,y);
//设置大小固定
frame1.setResizable(false);
return frame1;
}
}
import java.awt.*;
public class gui extends TextGUI{
public static void main(String[] args) {
gui g1 = new gui();
Panel pane1 =new Panel();
//设置布局
pane1.setLayout(null);
//设置窗口参数
Frame frame2 = g1.setWindowse("第二个",300,300,500,500,Color.white);
//设置面板参数
pane1.setBackground(new Color(10,1,1));
pane1.setBounds(50,50,400,400);
//把面板加到窗口里
frame2.add(pane1);
}
}
正常情况应该是这样:
那么为什么会出现这样的错误呢,原因是官方提供的方法就有问题,同样的代码会运行出不同的结果,但是有个玄学的办法,就是多声明几个frame然后设置成不显示,就可以了
import java.awt.*;
public class gui extends TextGUI{
public static void main(String[] args) {
gui g1 = new gui();
Panel pane1 =new Panel();
//设置布局
pane1.setLayout(null);
//设置窗口参数
Frame frame2 = g1.setWindowse("第二个",300,300,500,500,Color.white);
Frame frame3 = g1.setWindowse("第3个",300,300,500,500,Color.blue);
Frame frame4 = g1.setWindowse("第3个",300,300,500,500,Color.red);
frame3.setVisible(false);
frame4.setVisible(false);
//设置面板参数
pane1.setBackground(new Color(10,1,1));
pane1.setBounds(50,50,300,300);
//把面板加到窗口里
frame2.add(pane1);
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容