简体   繁体   中英

Automatically Maximize Window Using Netbeans

I have been trying to get the window to automatically maximize using Netbeans.

I've probably looked through 4 or 5 pages of Google for an answer.

The web pages always provide something like this:

public void run() {
    MyFrame myFrame = new MyFrame();
    myFrame.setVisible(true);
    myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}

I am using Netbeans 6.9.1

Does this no longer work? Is there another way to do this?

Also, if you find your answer on a web page, please provide the link so I can look into this further. Thanks in advance for any input! :)

to maximize your form at startup you have to let netbeans do it in its rigth time! You can accomplish this through the JFrame's windowOpened event:

In the JFrame's Properties window, click the Events button;

Click the ellipsis (...) button next to the windowOpened event;

In the Handler dialog box, add a handler called formWindowOpened (as suggested by NetBeans);

Within the formWindowOpened method in the Source Editor, paste the following code:

Code:

    setExtendedState(JFrame.MAXIMIZED_BOTH); 

Good luck!

Regarding setExtendedState() , "Note that if the state is not supported on a given platform, nothing will happen."

If that's not relevant, an sscce may be helpful.

Addendum: This example seems to function correctly:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/5207425 */
public class NewJavaGUI extends JPanel {

    private void display() {
        JFrame f = new JFrame("NewJavaGUI");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new NewJavaGUI().display();
            }
        });
    }
}

Addendum: The relevant state constants appear to form a coherent set. In particular, MAXIMIZED_HORIZ | MAXIMIZED_VERT == MAXIMIZED_BOTH MAXIMIZED_HORIZ | MAXIMIZED_VERT == MAXIMIZED_BOTH :

NORMAL          0 0000
MAXIMIZED_HORIZ 2 0010
MAXIMIZED_VERT  4 0100
MAXIMIZED_BOTH  6 0110

Just insert the code bellow

public  () {
  setExtendedState(MAXIMIZED_BOTH);
}

Put the code below to the initComponents();

public Home() {
        initComponents();
        this.setExtendedState(MAXIMIZED_BOTH);
    }

Put the below code above initcomponents(); :

 public Test() {
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);

    this.setUndecorated(true);
    //use this command to remove the maximize,minimize,close option from the 
    //title.        

     initComponents();

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM