时间紧 今天就需 大姥求解

时间紧 今天就需 大姥求解
感谢大姥的解决方案 很给力

img

img


package Programe;

import Intent.WebCache;
import ui.MainFrame;

public class Start {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    MainFrame minframe=new MainFrame();
    WebCache webCache=new WebCache();
    minframe.setWebCache(webCache);
    
}

}

放下手上所有的事情,以宇宙速度解决了问题,修改后的完整代码如下:

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();
    }
 
    @Override
    public void start() {
        // TODO Auto-generated method stub
        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");
    }
 
    @Override
    public void stop() {
        // TODO Auto-generated method stub
        Buttonstop.addActionListener(e -> {
            webcache.stopServer();
        });
    }
 
    @Override
    public void setport() {
        // TODO Auto-generated method stub
        PORT = Integer.parseInt((Textport.getText().trim()));
        System.out.println(PORT);
        webcache.setport(PORT);
    }
 
    @Override
    public void setWebCache(Inteface.WebCache webcache) {
        this.webcache = webcache;
        this.webcache.setFrame(this);
    }
 

如有帮助,请采纳,十分感谢!