GUI系列二

一.概念认知:


二,代码示例

package com.awt;

import java.awt.*;

public class TestMutiFrame {
    public static void main(String[] args) {
        MyFrame f1=new MyFrame(100, 100, 200, 200, Color.BLUE);
        MyFrame f2=new MyFrame(300, 100, 200, 200, Color.GRAY);
        MyFrame f3=new MyFrame(100, 300, 200, 200, Color.PINK);
        MyFrame f4=new MyFrame(300, 300, 200, 200, Color.BLACK);
    }
}

class MyFrame extends Frame{
    static int id=0;
    MyFrame(int x,int y,int w,int h,Color color){
        /*
         * Frame(String title)构造一个新的,最初不可见的 Frame对象,其中包含指定的标题。
         * 所以这里用到了父类frame的构造器来设置标题
         */
         super("MyFrame"+(++id));
         setBackground(color);
         //布局设置为null
         setLayout(null);
         //设置坐标和界面宽高
         setBounds(x, y, w, h);
         setVisible(true);
    }
}

import java.awt.*;

public class TestPanel {
    public static void main(String[] args) {
        Frame f=new Frame("Java Frame with Panel");
        /*
         * Panel(LayoutManager layout)
         * 使用指定的布局管理器创建一个新面板。 
         */
        Panel p=new Panel(null);
        f.setLayout(null);
        f.setBounds(300, 300, 500, 500);
        f.setBackground(new Color(0, 0, 102));
        //边界坐标相对于frame窗口
        p.setBounds(50, 50,400, 400);
        p.setBackground(new Color(204, 204, 255));
        f.add(p);
        f.setVisible(true);
    }
}

package com.awt;

import java.awt.*;

public class TestMuitPanel {
    public static void main(String[] args) {
        new MyFrames("坚持就是胜利", 300, 300, 400, 300);
    }
}

class MyFrames extends Frame{
    private Panel p1,p2,p3,p4;
    public MyFrames(String s,int x,int y,int w,int h) {
        super(s);
        setLayout(null);
        p1=new Panel(null); p2=new Panel(null);
        p3=new Panel(null); p4=new Panel(null);
        //设置坐标和宽高
        p1.setBounds(0, 0, w/2, h/2);
        p2.setBounds(0, h/2, w/2, h/2);
        p3.setBounds(w/2, 0, w/2, h/2);
        p4.setBounds(w/2, h/2, w/2, h/2);
        //设置背景色
        p1.setBackground(Color.gray);
        p2.setBackground(Color.green);
        p3.setBackground(Color.black);
        p4.setBackground(Color.blue);
        //容器panel添加进Frame窗口中
        add(p1);add(p2);add(p3);add(p4);
        //设置窗口宽高
        setBounds(x, y, w, h);
        setVisible(true);
    }
}


3.课堂练习作业

预期效果:中间部分宽高是Frame的一半且要居中

import java.awt.*;
import java.util.Scanner;
public class TestPractice{
    public static void main(String [] args){
        Scanner in=new Scanner(System.in);
        int x=in.nextInt();
        int y=in.nextInt();
        int w=in.nextInt();
        int h=in.nextInt();
        new MyFrame("果子还是很可爱滴",x,y,w,h);
    }
}

class MyFrame extends Frame{
    //四个私有panel容器对象
    private Panel p1;
    MyFrame(String s,int x,int y,int w,int h){
     //访问父类构造器设置窗口标题
     super(s);
     //Frame布局为空
     setLayout(null);
    //容器布局为空
    p1=new Panel(null);
    //设置容器的背景
    p1.setBackground(Color.yellow);
    //设置容器的边界
    p1.setBounds(x/4,y/4,w/2, h/2);
    //放入Frame窗口中
    add(p1);
    //设置Frame布局为null
    setLayout(null);
    //设置Frame边界
    setBounds(x,y,w,h);
    //设置Frame背景
    setBackground(Color.blue);
    //设置可见
    setVisible(true);    
    }
}

原文地址:https://www.cnblogs.com/zjm1999/p/10262627.html