[Original] Building a desktop application prototype (second part)

xiaoxiao2021-03-06  86

[Original] Building a desktop application prototype (second part) Setting the system appearance

The following setsystemLookAndfeel () method calls the setLookAndfeel () method for the Javax.swing.uimanager class: it requires Swing to convert from the default Metal appearance:

private void setSystemLookAndFeel () {try {UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());} catch (UnsupportedLookAndFeelException x) {log (x);} catch (ClassNotFoundException x) {log (x);} catch (IllegalAccessException x) { Log (x);} catch (instantiationexception x) {log (x);}}

Usually, because the setLookandFeel () parameter has a usable value so it will not throw an exception. However, any exception can be recorded as a serious error message using a standard log API: private static void log (Exception x) {logger.global.severe (x.getMessage ());} is available in prototype Activity However, a product should use its own log, save the error message in the file. Create and display the main window createframe () method to create a mainframe instance, and loaded pictures: private void createframe () {mainframe = new mainframe (); mainpanel = mainframe.getmainpanel (); mainpanel.Updatesize (); mainframe.pack ); LoadImage (); Updatesize () sets the reasonable size of the main panel obtained by getMainpanel (). The PACK () method makes the main framework to adjust the main panel and the application toolbar to the appropriate size. Note that getMainPanel () and UpdateSize () methods are the application methods implemented by the MainFrame and Mainpanel class. The PACK () method is inherited from java.awt.window. The showframe () method displays the main framework of the application and calls the main Panel's requestfocus () method. There is no call requestfocus (), the focus will be scaled in the toolbar to get the pull box, this component is not the main component of the frame. When the application starts, its main components should get focus, even if the primary Panel does not process any keyboard events. Call SetDefaultCloseOperation () when the window is closed, disable the default action of this method but passing the DO_NOTHING_ON_CLOSE as a parameter. The showframe () method registers the window listener you have to handle the window to close the event. When the user turns off the main frame, the listener saves an annotated picture, release the resource occupied by the frame and ends the application with system.exit (0). private void showFrame () {mainFrame.setDefaultCloseOperation (MainFrame.DO_NOTHING_ON_CLOSE); mainFrame.addWindowListener (new WindowAdapter () {public void windowClosing (WindowEvent e) {saveImage (); mainFrame.dispose (); System.exit (0);} }); Mainframe.show (); mainpanel.requestfocus ();} Loading and saving images A completed product will use the File dialog to load a source image and save a comment. On the concept, the "File Open" dialog box will enable the preview image, the File Save dialog will allow them to provide different parameters, such as saving the compression quality of the image. Swing Standard File dialog is based on component JFilechooser, which can be customized via the setAccessory () method, allowing you to load your components on the File dialog. In prototype, attention should be mainly functions. Thus, the prototype is replaced by a custom file dialog box by obtaining the load and saving the path from the command line. Javax.Imageio.Imageio class Simple READ () and WRITE () methods are used to load and save images.

转载请注明原文地址:https://www.9cbs.com/read-121401.html

New Post(0)