0% found this document useful (0 votes)
49 views5 pages

Lab 2

Uploaded by

busharshaqoor922
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views5 pages

Lab 2

Uploaded by

busharshaqoor922
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Visual Programming using Java Lab

Lab No. 2
GUI Components

Lab Subjects:
Java Example, which contains:
 Some of the swing package GUI components and some of it's built-in
methods:
A. JLabel.
B. JTextField.
C. JButton.

 Specify layout manager class from the awt package (FlowLayout,


BorderLayout, GridLayout) for the JFrame container.

 Event Handling with Nested Classes.


Code:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JOptionPane;

public class JavaApplication3 extends JFrame


{
private JLabel L;
private JTextField T;
private JButton B1, B2;

public JavaApplication3()
{
super("GUI Example");
setLayout( new FlowLayout(FlowLayout.CENTER) );

L = new JLabel( "Enter a Name:");


L.setToolTipText( "This is a label");
ImageIcon bug1= new ImageIcon("bug1.gif");
L.setIcon( bug1 );
L.setHorizontalTextPosition( SwingConstants.RIGHT );
L.setVerticalTextPosition( SwingConstants.CENTER );
add(L);
T = new JTextField (20);
add(T);

B1 = new JButton ("Display");


ImageIcon show= new ImageIcon("show.gif");
B1.setIcon(show);
add(B1);

B2 = new JButton ("Exit");


add(B2);

ButtonHandler handler = new ButtonHandler ();


B1.addActionListener ( handler );
B2.addActionListener ( handler );
}

private class ButtonHandler implements ActionListener


{
public void actionPerformed ( ActionEvent event )
{
if (event.getSource() == B1 )
JOptionPane.showMessageDialog ( null , "Name entered: " + T.getText() );
else if (event.getSource() == B2 )
System.exit(0);
//event.getActionCommand ()
}
}
public static void main( String args[] )
{
JavaApplication3 labelFrame = new JavaApplication3();
labelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
labelFrame.setSize( 900, 300 );
labelFrame.setLocation( 350, 200 );
labelFrame.setVisible( true );
}
}
Output:

Additional built-in methods:

 Change Font format:

Example:

Label L = new JLabel("TEXT");


Font F = new Font ("Arial", Font.BOLD, 20);
L.setFont (F);

Package:

import java.awt.Font;

 Change element dimensions (width, height):

Example:

Label L = new JLabel("TEXT");


L.setPreferredSize(new Dimension(500, 50));

Package:

import java.awt.Dimension;
 Change element font color:

Example:

Label L = new JLabel("TEXT");


L.setForeground(Color.GREEN);

Package:

import java.awt.Color;

 get password from JPasswordField element:

Example:

JPasswordField P = new JPasswordField (5);


String password = new String(P.getPassword());

You might also like