Web缓存服务器图形界面 - Java Swing实现
package ui; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import Inteface.WebCache; public class MainFrame extends JFrame implements Inteface.MainFrame { // 构造方法和其他方法 // 图形界面 private 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代理服务器 private WebCache webcache; private String DIR; private int PORT; public MainFrame() { super("Web缓存服务器"); init(); } // 初始化主窗口 private void init() { Config(); addCon(); showFrame(); } // 显示主窗口 private void showFrame() { this.pack(); this.setVisible(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } // 添加组件到主窗口 private void addCon() { bottom.add(Buttonstart); bottom.add(Buttonstop); bottom.add(Buttonsetport); bottom.add(ButtonsetDir); right.add(Textport); right.add(Buttonsetport); bottom.add(right); this.add(new JScrollPane(TextMessage)); this.add(bottom, BorderLayout.SOUTH); } private void Config() { // 初始化各个组件 Buttonstart = 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(); // 设置"设置缓存目录"按钮的动作监听器 ButtonsetDir.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); } }); Buttonstop.setEnabled(false); // 设置"设置端口"按钮的动作监听器 Buttonsetport.addActionListener(e -> { setport(); }); // 设置"停止"按钮的动作监听器 Buttonstop.addActionListener(e -> { Buttonstart.setEnabled(true); Buttonstop.setEnabled(false); Buttonsetport.setEnabled(true); Textport.setEnabled(true); }); start(); // 设置"启动"按钮的动作监听器 stop(); // 设置"停止"按钮的动作监听器 } public void start() {// 启动按钮的动作监听器 Buttonstart.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); } } }); } // 在文本域中追加消息 public void Messageappend(String str) { if (str == null) TextMessage.append("\n"); TextMessage.append(str + "\n"); } // 停止按钮的动作监听器 public void stop() { Buttonstop.addActionListener(e -> { webcache.stopServer(); }); } // 设置端口号 public void setport() { PORT = Integer.parseInt((Textport.getText().trim())); System.out.println(PORT); webcache.setport(PORT); } // 设置WebCache对象和设置this作为WebCache对象的Frame属性 public void setWebCache(Inteface.WebCache webcache) { this.webcache = webcache; this.webcache.setFrame(this); } }
原文地址: https://www.cveoy.top/t/topic/pSZb 著作权归作者所有。请勿转载和采集!