Build Java Desktop application prototype (ii)

Source: Internet
Author: User
Tags exit pack

Set the system appearance

The following Setsystemlookandfeel () method calls the Setlookandfeel () method of the Javax.swing.UIManager class:

It requires swing to convert from the default metal appearance to a local skin:

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);
  }
}

Typically, an exception is not thrown because the Setlookandfeel () parameter has an available value. However, any exception with the standard log API can be logged as a critical error message:

private static void log(Exception x) {
  Logger.global.severe(x.getMessage());
}

It is possible to use a global log for a prototype, but a product should be using its own log to save the error message in the file.

Create and display a primary window

The Createframe () method creates a mainframe instance and loads the picture:

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 frame resize so that the main panel and the application toolbar are resized to fit the size. Note that the Getmainpanel () and Updatesize () methods are the application methods implemented by the mainframe and Mainpanel classes. The pack () method is inherited from the Java.awt.Window.

The Showframe () method displays the main frame of the application and invokes the Requestfocus () method of the main panel. Without calling Requestfocus (), the focus will be obtained by the Indent drop-down box in the toolbar, which is not the main component of the framework. When the application starts, its main component should get the focus, even if the main panel does not handle any keyboard events.

Call Setdefaultcloseoperation () when the window is closed, disable the default action for this method, and pass Do_nothing_on_close as an argument. The Showframe () method registers its own window listener to handle window shutdown events. When the user closes the main frame, the listener saves an annotated picture, releases the resources occupied by the frame, and ends the application execution 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();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.