2017-06-30 3 views
0

Désolé pour l'ignorance, je suis un débutant. J'essaye de redimensionner un JFileChooser de sorte que ce soit la taille de la résolution d'écran. Pour obtenir la résolution I utilisé le code suivant:Redimensionner JFileChooser

GraphicsDevice gd = GraphicsEnvironment 
     .getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
int width = gd.getDisplayMode().getWidth(); 
int height = gd.getDisplayMode().getHeight(); 

J'ai cherché, mais je ne pouvais pas trouver comment changer efficacement la taille de JFileChooser. J'ai testé quelques solutions que j'ai trouvées, mais elles n'ont pas fonctionné.

+0

Considérons 'JFrame. MAXIMIZED_BOTH'; ajoutez le 'JFileChooser' à un panneau ayant' GridLayout'. – trashgod

Répondre

0

Essayez ceci (notez les commentaires):

import java.awt.BorderLayout; 

import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class FileChooserFrame extends JFrame { 

    public FileChooserFrame() {//construct a JFrame 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); //maximize to full screen 

     //constract japnel. apply a border layout manager to it 
     JPanel mainPanel = new JPanel(new BorderLayout()); 
     add(mainPanel); //add panel to frame 

     JFileChooser fc = new JFileChooser(); //construct file chooser 
     mainPanel.add(fc, BorderLayout.CENTER); //add it to panel 
     setVisible(true); 
    } 

    public static void main(String[] args) { 

     new FileChooserFrame(); 
    } 
} 


Si aucun autre composants doivent être ajoutés au cadre, vous pouvez ajouter le fichier-chooser directement sur le cadre (non recommandé):

public FileChooserFrame() {//construct a JFrame 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); //maximize to full screen 
    JFileChooser fc = new JFileChooser(); //construct file chooser 
    add(fc); //add file chooser to frame 
    setVisible(true); 
}