- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
I want to resize and position a JFrame in Java. How can I achieve that?
To resize and position JFrame, use the Dimensions class. Here, we have set the bounds for the frame −
int width = 500; int height = 400; Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); frame.setBounds((int) size.getWidth() - width, 0, width, height);
The following is an example to resize and poisiton a frame −
Example
package my; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo { public static void main(String[] args) throws Exception { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Register!"); JLabel label1, label2, label3; frame.setLayout(new GridLayout(2, 2)); label1 = new JLabel("Id", SwingConstants.CENTER); label2 = new JLabel("Age", SwingConstants.CENTER); label3 = new JLabel("Password", SwingConstants.CENTER); JTextField emailId = new JTextField(20); JTextField age = new JTextField(20); JPasswordField passwd = new JPasswordField(); passwd.setEchoChar('*'); frame.add(label1); frame.add(label2); frame.add(label3); frame.add(emailId); frame.add(age); frame.add(passwd); int width = 500; int height = 400; Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); frame.setBounds((int) size.getWidth() - width, 0, width, height); frame.setVisible(true); } }
Output
- Related Articles
- How can I resize the root window in Tkinter?
- I want to use
- How can I position JButtons vertically one after another in Java Swing?
- I Want Something in a Cage
- How can I determine the position of a Toplevel in Tkinter?
- I want to call JButton doClick() method to simulate a click action in Java
- How can we minimize/maximize a JFrame programmatically in Java?
- I want to teach in a school, without doing B.Ed. Will I be recruited?
- How can I reverse a string in Java?
- How I can reverse a Java Array?
- How to activate and deactivate JFrame in Java
- Where and how can I create a private constructor in Java?
- How to create a Titleless and Borderless JFrame in Java?
- How can I dynamically set the position of view in android?
- I want to return the next node after this node in a JTree with Java

Advertisements