Java图形界面:初次接触
2017-03-20 22:08
302 查看
GUI-Graphic User Interface 图形用户界面
JButton是最常见的组件- 按钮
注意:f.setVisible(true); 会对所有的组件进行渲染,所以一定要放在最后面
启动一个线程,每个100毫秒读取当前的位置信息,保存在文件中,比如location.txt文件。
启动的时候,从这个文件中读取位置信息,如果是空的,就使用默认位置,如果不是空的,就把位置信息设置在窗口上。
读取位置信息的办法: f.getX() 读取横坐标信息,f.getY()读取纵坐标信息。
SavingPositionThread
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; } }
相关文章推荐
- 初次接触java
- 写给初次接触JAVA及JAVA应用题的那些
- JAVA memcached初次接触
- .Net高手初次接触JAVA的场景
- <Java日记>初次接触Java
- 第一章 初次接触Java
- Java图形界面开发:SWT全接触
- 全面接触Java手机软件图形界面API
- 初次接触Java,头疼
- 【java】String类的使用以及日期类的初次接触
- javaweb 与jsp页面的交互流程 (初次接触时写)
- java_Swing之弹窗设计初次接触。
- 全面接触Java手机软件图形界面API
- JAVA第三课——L-system分形的初次接触
- 初次接触java
- 初次接触Java
- java axis2初次接触
- 初次接触Java 忘大家多多指教
- Java图形界面接触
- Java图形界面设计