Showing posts with label center. Show all posts
Showing posts with label center. Show all posts

Easiest way to make JFrame center

Complete source code below will show you, the easiest way to make JFrame get to the center of screen by only one line of code. We will use method setLocationRelativeTo(null) to make our JFrame get to the center. Remember, when you want to use this code, you must put it after you set JFrame size.

*****************************************************************
COMPLETE SOURCE CODE FOR : EasiestWayToMakeJFrameCenter.java
*****************************************************************


import javax.swing.JFrame;

public class EasiestWayToMakeJFrameCenter
{
public static void main(String[]args)
{
JFrame frame=new JFrame("Easiest way to make JFrame center");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);

//setLocationRelativeTo(null) will make JFrame get to the center
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Set JLabel text center

Complete source code below will show you, how to make text in JLabel to center.

****************************************************************************
COMPLETE SOURCE CODE FOR : JLabelTextToCenter.java
****************************************************************************


import javax.swing.JLabel;
import javax.swing.JFrame;

import javax.swing.SwingConstants;

public class JLabelTextToCenter
{
public static void main(String[]args)
{
//Create a label with text ( I am a JLabel )
JLabel label=new JLabel("I am a JLabel");

//Make JLabel text to center
label.setHorizontalAlignment(SwingConstants.CENTER);

//Create a window using JFrame with title ( JLabel text to center )
JFrame frame=new JFrame("JLabel text to center");

//Add created label into JFrame
frame.add(label);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,400);

//Make JFrame visible
frame.setVisible(true);
}
}


****************************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************************

Set JPasswordField text center

Complete source code below will show you, how to make text in JPasswordField start at center.

****************************************************************************
COMPLETE SOURCE CODE FOR : SetJPasswordFieldContentsToCenter.java
****************************************************************************


import javax.swing.JPasswordField;
import javax.swing.JFrame;

import javax.swing.SwingConstants;

public class SetJPasswordFieldContentsToCenter
{
public static void main(String[]args)
{
JPasswordField passwordField=new JPasswordField(10);

//Make contents in JPasswordField start at center
passwordField.setHorizontalAlignment(SwingConstants.CENTER);

JFrame frame=new JFrame("Set JPasswordField contents to center");
frame.add(passwordField);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,65);
frame.setVisible(true);
}
}


****************************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************************

Set JTextField text center

Source code below will show you, how to make text in JTextField start at center.

************************************************************************
COMPLETE SOURCE CODE FOR : SetJTextFieldTextToCenter.java
************************************************************************


import javax.swing.JTextField;
import javax.swing.JFrame;

import javax.swing.SwingConstants;

public class SetJTextFieldTextToCenter
{
public static void main(String[]args)
{
JTextField textField=new JTextField(10);

//Make text in JTextField start at center
textField.setHorizontalAlignment(SwingConstants.CENTER);

JFrame frame=new JFrame("Set JTextField text to center");
frame.add(textField);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,65);
frame.setVisible(true);
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

RELAXING NATURE VIDEO