Java基础之视图式用户交互(GUI)

时间:2024-11-17 08:25:03
  • import .*;
  • import ;
  • import ;
  • import .*;
  • import ;
  • import ;
  • import ;
  • import ;
  • import .*;
  • import ;
  • /**
  • * Created by lenovo on 2017/10/18.
  • */
  • public class GUITest {
  • //记录当前页
  • private static int nowPage = 1;
  • private static double WIDTH = 0;
  • private static double HEIGHT = 0;
  • private static String article = "";
  • //每页字数为1428
  • private static int pageSize = 1428;
  • //获得页数
  • private static int maxPageNumber = 0;
  • //文字显示
  • private static JLabel right = null;
  • //打开的窗体的集合
  • private static <JDialog> jDialogs = null;
  • public static void main(String args[]) {
  • jDialogs = new ArrayList<>();
  • //获得屏幕的宽高
  • Toolkit toolkit = ();
  • WIDTH = ().getWidth();
  • HEIGHT = ().getHeight();
  • JFrame jFrame = new JFrame("GUI测试");
  • JMenuBar jMenuBar = new JMenuBar();
  • JMenu jMenu1 = new JMenu("目录选取文件");
  • (new MenuListener() {
  • @Override
  • public void menuSelected(MenuEvent e) {
  • File[] files = getRootFir();
  • showDialog(files);
  • }
  • @Override
  • public void menuDeselected(MenuEvent e) {
  • }
  • @Override
  • public void menuCanceled(MenuEvent e) {
  • }
  • });
  • JMenu jMenu2 = new JMenu("输入目录选取文件");
  • (new MenuListener() {
  • @Override
  • public void menuSelected(MenuEvent e) {
  • JDialog jDialog = new JDialog();
  • //面板容器
  • Panel panel = new Panel();
  • (new FlowLayout());
  • //存放路径的文本框
  • JTextArea jtext = new JTextArea(1, 50);
  • (jtext);
  • //搜索
  • JButton jBtn = new JButton("打开");
  • (new ActionListener() {
  • @Override
  • public void actionPerformed(ActionEvent e) {
  • //开始阅读
  • try {
  • File file = new File(());
  • String fileName = ();
  • String fileType = (('.') + 1, ());
  • if (("txt")) {
  • article = getText(file);
  • maxPageNumber = () / pageSize + (() % pageSize == 0 ? 0 : 1);
  • ("<html>" + (0, pageSize > () ? () : pageSize) + "</html>");
  • nowPage = 1;
  • ();
  • //关闭打开的窗体
  • for (int i = 0; i < (); i++) {
  • (i).dispose();
  • }
  • } else {
  • ("没有找到该文件");
  • }
  • } catch (Exception ex) {
  • ("没有找到该文件");
  • }
  • }
  • });
  • (jBtn);
  • (panel);
  • //使窗口成为最佳大小
  • ();
  • //使窗体位于屏幕*
  • int x = (int) (WIDTH - ()) / 2;
  • int y = (int) (HEIGHT - ()) / 2;
  • //设置窗体位置
  • (x, y);
  • //设置窗体可见
  • (true);
  • //设置窗体在最前边
  • (true);
  • }
  • @Override
  • public void menuDeselected(MenuEvent e) {
  • }
  • @Override
  • public void menuCanceled(MenuEvent e) {
  • }
  • });
  • (jMenu1);
  • (jMenu2);
  • (jMenuBar);
  • //创建布局
  • BorderLayout borderLayout = new BorderLayout();
  • //给窗体设置布局
  • (borderLayout);
  • //设置窗体可见
  • (true);
  • //功能模块
  • Panel type = new Panel();
  • //Dimension的作用是随着窗口的大小改变type会跟着变
  • (new Dimension(150, 800));
  • JButton yjms = new JButton("夜间模式");
  • (new ActionListener() {
  • @Override
  • public void actionPerformed(ActionEvent e) {
  • ();
  • ();
  • ();
  • ();
  • }
  • });
  • (yjms);
  • JButton moms = new JButton("默认模式");
  • (new ActionListener() {
  • @Override
  • public void actionPerformed(ActionEvent e) {
  • ();
  • ();
  • ();
  • ();
  • }
  • });
  • (moms);
  • (type, );
  • Panel content = new Panel();
  • (new BorderLayout());
  • right = new JLabel("<html>欢迎来到Java简易阅读室,希望大家在这里能为自己的梦想插上一双翅膀!!!</html>");
  • (new Font("Dialog", 1, 15));
  • (right, );
  • Panel pageNumber = new Panel();
  • JTextArea jTextArea = new JTextArea();
  • (5);
  • JButton syy = new JButton("上一页");
  • (new ActionListener() {
  • @Override
  • public void actionPerformed(ActionEvent arg0) {
  • nowPage--;
  • if (nowPage > 0)
  • ("<html>" + ((nowPage - 1) * pageSize, nowPage * pageSize) + "</html>");
  • else
  • nowPage = 1;
  • }
  • });
  • (syy);
  • JButton xyy = new JButton("下一页");
  • (new ActionListener() {
  • @Override
  • public void actionPerformed(ActionEvent arg0) {
  • nowPage++;
  • if (nowPage <= maxPageNumber)
  • ("<html>" + ((nowPage - 1) * pageSize, (nowPage * pageSize) >= () ? () : (nowPage * pageSize)) + "</html>");
  • else
  • nowPage = maxPageNumber;
  • }
  • });
  • (xyy);
  • (jTextArea);
  • JButton ym = new JButton("跳转");
  • (new ActionListener() {
  • @Override
  • public void actionPerformed(ActionEvent arg0) {
  • int newPager = (().trim());
  • if (newPager > 0 && newPager <= maxPageNumber) {
  • nowPage = (().trim());
  • ("<html>" + ((nowPage - 1) * pageSize, (nowPage * pageSize) >= () ? () : (nowPage * pageSize)) + "</html>");
  • } else {
  • ("输入范围有误");
  • }
  • }
  • });
  • (ym);
  • (pageNumber, );
  • (content, );
  • //使窗口成为最佳大小
  • ();
  • //设置窗体大小
  • (800, 800);
  • //使窗体位于屏幕*
  • // (null);
  • //使窗体位于屏幕*
  • int x = (int) (WIDTH - ()) / 2;
  • int y = (int) (HEIGHT - ()) / 2;
  • // //设置窗体位置
  • (x, y);
  • //窗口大小不可调
  • (false);
  • }
  • private static void showDialog(File[] files) {
  • JDialog jDialog = new JDialog();
  • int len = 0;
  • for (int i = 0; i < ; i++) {
  • if (files[i].isDirectory()) {
  • len++;
  • } else if (files[i].isFile()) {
  • String fileName = files[i].getName();
  • String fileType = (('.') + 1, ());
  • if (("txt"))
  • len++;
  • }
  • }
  • (new GridLayout(len, 1));
  • for (int i = 0; i < ; i++) {
  • if (files[i].isDirectory())
  • (getJButton(files[i], true));
  • }
  • for (int i = 0; i < ; i++) {
  • //只显示txt类型的文件
  • if (files[i].isFile()) {
  • String fileName = files[i].getName();
  • String fileType = (('.') + 1, ());
  • if (("txt"))
  • (getJButton(files[i], false));
  • }
  • }
  • //使窗口成为最佳大小
  • ();
  • //使窗体位于屏幕*
  • int x = (int) (WIDTH - ()) / 2;
  • int y = (int) (HEIGHT - ()) / 2;
  • //设置窗体位置
  • (x, y);
  • //设置窗体可见
  • (true);
  • //设置窗体在最前边
  • (true);
  • //将打开的窗体放入集合中
  • (jDialog);
  • }
  • //读取文件中的文字
  • public static String getText(File file) {
  • // <String> lines = new ArrayList<>();
  • // try {
  • // BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
  • // String line = null;
  • // while ((line = ()) != null) {
  • // (line);
  • // }
  • // ();
  • // ((0));
  • // return (0);
  • // } catch (Exception e) {
  • // }
  • // return "";
  • try {
  • FileReader fileReader = new FileReader(file);
  • StringBuffer stringBuffer = new StringBuffer();
  • char[] c = new char[1024];
  • int len = 0;
  • while ((len = (c)) != -1) {
  • (c, 0, len);
  • }
  • ();
  • // (());
  • return ();
  • } catch (Exception e) {
  • }
  • return "";
  • }
  • //获得所有盘符
  • public static File[] getRootFir() {
  • // FileSystemView sys = ();
  • File[] files = ();
  • //获得盘符类型
  • // for(int i = 0; i < ; i++) {
  • // (files[i] + " -- " + (files[i]));
  • // }
  • return files;
  • }
  • //获得指定目录下的目录
  • public static File[] getFir(File file) {
  • return ();
  • }
  • public static JButton getJButton(File file, boolean type) {
  • JButton jButton = new JButton();
  • ();
  • ();
  • (type ? "[文件夹]" + (().equals("") ? file : ()) : "[文件]" + (().equals("") ? file : ()));
  • (new MouseAdapter() {
  • @Override
  • public void mouseClicked(MouseEvent e) {
  • if (() == MouseEvent.BUTTON1
  • && () == 2) {
  • if (()) {
  • //关闭打开的窗体
  • for (int i = 0; i < (); i++) {
  • (i).dispose();
  • }
  • //开始阅读
  • article = getText(file);
  • maxPageNumber = () / pageSize + (() % pageSize == 0 ? 0 : 1);
  • ("<html>" + (0, pageSize > () ? () : pageSize) + "</html>");
  • nowPage = 1;
  • } else {
  • File[] files = getFir(file);
  • showDialog(files);
  • }
  • }
  • super.mousePressed(e);
  • }
  • });
  • return jButton;
  • }
  • }