1

我有问题,我不能突然出现。我必须制作一个狂野的图形界面,在某个事件或特定算法的某个动作(如有限状态机)之后启用按钮。

我决定创建两个类,一个用于 GUI,另一个是创建框架后运行的线程。

现在必须等到消息到达套接字,之后他们必须启用一个按钮,然后必须等待您按下此按钮并随后启用两个,依此类推。

然后我在 Frame 类中创建了静态按钮,用于启用/禁用相应的线程。

这是一个正确的解决方案吗?

有没有办法用 GUI 创建“类似有限状态机”?

- 编辑 -

我有一个创建 GUI 的控制器类。在创建之后,我调用应该运行算法的“类过程”。

在 I 类 CONTROLLER 中的按钮和图形对象静态

静态 JButton btn1; 静态 JPanel panel1 静态 JButton btn2;

在 PROCEDURES 类中,将调用上面列出的项目的方法。

示例:当你可以看到 Frame 我需要检查一个特定的变量时,只需更改,我需要启用 btn1 .. 按下 btn1 后,我 abiliare btn2,等等 ..

那么作为解决方案是否正确?

--- 编辑 2 ---

public class Controller extends JFrame {

    static JButton btn1;
    static JPanel panel1;
    static JButton btn2;

    public Controller() {       

        JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(new GridLayout(7, 1, 0, 0));

        //PANEL
        panel1 = new JPanel();      
        FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        panel_chkCom0.add(panel_1);
        btn1 = new JButton("BUTTON 1");     
        btnChk.setEnabled(false);
        btnChk.setPreferredSize(new Dimension(300, 50));
        panel_1.add(btn1);
        panel.add(panel1);

        //TODO

        //When I finish init myself, call my manager thread
        Thread t = new Thread(new SetupProcedure());
        t.start();
    }   
}



public class SetupProcedure implements Runnable {
    protected boolean btnChkIsPress = false;

    public void run(){ 
         SetupManager();
    }

    public void SetupManager() {

        final long POLLING_TIME = 600000;
        boolean isOff = true;

        long pollingTime = 0;
        //Check if it's on

        while(isOff){
            //SIMULATE READING FROM REGISTER
            try {
                Thread.sleep(3000);
                PWisOff = false;
                Controller.btn1.setEnabled(true);               
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }           
        }

        SetupController.btn1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                btnChkIsPress  = true;
            }
        });
        while(!btnChkIsPress){
            System.out.println("..wait..");
        }

        SetupController.btn2.setEnabled(true);

    ...OTHER...

    public String getResponseFromKernel(String request){
        String s = "Response";
        return s;
    }
}
4

1 回答 1

1

Make a data model FiniteStateModel, with listeners for events. Maka a view ViewFrame, with normal non-static buttons, if needed dynamically added buttons or so. The controller Controller can then wire the events to the view, and change so.

You will need EventQueue.invokeLater(new Runnable() { }); to be responsive and to switch to the AWT event queue.


Schematically:

For an easy GUI editor, creating JFrames and JPanels, you could try the NetBeans IDE. (It really helps - concentrating on the business on hand, instead of the plumbing.)

/** Model. */
public class MessagesModel {
    ... controller.onMessageArrived(messageInfo); // Or via a more generic listener pattern.
}

/** View. */
public class ViewFrame extends JFrame {
    private App controller;
    private JButton btn1;

    public ViewFrame(App controller) {
        this.controller = controller;
        ...
    }

    ... controller.markMessageRead(messageInfo);
}

/** Controller. */
public class App {
    private MessagesModel model;
    private ViewFrame view;

    public static void main(String[] args) {
        new App().start();
    }

    public void start() {
        model = new MessagesModel();
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                view = new ViewFrame(this);
                view.setVisible(true);
            });
    }

    /** Called by the model. */
    public void onMessageArrived(MessageInfo info) {
        view.setSomeAlert(info.title);
    }

    /** Called by the view. */
    public void markMessageRead(MessageInfo info) {
        model.markMessageRead(info);
    }
}

The model informs the controller of changes, and the controller calls a corresponding view method. This can be done by an event listener, or for simplicities sake, passing the controller to the model's constructor.

The advantage of MVC being that all the business logic / control flow is concisely written in the controller class.

于 2012-12-16T14:35:11.440 回答