开发者

Java GUI building tips?

开发者 https://www.devze.com 2023-01-11 04:49 出处:网络
I\'m building a Java GUI for some code, and I\'m not quite sure about some design aspects. As some background - The GUI will be used only for one stream of activity

I'm building a Java GUI for some code, and I'm not quite sure about some design aspects.

As some background - The GUI will be used only for one stream of activity

  • The user loads a file, the file appears on the scre开发者_JAVA技巧en and the user is shown the effects of mathematical transformation 1, transformation 2, and transformation 3 each time after pressing a button to indicate a yes/no answer.

What I was wondering was - is it ok design to somewhat hard code the GUI code so that after the file is loaded, the events associated with the buttons change from connecting with transformation(s) 1 to 2 to 3?

The functionality for these transformations is implemented in another Java file, and I was wondering how I separation/communication between the two would work; or if I should just go ahead and use one file to cover both the mathematical functionality to be performed with the GUI items.

Please feel free to ask any questions that you think will help you answer this question.


Do some research on the Model-View-Controller paradigm; it's probably best suited for what you're trying to do.

In your current setup, the code you have written is the model. It contains all of the 'business logic' for your application. What you need to write is the front end, the View and the Controller. The Controller acts as the intermediary between the View and the Model; when you click on a button (a view element) the associated button handler (the controller) will delegate to the Model by calling some method. For instance

public class View extends JPanel implements ActionListener {

  JButton transform = new JButton("Transform1");
  Model m;
  public View(Model m) {
       super();
       this.m = m;
       transform.setActionListener(this);
       // lay out the GUI
       setLayout(...);
       add(// whatever view you need to visualize the data);
       add(transform);
  }

  /**
   Make sure all of the GUI elements are correct */
  public void update() {
    State transformState = m.getTransformState();
    transform.setText(transformState.getNextStateDescription());

    // Draw the state of the file they loaded in, probably to another JPanel
  }

  public void actionPerformed(ActionEvent e) {
    m.transform();
  }

}

In small examples it's probably fine to have the View and Controller intermingled like this (the view is handling the button presses in this example).

As far as communication among the two, what I would recommend is that you make your Model extends Observable

public class Model extends Observable {
    public void transform() {

       // Do the transformation

       // notify the view
       setChanged();
       notifyObservers();
    }
}

and make the View implement Observer

public class View implements ActionListener, Observer {

    public View(Model m) {
            // snip
            m.addObserver(this);
    }

    public void     update(Observable o, Object arg) {
        // Delegate to previously defined method
        update();
    }

}


I would definitely separate the GUI and the math into different classes. As for the GUI, I'd suggest not getting bogged down too much with getting the class design right, and just do it in the simplest way possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消