Java size of jframe

How to tell a Java JFrame to fill the entire screen

In an earlier tutorial, I shared some code on how to maximize a JFrame, but every once in a while you’ll run into a situation where you want to fill an entire screen with a JFrame, but not call a method to actually maximize it. This is rare, but I just ran into this situation, so I thought I’d share the source code here.

So, here’s a sample Java Swing method that demonstrates how to make a given JFrame fill the full size of the current screen:

private void makeFrameFullSize(JFrame aFrame)

As you can see from the code, the method uses the Toolkit class to determine the current screen size, then sets the size of the given JFrame with the setSize method.

Setting a JFrame to fill half the screen

One good thing about sharing this code is that you can easily use adapt it to set a JFrame to other sizes. For instance, if you want to create a JFrame that is half the width and half the height of the screen, you can just divide the dimensions in half, as shown in this code:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); aFrame.setSize(screenSize.width/2, screenSize.height/2);

If you really want to maximize a JFrame .

Again, use this link if you really want to maximize a JFrame, otherwise use the source code shown in this tutorial if you want to set the frame size to some other dimension, including setting the frame to fit the full screen size, without setting the maximize state.

Источник

Java JFrame size: How to set the JFrame size

Solution: There are several different ways to set the size of a Java JFrame , but I generally use the setPreferredSize method of the JFrame class in combination with the Java Dimension class, like this:

jframe.setPreferredSize(new Dimension(400, 300));

A JFrame size example

Here’s the source code for a complete «JFrame set size» example, showing how to use this setPreferredSize method call in an actual Java program.

import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class JFrameSize < public static void main(String[] args) < // schedule this for the event dispatch thread (edt) SwingUtilities.invokeLater(new Runnable() < public void run() < displayJFrame(); >>); > static void displayJFrame() < // create our jframe as usual JFrame jframe = new JFrame("JFrame Size Example"); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set the jframe size and location, and make it visible jframe.setPreferredSize(new Dimension(400, 300)); jframe.pack(); jframe.setLocationRelativeTo(null); jframe.setVisible(true); >>

More JFrame size information

For more information related to setting the JFrame size, see the Javadoc for the Java Window class (which JFrame inherits from). You’ll want to look at the Javadoc for these methods (part of which I have shown below):

  • setSize — Resizes this component so that it has width w and height h. The width and height values are automatically enlarged if either is less than the minimum size as specified by previous call to setMinimumSize.
  • setMinimumSize — Sets the minimum size of this window to a constant value. If current window’s size is less than minimumSize the size of the window is automatically enlarged to honor the minimum size. If the setSize or setBounds methods are called afterwards with a width or height less than that specified by setMinimumSize the window is automatically enlarged to honor the minimumSize value. Setting the minimum size to null restores the default behavior.
  • setPreferredSize — Sets the preferred size of this component to a constant value. Subsequent calls to getPreferredSize will always return this value. Setting the preferred size to null restores the default behavior.
  • setMaximumSize — Sets the maximum size of this component to a constant value. Subsequent calls to getMaximumSize will always return this value. Setting the maximum size to null restores the default behavior.
Читайте также:  Другая форма си шарп

Note that some of those methods are actually in the Java Component class.

Источник

JFrame Tutorial in Java with Examples | Java Swing

Hello Friends, Welcome to my new post of Java JFrame Tutorial for Beginners. In my last post we have seen Java Environment Setup in Windows and if you don’t know the basic idea of how to run a Java program then go through my previous post.

In this JFrame in Java Tutorial for beginners , we will see how to create JFrame in Java and how to use JFrame using its associated methods.

So let’s begin JFrame in Java Tutorial,

  • 1 JFrame in Java Tutorial for Beginners
    • 1.1 What is JFrame in Java?
    • 2.1 Steps For Creating a GUI JFrame in Java
    • 3.1 setVisible() method
    • 4.1 setDefaultCloseOperation() method
    • 5.1 setSize() method
    • 6.1 setLocation() method
    • 7.1 setBounds() method
    • 8.1 setTitle() method
    • 9.1 setBackground() method
    • 9.2 Steps to Add Color
    • 10.1 setResizable() method

    JFrame in Java Tutorial for Beginners

    What is JFrame in Java?

    • It is an extended version of Java.awt.Frame that adds support for the JFC/Swing architecture.
    • JFrame is the core class of javax.swing package and is used to develop GUI (Graphical User Interface) in which various visual objects like Text Field, Radio Button, Scroll Bar, Check Box etc are embedded. This GUI is called Window pane.
    • JFrame is a window with a title Bar, Border, a resizable Border, menu Bar, Buttons that close or iconify the window and a content pane that acts as a container.
    • JFrame contains a JRootpane, which is where all of the components are added, and this JRootpane is shared among all container objects. When components are added to a JFrame, they are added to the content pane(JRootpane).
    • The JFrame is typically the outermost container used in GUI applications.

    So this was all about JFrame in Java, and now we will see how to create JFrame in Java and then how to use JFrame in Java step by step.

    How to Create JFrame in Java

    Steps For Creating a GUI JFrame in Java

    • Import javax.swing.JFrame package, or you can also use javax.swing.* package. This package will make available the JFrame class.
    • Create a class and give a name to it.
    • Write main method and inside the main method create an object of the JFrame class.
    • Now save the program, compile it, and run it.
    • The most exciting thing about this program is that although you have successfully compiled and run the program, nothing shows up. It is just because the Frame is invisible. To make it visible, we have to call the setVisible() method of the JFrame class using the JFrame class object.

    Setting the visibility of JFrame in Java

    setVisible() method

    • This method is used to control whether a component will be displayed on the screen or not.
    • It takes a Boolean value as an argument.
    • setVisible(false) –“it hides the component by making it invisible “.
    • setVisible(true) –“it reveals the component by making it visible “.

    JFrame tutorial for beginners

    Output :

    • Now, as you can see, the window is visible, and by default, it opens up in the top-left corner of the screen.
    • There is an interesting thing associated with the above program. Although the close button has been clicked, the program will not be removed from the memory because of its TSR(Terminate but stay resident) characteristics. However, Something is missing for the complete termination of the program. The solution is to invoke the setDefaultCloseOperation() method.

    Setting the Default Close Operation of JFrame/How to Close a JFame in Java

    setDefaultCloseOperation() method

    • This method is called when one clicks the Close Button.
    • This method specifies many options for the close Button. We can choose any one of the given constants to determine the close Button’s nature when someone clicks on it. Constants are given below :
    • JFrame.DO_NOTHING_ON_CLOSE – “As the name suggests, it neglects the click, and nothing happens when someone clicks on the Close Button. “
    • JFrame.HIDE_ON_CLOSE –” As the name suggests, it only hides the frame when someone clicks on the Close Button, but the application still remains on the running state. “
    • JFrame.DISPOSE_ON_CLOSE –“As the name suggests, it disposes the frame when someone clicks on the Close Button, but the application still remains on the running state. “
    • JFrame.EXIT_ON_CLOSE –“As the name suggests, it exits the application when someone clicks on the Close Button and removes the program from memory permanently. “

    Источник

Оцените статью