1

SwingWorkerスレッドを使用するための実装が正しくクリーンな方法で実行されていることを再確認したかっただけです。また、model-view-controllerパターンの実装が正しく、クリーンであることを確認する必要があります。すべてが正常に機能しているようで、私にはそれは素晴らしく単純な実装のようです。

Modelクラス。

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;
    }
}

Viewクラス。

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");
    }
}

Controllerクラス。

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