java - GUI graphical interface

1. A simple SWING example

Exercise - start the window where it was last closed For
example, using this window this time causes the window to be moved to the lower right corner. Close this window, and it will automatically appear in the lower right corner the next time you start it again.

Ideas for ideas :
Start a thread, read the current location information every 100 milliseconds, and save it in a file, such as the location.txt file.
At startup, the location information is read from this file. If it is empty, the default location is used. If it is not empty, the location information is set on the window.
The method of reading position information: f.getX() reads the abscissa information, and f.getY() reads the ordinate information.

Note: This exercise requires the use of multiple threads to complete. There is another way to do it, which is to use the listener. Because I just started to learn GUI and haven't mastered the use of the listener, I temporarily use multithreading to complete this function.

/*
 * SavingPostionThread 用于每隔100毫秒记录当前的位置信息到location.txt中,
 * 记录数据的时候用到了数据输出流可以方便的保存多个整数
 * 接着在TestGUI设计一个静态内部类 Point用于保存x和y。
 * 
 */
public class SavingPositionThread extends Thread {

    private JFrame f;
    File file = new File("location.txt");

    public SavingPositionThread(JFrame f) {
        this.f = f;
    }

    public void run() {
        //因为要动态获取界面的位置,所以要写一个死循环不停获取位置
        while(true) {
            //下面这两行要放在while里面才行,不然每次run都归零了
            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 (Exception e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }

            try {
                Thread.sleep(100);
            }catch(InterruptedException e){
                e.printStackTrace();
            }

        }
    }
}

test:

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("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;  

    }
}

2. Event monitoring

button monitor

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324504787&siteId=291194637