相关疑难解决方法(0)

重写到MVC后GUI无法正常工作

我正在练习MVC风格的编程.我在一个文件中有一个Mastermind游戏,工作正常(可能除了"Check"按钮在开始时不可见).

http://paste.pocoo.org/show/226726/

但是当我把它重写为模型,视图,控制器文件时 - 当我点击空Pin(应该更新,并重新绘制新颜色)时 - 注意到了.谁能在这里看到任何问题?我尝试在不同的地方放置repaint(),但它根本不起作用:/

主要:

public class Main { 
    public static void main(String[] args){
        Model model = new Model();
        View view = new View("Mastermind", 400, 590, model);
        Controller controller = new Controller(model, view); 
        view.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

型号:

import java.util.Random;

public class Model{
    static final int
    LINE = 5,
    SCORE = 10, OPTIONS = 20;
    Pin pins[][] = new Pin[21][LINE];
    int combination[] = new int[LINE];
    int curPin = 0;
    int turn = 1;
    Random generator = new Random(); …
Run Code Online (Sandbox Code Playgroud)

java model-view-controller user-interface swing

121
推荐指数
2
解决办法
4万
查看次数

MVC进度条线程

我正在为我的设计使用MVC模式,当用户按下搜索按钮时,我在模型中调用搜索,但我还想更新从该模型返回的信息的进度条.

我尝试过使用swingworker,但进度条没有更新.我怀疑我的线程出错了.

我在控制器中定义的按钮是:

 class SearchBtnListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            _view.displayProgress();  
        }    
}
Run Code Online (Sandbox Code Playgroud)

这将调用模型中的搜索,并在视图中进行以下调用:

public void displayProgress() {

    TwoWorker task = new TwoWorker();
    task.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent e) {
             if ("progress".equals(e.getPropertyName())) {
                _progressBar.setValue((Integer) e.getNewValue());
             }
         }

     });
     task.execute();             
}      


private class TwoWorker extends SwingWorker<Void, Void> {        
    @Override
    protected Void doInBackground() throws Exception {
        _model.startSearch(getTerm());                  // time intensive code
        File file = new File("lock");           
        while (file.exists()){
            setProgress(_model.getStatus());
            System.out.println(_model.getStatus()); // never called
        }           
        return null;
    } …
Run Code Online (Sandbox Code Playgroud)

java model-view-controller swing multithreading swingworker

9
推荐指数
1
解决办法
4259
查看次数