Java Web 缓存服务器 GUI 界面 - 使用 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('
');
TextMessage.append(str + '
');
}
// 停止按钮的动作监听器
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);
}
这段代码是一个图形界面的主窗口类,实现了一个MainFrame接口。该类包含了启动、停止和设置端口等按钮,以及显示消息的文本域。其中,init()方法用于初始化主窗口,showFrame()方法用于显示主窗口,addCon()方法用于添加组件到主窗口,Config()方法用于初始化各个组件,并设置各个按钮的动作监听器。start()方法用于启动按钮的动作监听器,当点击启动按钮时,如果缓存目录和端口号都已设置,则调用webcache.startServer()方法启动服务器,并根据启动结果更新按钮的状态。stop()方法用于停止按钮的动作监听器,当点击停止按钮时,调用webcache.stopServer()方法停止服务器。setport()方法用于设置端口号,获取端口号文本框中的值,并调用webcache.setport()方法设置端口号。setWebCache()方法用于设置WebCache对象,并将当前MainFrame对象设置为WebCache对象的Frame属性。
原文地址: https://www.cveoy.top/t/topic/pR3O 著作权归作者所有。请勿转载和采集!