2012-02-24 17 views
6

Sto cercando di ottenere il massimo di JInternalFrame al momento del lancio. Ho fatto ricerche sul web su questo e ho provato vari suggerimenti di codice, ma non sembrano funzionare correttamente sulla mia macchina, che esegue Java 6 su Windows 7.Ingrandimento di JInternalFrame in Java

Ho semplificato il codice qui sotto in modo che sia più facile da isolare la soluzione.

Qualcuno può mostrarmi come modificare il codice di seguito in modo che il frame interno venga ingrandito automaticamente quando viene creato?

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 

import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JLayeredPane; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.KeyStroke; 

public class MaximizeInternalFrame extends JFrame implements ActionListener{ 
private static final long serialVersionUID = 1L; 
JLayeredPane desktop; 
JInternalFrame internalFrame; 

public MaximizeInternalFrame() { 
    super("Test To Maximize Internal Frame"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //Make the big window be indented 50 pixels from each edge of the screen. 
    int inset = 50; 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    Dimension screenMinus50 = new Dimension(screenSize.width - inset*2, screenSize.height - inset*2); 

    this.setPreferredSize(screenMinus50); 
    desktop = new JDesktopPane(); 
    setJMenuBar(createMenuBar()); 
    this.add(desktop, BorderLayout.CENTER); 
    this.pack(); 
    this.setSize(screenMinus50); 
    this.setLocationRelativeTo(null); 
} 
protected JMenuBar createMenuBar() { 
    JMenuBar menuBar = new JMenuBar(); 
    //Set up the File menu. 
    JMenu FileMenu = new JMenu("File"); 
    FileMenu.setMnemonic(KeyEvent.VK_F); 
    menuBar.add(FileMenu); 
    //Set up the first menu item. 
    JMenuItem menuItem = new JMenuItem("New"); 
    menuItem.setMnemonic(KeyEvent.VK_N); 
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK)); 
    menuItem.setActionCommand("new"); 
    menuItem.addActionListener(new OpenListener()); 
    FileMenu.add(menuItem); 
    //Set up the second menu item. 
    menuItem = new JMenuItem("Quit"); 
    menuItem.setMnemonic(KeyEvent.VK_Q); 
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK)); 
    menuItem.setActionCommand("quit"); 
    menuItem.addActionListener(this); 
    FileMenu.add(menuItem); 

    return menuBar; 
    } 
class OpenListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     // create internal frame 
     internalFrame = new JInternalFrame("this internal frame needs to be maximized", true, true, true, true); 
     desktop.add(internalFrame); 
     internalFrame.setSize(internalFrame.getMaximumSize()); 
     internalFrame.pack(); 
     internalFrame.setVisible(true); 
    } 
} 
public static void main(String args[]) { 
    MaximizeInternalFrame myParentFrame = new MaximizeInternalFrame(); 
    myParentFrame.setVisible(true); 
} 
public void actionPerformed(ActionEvent e) {if ("quit".equals(e.getActionCommand())){System.exit(0);}} 
} 
+0

[ 'InternalFrameCount'] (http://stackoverflow.com/a/9422246/230513) è un esempio. – trashgod

+1

si prega di imparare le convenzioni di denominazione java e attenersi ad esse – kleopatra

risposta

13

Aggiungere il seguente testo dopo internalFrame.setVisible(true):

try { 
    internalFrame.setMaximum(true); 
} catch (PropertyVetoException e) { 
    // Vetoed by internalFrame 
    // ... possibly add some handling for this case 
} 

È possibile anche rimuovere internalFrame.setSize(internalFrame.getMaximumSize());

+6

FYI ad altri: setMaximum() deve essere chiamato * dopo * il JInternalFrame è stato aggiunto ad un contenitore genitore. Chiamandolo in anticipo genererà un'eccezione. Di buon senso (dopotutto, come si dice a una finestra di riempire il suo genitore se non è stato ancora dato un genitore), ma può essere facilmente trascurato. Non quello * I * l'ho fatto. ;) –

Problemi correlati