The Magic of Merlin: another simple frame

Source: Internet
Author: User
Tags gety

As you read the enhancements to the latest release of the J2SE platform, you will immediately notice that Sun finally listens to our suggestions. I'm not suggesting that they didn't listen to us at all, but they seem to be focusing more on adding large APIs rather than patching up APIs that have been in use for years. For example, the AWT Frame class. Although Sun added the ability to programmatically icon a frame in version 1.2, you still cannot hide the Title bar or maximize the frame. Although you can use Windows to avoid the Title bar, some tasks require a top-level Frame instead of a window. There is basically no luck.

Now, with the 1.4 release, you can programmatically hide platform-specific window adornments, such as Title Bar, and maximize the Frame. Both of these functions were first proposed as early as 1997. The absence of modified Frame support was due to the fact that in the spring of 1997, Sun's error database recorded related errors, and the error identification number for 4038769,frame scaling support was presented in August 1997, with the error identification number 4071554. I'll show you how to use both of these features in this article.

Non-decorated Frame

The simplest way to use these two features is to support unmodified Frame. To hide the Title bar on a frame, you need to set the Undecorated property of the specified frame to true. By default, this value is false and you cannot change this setting when the Frame is displayed (the system throws a Illegalcomponentstateexception exception if you try to change it).

Listing 1. Create a no-decorated Frame

Frame frame = new Frame();
  frame.setUndecorated(true);

Because Title Bar and other window decorations are now hidden, you cannot rely on the underlying window management system to provide support for the drag Frame. You must add this support yourself with a pair of mouse listeners.

Listing 2. Add drag Support

// Avoid creating a point with each mousePressed() call
  Point origin = new Point();
  frame.addMouseListener(new MouseAdapter() {
   public void mousePressed(MouseEvent e) {
    origin.x = e.getX();
    origin.y = e.getY();
   }
  });
  frame.addMouseMotionListener(new MouseMotionAdapter() {
   public void mouseDragged(MouseEvent e) {
    Point p = frame.getLocation();
    frame.setLocation(
     p.x + e.getX() - origin.x,
     p.y + e.getY() - origin.y);
   }
  });

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.