1

我只是想仔细检查我的使用 SwingWorker 线程的实现是否以正确和干净的方式完成。此外,只需要验证我的模型-视图-控制器模式的实现是否正确且干净。一切似乎都在正常工作,对我来说这似乎是一个不错的简单实现。

模型类。

package Model;

public class Model
{
    private int counter;
    private boolean go = true;

    public Void go()
    {
        counter = 0;

        while(go)
        {
            counter++;
            System.out.println(counter);
        }
        return null;
    }

    public int getCounter()
    {
        return counter;
    }

    public String getCounterToString()
    {
        return Integer.toString(counter);
    }

    public void setGo(boolean value)
    {
        this.go = value;
    }
}

视图类。

package View;

import java.awt.*;
import javax.swing.*;

public class View extends JFrame
{
    private JPanel                  topPanel, bottomPanel;
    private JTextArea               messageArea;
    private JButton                 startButton, cancelButton;
    private JLabel                  messageLabel;
    private JScrollPane             scrollPane;

    public View()
    {
        setSize(250, 220);
        setTitle("View");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        topPanel = new JPanel();
        bottomPanel = new JPanel();
        messageArea = new JTextArea(8, 20);
        messageArea.setEditable(true);
        scrollPane = new JScrollPane(messageArea);
        messageLabel = new JLabel("Message Area");
        topPanel.setLayout(new BorderLayout());
        topPanel.add(messageLabel, "North");
        topPanel.add(scrollPane, "South");
        startButton = new JButton("START");
        cancelButton = new JButton("CANCEL");
        bottomPanel.setLayout(new GridLayout(1, 2));
        bottomPanel.add(startButton);
        bottomPanel.add(cancelButton);
        Container cp = getContentPane();
        cp.add(topPanel, BorderLayout.NORTH);
        cp.add(bottomPanel, BorderLayout.SOUTH);
    }

    public JButton getStartButton()
    {
        return startButton;
    }

    public JButton getCancelButton()
    {
        return cancelButton;
    }

    public void setMessageArea(String message)
    {
        messageArea.append(message + "\n");
    }
}

控制器类。

package Controller;

import java.awt.event.*;
import javax.swing.SwingWorker;
import Model.*;
import View.*;

public class Controller implements ActionListener
{
    private Model theModel;
    private View  theView;
    private SwingWorker<Void, Void> worker;

    public Controller(Model model, View view)
    {
        this.theModel = model;
        this.theView = view;
        view.getStartButton().addActionListener(this);
        view.getCancelButton().addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
        Object buttonClicked = ae.getSource();
        if(buttonClicked.equals(theView.getStartButton()))
        {
            theModel.setGo(true);
            worker = new SwingWorker<Void, Void>()
            {
                @Override
                protected Void doInBackground()
                {
                    //theView.setMessageArea(theModel.getCounterToString());
                    return theModel.go();
                }
                @Override
                protected void done()
                {
                    //theView.setMessageArea(theModel.getCounterToString());
                }
            };
            worker.execute();

        }
        else if(buttonClicked.equals(theView.getCancelButton()))
        {
            theModel.setGo(false);
        }
    }
}

主班。

package swinginfiniteloop;

import Model.*;
import View.*;
import Controller.*;

public class Main
{
    public static void main(String[] args)
    {
        Model model = new Model();
        View view = new View();
        Controller controller = new Controller(model, view);
        view.setVisible(true);
    }
}
4

0 回答 0