How to implement CMD + N in Java (OS X)
I'm writing one of my first Java-Applications for OSX.
As you might know the handling of applications in OSX differs a little bit from Windows. In windows 1 JFrame normaly means that this is one instance of the application, so if I want to close the whole application I just close the window.
In OSX the windows and the application are 2 seperate things. When you close the window an application keeps running in the background.
What I want to do:
- When the user hits CMD + N I want to c开发者_如何学Create a new Instance of my class Application
Is there something like a best practise for this? Who has to listen to Key-Stroke's when there is no active window? The class where main is implemented?
There is no reason to have this different on Windows and OSX. If you implement what Windows calls Multiple Document Interface (MDI) it works exactly the same on OSX as it does on Windows. They will look a little different, but conceptually they are identical. In both cases, there is a menu bar (Application class) that controls zero or more document windows (a JFrame instance).
So you have a main class that creates a single Application, which creates your menu bar and begins listening for key strokes, it may also create an initial window, depending upon your application rules. Once it is running, you can listen for CMD-N to create new JFrame.
As an aside, in Windows, 1 JFrame can mean a seperate instance, but it doesn't always have to mean that. Consider the way Internet Explorer creates new window instances -- if you use Cntl-N to create a new window, it shares session and other resources with the parent window, that is, it is not seperate, but just another document. At the same time, you can launch another IE instance from your start menu and it will NOT share session or resources with the other two running IE windows. Yet all three look like fully separate applications in the task bar.
精彩评论