Java Web 缓存服务器 UI 界面 - 主窗口实现
{ "package": "ui", "import": "java.awt.BorderLayout; java.awt.FlowLayout; java.io.File; javax.swing.JButton; javax.swing.JFileChooser; javax.swing.JFrame; javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JScrollPane; javax.swing.JTextArea; javax.swing.JTextField; Inteface.WebCache;", "public": "class MainFrame extends JFrame implements Inteface.MainFrame { // 类的成员变量\n\n// 构造方法和其他方法\n// 图形界面\nprivate JButton Buttonstart; private JButton Buttonstop; private JButton ButtonsetDir; private JButton Buttonsetport; private JTextField Textport; private JTextArea TextMessage; private JPanel right; private JPanel bottom; // Web Cache代理服务器\nprivate WebCache webcache; private String DIR; private int PORT; \n\npublic MainFrame() { super("Web缓存服务器"); init(); } // 初始化主窗口\nprivate void init() { Config(); addCon(); showFrame(); } // 显示主窗口\nprivate void showFrame() { this.pack(); this.setVisible(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } // 添加组件到主窗口\nprivate void addCon() { bottom.add(Buttonstart); bottom.add(Buttonstop); bottom.add(Buttonsetport); bottom.add(ButtonsetDir); \n\nright.add(Textport); right.add(Buttonsetport); bottom.add(right); \n\nthis.add(new JScrollPane(TextMessage)); this.add(bottom, BorderLayout.SOUTH); } \n\nprivate void Config() { // 初始化各个组件\nButtonstart = new JButton("启动"); Buttonstop = new JButton("停止"); Buttonsetport = new JButton("设置端口"); ButtonsetDir = new JButton("设置缓存目录"); Textport = new JTextField(4); TextMessage = new JTextArea(10, 20); right = new JPanel(new FlowLayout()); bottom = new JPanel(); // 设置"设置缓存目录"按钮的动作监听器\nButtonsetDir.addActionListener(e -> { JFileChooser jchose = new JFileChooser(); jchose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int returnValue = jchose.showOpenDialog(this); if (returnValue == JFileChooser.APPROVE_OPTION) { File file = jchose.getSelectedFile(); DIR = file.getPath(); webcache.setdir(DIR); } }); \n\nButtonstop.setEnabled(false); \n\n// 设置"设置端口"按钮的动作监听器\nButtonsetport.addActionListener(e -> { setport(); }); // 设置"停止"按钮的动作监听器\nButtonstop.addActionListener(e -> { Buttonstart.setEnabled(true); Buttonstop.setEnabled(false); Buttonsetport.setEnabled(true); Textport.setEnabled(true); }); start(); // 设置"启动"按钮的动作监听器\nstop(); // 设置"停止"按钮的动作监听器\n} \n\npublic void start() { // 启动按钮的动作监听器\n\nButtonstart.addActionListener(e -> { if (DIR == null) JOptionPane.showMessageDialog(this, "请选择缓存目录"); else if (PORT == 0) JOptionPane.showMessageDialog(this, "请设置端口号"); else { String str = webcache.startServer(); if (str.equals("true")) { Messageappend("启动成功"); Buttonstart.setEnabled(false); Buttonstop.setEnabled(true); Buttonsetport.setEnabled(false); Textport.setEnabled(false); } else { Buttonstart.setEnabled(true); Buttonstop.setEnabled(false); Buttonsetport.setEnabled(true); Textport.setEnabled(true); Messageappend(str); } } \n\n});\n} \n// 在文本域中追加消息\npublic void Messageappend(String str) { if (str == null) TextMessage.append("\n"); \n\nTextMessage.append(str + "\n"); } \n\n// 停止按钮的动作监听器\npublic void stop() { \n\nButtonstop.addActionListener(e -> { webcache.stopServer(); });\n} \n\n// 设置端口号\npublic void setport() { \n\nPORT = Integer.parseInt((Textport.getText().trim())); System.out.println(PORT); webcache.setport(PORT);\n} \n\n// 设置WebCache对象和设置this作为WebCache对象的Frame属性\npublic void setWebCache(Inteface.WebCache webcache) { this.webcache = webcache; this.webcache.setFrame(this); }\n}
原文地址: https://www.cveoy.top/t/topic/pR4u 著作权归作者所有。请勿转载和采集!