Maximize AWT FRAME and cancel it

zhaozj2021-02-16  83

Sender: TTLINK (Skywalker), the letter area: Java Title: Maximize AWT Frame and cancel its modified letter station: Sun and Moonlight (December 19, 2002 10:26 Thursday), station letter

http://www.cn-java.com/target/news.php?news_id=237

Java 1.4 now allows the Frame's Title Bar and allows maximization of Frame by programming control. Through this quick start, John Zukowski describes in detail and demonstrates these changes, and also describes how to support Frame without a drag region. Please communicate with this article and other readers in the discussion forum and other readers.

When reading the Enhancements of the latest release of the J2SE platform, you will immediately notice that Sun is ultimately listening to our suggestions. I am not implying that they don't listen to our suggestions at all, but they seem to put more attention to add large-scale APIs, rather than patching those APIs that have been put into use. For example, the AWTFRAME class. Although Sun adds Frame icon-based functionality to the version 1.2, you still have no hide the title bar or maximize Frame. Although you can use Window to avoid Title Bar, some tasks require a top-level Frame instead of a window. Basically, there will be no luck.

Now, use the 1.4 release version, you can hide the platform-specific window modification, such as Title Bar, and maximize Frame. Both functions were first proposed in 1997. The generation of non-modified Frame support is due to the spring error database recorded related errors in the spring of 1997, the error identification number is 4038769, and Frame Zoom Support is proposed in August 1997, the error identification number is 4071554. I will explain how to use these two functions herein.

Robinable Frame

The easiest way to use these two features is to support no modified Frame. To hide the title bar on the Frame, you need to set the undecorated property of the specified frame to True. By default, this value is false. When the frame is displayed, you cannot change this setting (if you try to change, you will throw an IllegalComponentStateException).

Listing 1. Create a modified Frame

Frame frame = new frame (); frame.setundecoated (TRUE);

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

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 (PX E.GETX () - Origin .x, py E.Gety () - Origin.y);}}); Maximize Frame

The setState () method is added to version 1.2 to implement programming control icon. Now, in version 1.4 is the setExtendedState () method. This method uses a bit mask flag of acceptable state (a total of 4). The 5th marker Maximized_both is a combination of the other two. Acceptable states:

Normal - Frame Normal Size

Iconified - iconized state

Maximized_both - frame maximizes status (maximize horizontal and maximize)

Maximized_horiz - maximize the level (window environment may not be supported)

Maximized_vert - Vertical Maximization Status (window environment may not be supported)

Typically, you may use only the first three states. If you find a needle status, such as what you want to make a frame icon, but keep its current maximization status, you have to use vertical lines (|) to combine multiple status. Then, once you have already determined the correct state, you should use the new setExtendedState () method to change the Frame status.

Listing 3. Keep the maximum state

// preserve maximized frame.seXtendedState (frame.iconified | frame.getextendedState ());

A complete example

Below is a complete example, it hides Frame modifications, then adds a window button for maximizing, normalization, and iconization (minimize) window, and close this frame by simple AWT button components. In addition to the button, there is a region for dragging Frame.

Listing 4. Complete example of frame modification

Import java.awt. *; import java.awt.event. *;

public class FrameTest {static Point origin = new Point (); public static void main (String args []) {final Frame frame = new Frame (); frame.setUndecorated (true); 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 (PX E.GETX () - Origin.x, Py E.Gety () - Origin.y);}}); Frame.Setsize (300, 300); Button b1 = new Button ( "Maximize"); b1.addActionListener (new ActionListener () {public void actionPerformed (ActionEvent e) {frame.setExtendedState (Frame.MAXIMIZED_BOTH);}}); Button b2 = new Button ( "Iconify") B2.AddActionListener (New ActionListener () {Public Void ActionPerformed (ActionEvent E) {// Preserve Maximizing Frame.s etExtendedState (Frame.ICONIFIED | frame.getExtendedState ());}}); Button b3 = new Button ( "Normal"); b3.addActionListener (new ActionListener () {public void actionPerformed (ActionEvent e) {frame.setExtendedState (Frame .NORMAL);}}); Button B4 = New Button ("close"); b4.addActionListener (new actionListener () {public void actionPerformed (ActionEvent E) {system.exit (0);}}); frame.setLayout (NEW GRIDLAYOUT (5, 1)); frame.Add (b1); frame.Add (b3); frame.Add (b4); frame.show ();

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

New Post(0)