Avatar billede fredand Forsker
18. januar 2011 - 15:00 Der er 5 kommentarer og
1 løsning

How to maximaze a window correct?

Hello Guys!

I need to maximize a JFrame.
But the problem is that it overlays the desktop bar with applications at the bottom of the screen.

I would like to have the bar with the applications visible.

If you test this code In hope you see my problem:

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


public class TestFrame
{
    private static JFrame frame;
   
    public static JFrame getFrame()
    {
        return frame;
    }

    private static void setFrame(JFrame jFrame)
    {
        frame = jFrame;
    }
   
    private static void createAndShowGUI()
    {
        setFrame(new JFrame("Test"));   
        getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getFrame().setResizable(false);
        getFrame().pack();
        getFrame().validate();
        getFrame().setVisible(true);
        getFrame().setExtendedState(getFrame().getExtendedState() | JFrame.MAXIMIZED_BOTH);
        getFrame().getContentPane().add(new JPanel());   
    }
   
    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}


Best regards
Fredrik
Avatar billede arne_v Ekspert
18. januar 2011 - 23:38 #1
You want all other apps to hide the bar but this specific Java app you don't want to hide the bar?
Avatar billede fredand Forsker
19. januar 2011 - 08:29 #2
Hello!

No I would like this Java app behave like other apps.

The code above fills the screen complete, you can not see the bar at the bottom of your screen.

I would like it to behave like IE at this screen shot:
http://fredand44.webs.com/bilder/screenshot/desktop.png

As you can see the bar at the bottom is visible below the IE window.

So if you see my misstake please let me know!

Best regards

Fredrik
Avatar billede arne_v Ekspert
20. januar 2011 - 03:04 #3
Try this:

import java.awt.GraphicsEnvironment;

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

public class TestFrame
{
    private static JFrame frame;
 
    public static JFrame getFrame()
    {
        return frame;
    }

    private static void setFrame(JFrame jFrame)
    {
        frame = jFrame;
    }
 
    private static void createAndShowGUI()
    {
        setFrame(new JFrame("Test")); 
        getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getFrame().setResizable(false);
        getFrame().pack();
        getFrame().validate();
        getFrame().setVisible(true);
        getFrame().setMaximizedBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds());
        getFrame().setExtendedState(getFrame().getExtendedState() | JFrame.MAXIMIZED_BOTH);
        getFrame().getContentPane().add(new JPanel());   
    }
 
    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}
Avatar billede arne_v Ekspert
20. januar 2011 - 03:05 #4
PS: The number of static does look a bit high !!
Avatar billede fredand Forsker
20. januar 2011 - 08:15 #5
Hello Arne!

What I wonderful line of code that is:
"GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds()"

You soleved it in the best of ways.
I have never woek or explore that class.

Thanks alot for your help, I was really stucked!

(Thanks for the comment about statics, I have to omitt that I use it to often)

Please leave a "svar".

Best reagrds
Fredrik
Avatar billede arne_v Ekspert
20. januar 2011 - 15:13 #6
.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester