图形用户界面(界面组件 Frame ,Panel,Button的简单使用)

Frame

import java.awt.*;
public class FrameTest {
    public static void main(String[] args) {
        Frame f = new Frame("My first frame");
        f.setSize(200,200);
        f.setBackground(Color.LIGHT_GRAY);
        f.setVisible(true);
    }
}

Panel 中加入 Frame

import java.awt.*;

public class TestFrameWithPanel {
    public static void main(String[] args) {
        //创建Frame体 并做基本设置
        Frame f = new Frame("my frame");//Frame标题
        f.setSize(400,400);//设置大小
        f.setLocation(450,450);
        f.setBackground(Color.LIGHT_GRAY);//设置背景颜色
        f.setVisible(true);//设置为可见

        //创建面板
        Panel panel = new Panel();
        panel.setSize(250,250);
        panel.setLocation(100,100);
        panel.setBackground(Color.PINK);

        //创建按钮
        Button button = new Button("Hi");
        button.setSize(60,60);
        button.setLocation(20,20);
        button.setBackground(Color.ORANGE);

        f.setLayout(null);//取消默认布局管理器
        panel.setLayout(null);//frame和panel的布局方式可以不相同 panel不是顶层容器 它必须位于窗口或其它容器内
        panel.add(button);//Panel上加 Button
        f.add(panel);//Frame上加Panel
    }
}

你可能感兴趣的:(在线OJ算法题)