I just want to know some thing about the swing 1) How to use MVC model in swing? 2)Say i Have an Main window and i need to do the menu as separate class ,all the component as separate class and .which will be the best method to integrate it
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Swing has build in mechanism that simplifies MVC implementation. It has Actions framework. Class that is responsible on building view should care about instantiation of the JComponent subclasses and placing them onto panels. Each component that should react on the user's activity should have corresponding Action (
b.setAction(myAction)
). I typically create packagecom.myapp.actions
and put all actions there. Sometimes I create abstract action as well but it is application specific. Actions allow you to separate the logic from presentation layer. Think about action as a entry point to "Model".Typical application has more controls than actions. Some controls reuse the same action. For example you can save file by typing Ctrl-S, clicking menu item or toolbar button, using context menu etc. But all theses controls will invoke the same action
SaveFileAction
.Concerning to your second question there are 2 different ways. First is based on inheritance. There are people that extend JFrame when they need frame and implement all layout into this special class.
Other approach is to create a set of utility methods that generate the layout. I personally prefer this one. I think that inheritance should be used if you really need a subclass of something for example when you wish to override one of super class methods (e.g.
paint()
)I hope my description helps. Good luck.
OK, this is called answering with overkill, so sorry about that, but here's a quick example I've whipped up that tries to use a simple MVC pattern to do a trivial thing: press a button and change the text in a JTextField. It's overkill because you could do the same thing in just a few lines of code, but it does illustrate some MVC in separate files and how the model controls the State. Please ask questions if anything is confusing!
The main class that puts all together and gets things started:
The view class:
The Control:
The model uses a PropertyChangeSupport object to allow other objects (in this situation the View) to listen for changes in state. So the model is in effect our "observable" while the view is the "observer"
A simple enum, State, to encapsulate the concept of state:
edit: I see you mentioned menu as well, so I've added menu support with the addition of this class and the addition of several lines in the SwingMcvTest class. Note that because of separation of code, it was trivial to make this change to the GUI since all the menu needs to do is call control methods. It needs to know nothing of the model or the view:
God that's a lot of code to do a trivial bit of chit! I nominate myself and my code for this week's stackoverflow Rube Goldberg award.