JAVA 创建JFRAME窗口

今天用java写了一个JFrame的窗口

一共包含两个类

第一个是AppFrame.java 用来定义窗口的各种属性

package wzc;
import java.awt.*;
//import javax.swing.*;
public class AppFrame extends Frame {    
    private static final long serialVersionUID = 1L;//版本号
    private String[] textLines = new String[6];
    private Image img;
    private Font fontTitle;
    private Font fontText;
    private int height = 600;
    private int width = 600;
    private String appName = "Mobile Controller";
    private Toolkit tk = Toolkit.getDefaultToolkit();
    public AppFrame() {
        super();
        this.setSize(this.width, this.height);
        this.textLines[0] = "Mobile Controller Server is now running.";
        this.textLines[1] = "";
        this.textLines[2] = "Your IP address is: ";
        this.textLines[3] = "";
        this.textLines[4] = "Enter this IP address on the start screen of the";
        this.textLines[5] = "Mobile Controller application on your phone to begin.";
        this.img=this.tk.getImage("res/1.jpg");//得到图像地址
    }
    public void init() {
        this.fontTitle = new Font("Verdana", Font.BOLD, 16);//字体
        this.fontText = new Font("Verdana", Font.PLAIN, 11);
        this.setBackground(Color.BLACK);//背景色
        this.setForeground(Color.WHITE);//前景色
    }
    public void paint(Graphics g) {
        g.setColor(this.getBackground());
        g.fillRect(0, 0, this.width, this.height);//用指定的画刷填充矩形,此函数包括矩形的左上边界,但不包括矩形的右下边界。
        g.setColor(this.getForeground());
        g.setFont(this.fontTitle);//字体
        g.drawString(this.appName, 70, 55);//写字
        g.setFont(this.fontText);
        int startY = 90;//y轴距离
        int l = 6;
        for (int i = 0;i<l;++i) {
            g.drawString(this.textLines[i], 10, startY);
            startY += 20;
        }
        g.drawImage(this.img, 20, startY+10, this);//画图
    }
}

第二个类是myFrame.java定义AppFrame的实体,初始化

package wzc;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class myFrame {
private static AppFrame f;
    
    public static void main(String[] args) {
        f = new AppFrame();//新建窗口
        f.setVisible(true);//窗口可见
        f.setResizable(false);//不可以改变大小
        f.setTitle("Mobile Controller");//设置标题
        f.setLocationRelativeTo(null); //设置窗居中
        f.addWindowListener(new WindowAdapter() {//添加一个窗口监听
            public void windowClosing(WindowEvent e) {//这是窗口关闭事件
                f.setVisible(false);//窗口不可见
                f.dispose();//释放由此 Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源。
                System.exit(0);//退出
            }
        });
        
        f.init();//初始化
        //
        //System.out.println(System.getProperty("os.name"));//参数的获取 在程序启动时将不变的整个程序用到的参数

    }
}

效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值