GUI编程
(1)

BDP%1ZL(T%NP{$Y8`$Z5%CV.png

实现这种效果的主要代码为

package com.wang.first;
import java.awt.*;

/**
 * @author valive
 * @create 2021-04-06 11:04
 **/
public class testFrame2 {
    public static void main(String[] args) {
        myFrame myFrame1 =new myFrame(100,100,200,200,Color.blue);
        myFrame myFrame2 =new myFrame(300,100,200,200,Color.red);
        myFrame myFrame3 =new myFrame(100,300,200,200,Color.yellow);
        myFrame myFrame4 =new myFrame(300,300,200,200,Color.green);
    }

}
class myFrame extends Frame {
    static  int id=0;
    public myFrame(int x,int y,int w,int h,Color color){
        super("myframe"+(++id));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);
    }

}

CN53ZCH@J_L7EG)_[JXJB17.png

    package com.wang.first;
import  java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author valive
 * @create 2021-04-06 11:20
 **/
public class testpanel {
    public static void main(String[] args) {
   Frame frame= new Frame();
   //布局的概念
   Panel panel=new Panel();

   //设置布局
    frame.setLayout(null);

    //坐标
    frame.setBounds(300,300,500,500);
    frame.setBackground(new Color(40,161,35));

    //panel设置坐标
    panel.setBounds(50,50,400,400);
    panel.setBackground(Color.red);

    //frame.add(panel)
    frame.add(panel);
    frame.setVisible(true);

    //监听事件,监听关闭事件 system.exit(0)
    //适配器模式
    frame.addWindowListener(new WindowAdapter() {
        //窗口点击关闭时需要做的事情
        @Override
        public void windowClosing(WindowEvent e){
            //结束程序
            System.exit(0);
        }
    });
}

}


}2WD`KH%SVZER~%_M9X_4_T.png
主要实现的代码为:

package com.wang.first;

import java.awt.*;
/**
 * @author valive
 * @create 2021-04-06 12:05
 **/
public class testFlowLayout {
public static void main(String[] args) {
    //总体Frame
    Frame frame =new Frame();
    frame.setSize(400,300);
    frame.setLocation(300,400);
    frame.setBackground(Color.black);
    frame.setVisible(true);
    frame.setLayout(new GridLayout(2,1));

    //四个面板
    Panel p1 =new Panel(new BorderLayout());
    Panel p2 =new Panel(new GridLayout(2,1));
    Panel p3 =new Panel(new BorderLayout());
    Panel p4 =new Panel(new GridLayout(2,1));

    //上面部分
    p1.add(new Button("East-1"),BorderLayout.EAST);
    p1.add(new Button("Wast-1"),BorderLayout.WEST);
    p2.add(new Button("p2-btn-1"));
    p2.add(new Button("p2-btn-2"));
    p1.add(p2,BorderLayout.CENTER);

    //下面部分
    p3.add(new Button("East-2"),BorderLayout.EAST);
    p3.add(new Button("Wast-2"),BorderLayout.WEST);
    for (int i = 0; i < 4; i++) {
        p4.add(new Button("for-"+i));
    }
    p3.add(p4,BorderLayout.CENTER);
    frame.add(p1);
    frame.add(p3);
}

}

本文为作者valive发布,未经允许禁止转载!
上一篇 下一篇
评论
评论已关闭 >_<

评论已关闭