Pages

Why we use JLabel

JLabel is use when you want to add text or image into you graphical user interface.

Create text area using JTextArea

Source code below will show you how to create text area in java using JTextArea class.

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


import javax.swing.JTextArea;
import javax.swing.JFrame;

public class CreateTextAreaUsingJTextArea
{
public static void main(String[]args)
{
//Create text area without initial text using JTextArea
JTextArea textArea=new JTextArea();

//Create a JFrame that will be use to place JTextArea
JFrame frame=new JFrame("Create text area using JTextArea");

//Add text area into JFrame
frame.add(textArea);

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

//set JFrame size
//WIDTH = 500 pixels
//HEIGHT = 500 pixels
frame.setSize(500,500);

//make JFrame visible. So we can see it
frame.setVisible(true);
}
}


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

Create text area in java

In java, you can create text area using JTextArea class.

Create text field using JTextField

Source code below will show you, how to create text field in java using JTextField class.

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


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

public class CreateTextFieldUsingJTextField
{
public static void main(String[]args)
{
//Create a text field with no initial text in it
JTextField textField=new JTextField();

//Create a JFrame that will be use to place created text field
JFrame frame=new JFrame("Create text field using JTextField");

//add created JTextField into JFrame
frame.add(textField);

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

//set JFrame size
//WIDTH = 400 pixels
//HEIGHT = 65 pixels
frame.setSize(400,65);

//make JFrame visible. So we can see it
frame.setVisible(true);
}
}


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

Create text field in java

In java, you can create text field using JTextField class.

Create button using JButton

Source code below will show you, how to create button in Java using JButton class.

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


import javax.swing.JButton;
import javax.swing.JFrame;

public class CreateButtonUsingJButton
{
public static void main(String[]args)
{
//Create button using JButton
//Button that we create contain text My first button
JButton myButton=new JButton("My first button");

JFrame frame=new JFrame("Create button using JButton");

//add button that we create in JFrame
frame.add(myButton);

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

//set JFrame size
//WIDTH = 300 pixels
//HEIGHT = 100 pixels
frame.setSize(400,100);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Create button in java

In java, you can create button using JButton class.

Why we use JPanel

JPanel we will help java programmer to create more complex graphical user interface. But how ? It answer is, it allow other component to add into it. Component that i mean here like JButton, JLabel or JTextField or others. It also give ability to other JPanel add into it. For example, we has two JPanel called panelA and panelB. We can add panelA into panelB. So, if panelA also contains other components and panelB also contains other component beside panelA that we add before, how complex the graphical user interface...i hope you can imagine it.

Create JPanel in java

Source code below will show you how to create a JPanel in java and after that we will put the JPanel into a JFrame.

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


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

public class CreateAJPanel
{
public static void main(String[]args)
{
//Create a JPanel
JPanel panel=new JPanel();

//Create a JFrame that we will use to add the JPanel
JFrame frame=new JFrame("Create a JPanel");

//ADD JPanel INTO JFrame
frame.add(panel);

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

//Set JFrame size to :
//WIDTH : 400 pixels
//HEIGHT : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


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

JFrame fullscreen

Source code below will show you, how to set JFrame to full screen follow to current computer screen

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


import javax.swing.JFrame;

import java.awt.Toolkit;

public class FullScreenJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Full Screen JFrame )
JFrame frame=new JFrame("Full Screen JFrame");

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

//Set JFrame size to full screen size follow current screen size
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width,Toolkit.getDefaultToolkit().getScreenSize().height);

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


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

Get JFrame size

Source code below will create a JFrame with a button in it. You can know current JFrame size by click the button. You also can try resize the JFrame first, and after that click the button.

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


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.FlowLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GetJFrameSize extends JFrame
{
//Create ActionListener for listen when someone click button
ActionListener al=new ActionListener()
{
//Overwrite abstract method ( actionPerformed ) in ActionListener class
public void actionPerformed(ActionEvent event)
{
//Show message box that contain information about current JFrame width and height in pixels
JOptionPane.showMessageDialog(null,"JFrame size in pixels is : \n"+"WIDTH : "+getSize().width+"\n"+"HEIGHT : "+getSize().height);
}
};

public GetJFrameSize()
{
//Create JFrame title ( Resize me and click button )
super("Resize me and click button");

//Set JFrame layout to FlowLayout
setLayout(new FlowLayout());

//Create a button with text ( Click me to see current JFrame size )
JButton button=new JButton("Click me to see current JFrame size");

//Add ActionListener to the button
button.addActionListener(al);

//Add the button into created JFrame
add(button);

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

//Set initial JFrame size to :
//WIDTH = 500 pixels
//HEIGHT = 500 pixels
setSize(500,500);

//Make JFrame visible
setVisible(true);
}

public static void main(String[]args)
{
GetJFrameSize gjfs=new GetJFrameSize();
}
}


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

Set JFrame size

Source code below will show you, how to set JFrame size

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


import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class SetSizeForJFrame
{
public static void main(String[]args)
{
//Receive user input for JFrame width
String jframeWidth=JOptionPane.showInputDialog(null,"Put JFrame width in pixels here");

//Receive user input for JFrame height
String jframeHeight=JOptionPane.showInputDialog(null,"Put JFrame height in pixels here");

//Create a JFrame
JFrame frame=new JFrame("Set JFrame size");

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

//Set JFrame size base on user input
frame.setSize(Integer.parseInt(jframeWidth),Integer.parseInt(jframeHeight));

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


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

Set JFrame title

Source code below will show how to set JFrame title

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


import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class SetJFrameTitle
{
public static void main(String[]args)
{
//Create input dialog that will use to receive JFrame title
String jframeTitle=JOptionPane.showInputDialog(null,"Put JFrame title here");

//Create a JFrame
JFrame frame=new JFrame();

//Set title for JFrame
//Method setTitle() is use to set JFrame title
frame.setTitle(jframeTitle);

//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
******************************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You