Java图形界面:初次接触

来源:互联网 发布:2016淘宝店铺开店流程 编辑:程序博客网 时间:2024/05/16 10:07

GUI-Graphic User Interface 图形用户界面

SWING简单案例

JFrame是GUI中的容器
JButton是最常见的组件- 按钮
注意:f.setVisible(true); 会对所有的组件进行渲染,所以一定要放在最后面

import javax.swing.JButton;import javax.swing.JFrame;public class TestGUI {    public static void main(String[] args) {        // 主窗体        JFrame f = new JFrame("LOL");        // 主窗体设置大小        f.setSize(400, 300);        // 主窗体设置位置        f.setLocation(500, 300);        // 主窗体中的组件设置为绝对定位        f.setLayout(null);        // 按钮组件        JButton b = new JButton("泉水杀人挂");        // 同时设置组件的大小和位置        b.setBounds(50, 50, 280, 30);        // 把按钮加入到主窗体中        f.add(b);        // 关闭窗体的时候,退出程序        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        // 让窗体变得可见        f.setVisible(true);    }}

这里写图片描述

练习:在上次关闭位置启动窗口(多线程实现)

思路提示:
启动一个线程,每个100毫秒读取当前的位置信息,保存在文件中,比如location.txt文件。
启动的时候,从这个文件中读取位置信息,如果是空的,就使用默认位置,如果不是空的,就把位置信息设置在窗口上。
读取位置信息的办法: f.getX() 读取横坐标信息,f.getY()读取纵坐标信息。

SavingPositionThread

package com.gui;import java.io.DataOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.swing.JFrame;public class SavingPositionThread extends Thread{    private JFrame f;    File file = new File("E:/Javalearn/j2se/location.txt");    public SavingPositionThread(JFrame f) {        this.f = f;    }    public void run(){        while(true){            int x = f.getX();            int y = f.getY();            try(FileOutputStream fos = new FileOutputStream(file);                    DataOutputStream dos = new DataOutputStream(fos);){                dos.writeInt(x);                dos.writeInt(y);            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }            try{                Thread.sleep(100);            } catch(InterruptedException e){                e.printStackTrace();            }        }    }}
package com.gui;import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import javax.swing.JButton;import javax.swing.JFrame;public class TestGUI {    public static void main(String[] args) {        // 主窗体        JFrame f = new JFrame("LoL");        // 主窗体设置大小        f.setSize(400, 300);        // 主窗体设置位置        Point p =getPointFromLocationFile();        if(p!=null)            f.setLocation(p.x,p.y);        else            f.setLocation(200, 200);        // 主窗体中的组件设置为绝对定位        f.setLayout(null);        // 按钮组件        JButton b = new JButton("一键秒对方基地挂");        // 同时设置组件的大小和位置        b.setBounds(50, 50, 280, 30);        // 把按钮加入到主窗体中        f.add(b);        // 关闭窗体的时候,退出程序        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        // 让窗体变得可见        f.setVisible(true);        new SavingPositionThread(f).start();    }    static class Point {        int x;        int y;    }    public static Point getPointFromLocationFile() {        File file = new File("E:/Javalearn/j2se/location.txt");        Point p = null;        try (FileInputStream fis = new FileInputStream(file); DataInputStream dis = new DataInputStream(fis);) {            int x = dis.readInt();            int y = dis.readInt();            p = new Point();            p.x = x;            p.y = y;        } catch (FileNotFoundException e) {            //第一次运行,并没有生成位置文件,所以会出现FileNotFoundException        } catch (IOException e1) {            e1.printStackTrace();        }        return p;       }}
1 0