简体   繁体   中英

Java Window Event - Maximize. How to hardcode?

I have a JDesktopPane and a JInternalFrame. I'd like the JInternalFrame to automatically maximize after I make it. How can I hardcode the "maximize window" event?

Use JInternalFrame.setMaximum(true) after you create your frame.

Here is how you can maximize your frame:

JInternalFrame frame = ...
frame..setMaximum(true); // Maximize this window to fill up the whole desktop area

Setting the method setMaximum(boolean b) of JInternalFrame to "true" , will maximize it.

eg:

JInternalFrame.setMaximum(true)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class JInternalFrameMaximumTest {
  public JComponent makeUI() {
    final JDesktopPane desktop = new JDesktopPane();
    Action a1 = new AbstractAction("JInternalFrame#setMaximum") {
      @Override public void actionPerformed(ActionEvent e) {
        JInternalFrame f = new JInternalFrame("#",true,true,true,true);
        desktop.add(f);
        f.setVisible(true);
        try {
          f.setMaximum(true);
        } catch(java.beans.PropertyVetoException ex) {
          ex.printStackTrace();
        }
      }
    };
    Action a2 = new AbstractAction("DesktopManager#maximizeFrame(f)") {
      @Override public void actionPerformed(ActionEvent e) {
        JInternalFrame f = new JInternalFrame("#",true,true,true,true);
        desktop.add(f);
        f.setVisible(true);
        desktop.getDesktopManager().maximizeFrame(f);
      }
    };
    JToolBar toolbar = new JToolBar("toolbar");
    toolbar.add(new JButton(a1));
    toolbar.add(new JButton(a2));

    JPanel p = new JPanel(new BorderLayout());
    p.add(desktop);
    p.add(toolbar, BorderLayout.NORTH);
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JInternalFrameMaximumTest().makeUI());
    f.setSize(640, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

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