avatarSarang S. Babu

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

7412

Abstract

yout</li></ul><p id="c536">The above are the classes that implement the LayoutManager interface which is defined in java.awt package.</p><h1 id="21c7">FlowLayout</h1><p id="4300">A flow layout arranges components in a directional flow, much like lines of text in a paragraph, until there is no more room, then onto the next line.</p><p id="ff09">By default, the components are centered. The programmer can specify the alignment using one of the following arguments with the constructor:</p><ul><li>FlowLayout.LEFT</li><li>FlowLayout.CENTER</li><li>FlowLayout.RIGHT</li></ul><h1 id="9fcf">BorderLayout</h1><p id="d66f">A <a href="https://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html">border layout</a> lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center.</p><p id="ec24">Each region may contain no more than one component and is defined by a corresponding constant: NORTH, SOUTH, EAAST, WEST, and CENTER.</p><p id="00ef">The programmer can specify the region using the following in the add method:</p><ul><li>add(“North”, b1);</li><li>add(“South”,b2);</li><li>add(“East”,b3);</li><li>add(“West”,b4);</li><li>add(“Center”,b5);</li></ul><p id="0ba4">OR</p><ul><li>add(BorderLayout.NORTH,b1);</li><li>add(BorderLayout.SOUTH,b2);</li><li>add(BorderLayout.EAST,b3);</li><li>add(BorderLayout.WEST,b4);</li><li>add(BorderLayout.CENTER,b5);</li></ul><p id="62eb"><b>Note1</b>: JVM raises IllegalArgumentException if the region name is not as shown.</p><p id="55de"><b>Note2</b>: Compilation fails if the constant name is not in upper case as shown.</p><p id="7e8d"><b>Note3</b>: Above can be combined also.</p><p id="ab5c"><b>JLabel</b></p><p id="8112">JLabel is a display area for a short text string or an image, or both. A label does not react to input events.</p><figure id="3003"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*lASEv7pI4WtuU_HmZmRtKQ.png"><figcaption></figcaption></figure><p id="38b7"><b>Program:</b></p><blockquote id="7ce7"><p>import java.awt.;</p></blockquote><blockquote id="8d15"><p>import javax.swing.;</p></blockquote><blockquote id="2dd1"><p>import java.awt.event.;</p></blockquote><blockquote id="93c6"><p>class S2 extends JFrame{</p></blockquote><blockquote id="3733"><p>Container c;</p></blockquote><blockquote id="74eb"><p>JTextField txt1;</p></blockquote><blockquote id="ef47"><p>JButton btn1;</p></blockquote><blockquote id="a7ca"><p>JLabel lbl1, lbl2;</p></blockquote><blockquote id="2c36"><p>S2(){</p></blockquote><blockquote id="1317"><p>c=getContentPane();</p></blockquote><blockquote id="e0ea"><p>c.setLayout(new FlowLayout());</p></blockquote><blockquote id="e606"><p>lbl1= new JLabel(“Enter a number”);</p></blockquote><blockquote id="69e6"><p>txt1= new JTextField(10);</p></blockquote><blockquote id="2ff0"><p>btn1= new JButton(“Answer”);</p></blockquote><blockquote id="c175"><p>lbl2= new JLabel();</p></blockquote><blockquote id="ed6d"><p>c.add(lbl1);</p></blockquote><blockquote id="1294"><p>c.add(txt1);</p></blockquote><blockquote id="4663"><p>c.add(btn1);</p></blockquote><blockquote id="645f"><p>c.add(lbl2);</p></blockquote><blockquote id="1912"><p>btn1.addActionListener(new L2());</p></blockquote><blockquote id="e9ff"><p>}</p></blockquote><blockquote id="450b"><p>public static void main(String args[]){</p></blockquote><blockquote id="8881"><p>S2 frm= new S2();</p></blockquote><blockquote id="e18f"><p>frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</p></blockquote><blockquote id="fc73"><p>frm.setSize(250,150);</p></blockquote><blockquote id="c0d9"><p>frm.setTitle(“Square Finder”);</p></blockquote><blockquote id="8c47"><p>frm.setVisible(true);</p></blockquote><blockquote id="d4a5"><p>}</p></blockquote><blockquote id="9b99"><p>class L2 implements ActionListener{</p></blockquote><blockquote id="d15e"><p>public void actionPerformed(ActionEvent ae){</p></blockquote><blockquote id="0d3c"><p>try{</p></blockquote><blockquote id="ac74"><p>int a= Integer.ParseInt(txt1.getText());</p></blockquote><blockquote id="f820"><p>float s= aa;</p></blockquote><blockquote id="49f6"><p>lbl2.setText(“Square=”+s);</p></blockquote><blockquote id="b2a5"><p>}</p></blockquote><blockquote id="4771"><p>catch(NumberFormatException e){</p></blockquote><blockquote id="2563"><p>JOptionPane.showMessageDialog(c,”Please enter integer”);</p></blockquote><blockquote id="50d6"><p>txt1.setText(“ “);</p></blockquote><blockquote id="22b9"><p>txt1.requestFocus();</p></blockquote><blockquote id="69fd"><p>}</p></blockquote><blockquote id="41a1"><p>}</p></blockquote><blockquote id="3b23"><p>}</p></blockquote><blockquote id="c844"><p>}</p></blockquote><h1 id="0aea">Handling Key Board Events</h1><figure id="2f6d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*LJMhGpVBvAPqHzpvD4UO6g.png"><figcaption>Handling key board events</figcaption></figure><p id="8466"><b>Program:</b></p><blockquote id="86df"><p>import java.awt.;</p></blockquote><blockquote id="7704"><p>import javax.swing.;</p></blockquote><blockquote id="e4e3"><p>import java.awt.event.;</p></blockquote><blockquote id="9c38"><p>class S7 extends JFrame{</p></blockquote><blockquote id="9d60"><p>Container c;</p></blockquote><blockquote id="878c"><p>JTextField txt1;</p></blockquote><blockquote id="72e2"><p>JButton btn1;</p></blockquote><blockquote id="b3ed"><p>JLabel lbl1,lbl2;</p></blockquote><blockquote id="0c64"><p>S7(){</p></blockquote><blockquote id="d9b9"><p>c=getContentPane();</p></blockquote><blockquote id="94aa"><p>c.setLayout(new FlowLayout());</p></blockquote><blockquote id="8e61"><p>lbl1= new JLabel(“Enter a number”);</p></blockquote><blockquote id="17f4"><p>txt1= new JTextField(10);</p></blockquote><blockquote id="dfb1"><p>btn1= new JButton(“Answer”);</p></blockquote><blockquote id="6d30"><p>lbl2= new JLabel();</p></blockquote><blockquote id="24ed"><p>c.add(lbl1);</p></blockquote><blockquote id="21f2"><p>c.add(txt1);</p></blockquote><blockquote id="cd0d"><p>c.add(btn1);</p></blockquote><blockquote id="b67b"><p>c.add(lbl2);</p></blockquote><blockquote id="25f7"><p>btn1.addActionListener(new L2());</p></blockquote><blockquote id="52ff"><p>btn1.addKeyListener(new L3());</p></blockquote><blockquote id="88a6"><p>}</p></blockquote><blockquote id="14b3"><p>public static void main(String args[]){</p></blockquote><blockquote id="6976"><p>S7 frm= new S7();</p></blockquote><blockquote id="62ac"><p>frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</p></blockquote><blockquote id="30b2"><p>frm.setSize(250,150);</p></blockquote><blockquote id="b26f"><p>frm.setTitle(“Square Finder”);</p></blockquote><blockquote id="f311"><p>frm.setVisible(true);</p></blockquote><blockquote id="76a6"><p>}</p></blockquote><blockquote id="d771"><p>class L3 implements KeyListener</p></blockquote><blockquote id="dc67"><p>{</p></blockquote><blockquote id="e85c"><p>public void keyPressed(KeyEvent ke)</p></blockquote><blockquote id="f62e"><p>{</p></blockquote><blockquote id="033d"><p>if(ke.getKeyCode()==KeyEvent.VK_ENTER);</p></blockquote><blockquote id="d07f"><p>try{</p></blockquote><blockquote id="9743"><p>int a= Integer.parseInt(txt1.getText());</p></blockquote><blockquote id="f128"><p>float s= aa;</p></blockquote><blockquote id="7f69"><p>lbl2.setText(‘Square=’+s’);</p></blockquote><blockquote id="e8a1"><p>}</p></blockquote><blockquote id="ffc9"><p>catch(NumberFormatExcepti

Options

on e){</p></blockquote><blockquote id="12b9"><p>JOptionPane.showMessageDialog(c,”Please enter integer”);</p></blockquote><blockquote id="434d"><p>txt1.setText(“”);</p></blockquote><blockquote id="8cb0"><p>txt1.requestFocus();</p></blockquote><blockquote id="650b"><p>lbl2.setText(“”);</p></blockquote><blockquote id="a5ef"><p>}</p></blockquote><blockquote id="0cf1"><p>}</p></blockquote><blockquote id="09c8"><p>}</p></blockquote><blockquote id="5a6e"><p>public void keyReleased(KeyEvent ke){}</p></blockquote><blockquote id="98b9"><p>public void keyTyped(KeyEvent ke){}</p></blockquote><blockquote id="23e9"><p>}</p></blockquote><blockquote id="c3b6"><p>class L2 implements ActionListener{</p></blockquote><blockquote id="3b1f"><p>public void actionPerformed(ActionEvent ae){</p></blockquote><blockquote id="9ab4"><p>try{</p></blockquote><blockquote id="2c15"><p>int a= Integer.parseInt(txt1.getText());</p></blockquote><blockquote id="4c49"><p>float s=aa;</p></blockquote><blockquote id="60b5"><p>lbl2.setText(“Square=”+s);</p></blockquote><blockquote id="4a5f"><p>}</p></blockquote><blockquote id="731a"><p>catch(NumberFormatExcepption e){</p></blockquote><blockquote id="e4af"><p>JOptionPane.showMessageDialog(c,”Please enter integer”);</p></blockquote><blockquote id="672f"><p>txt1.setText(“”);</p></blockquote><blockquote id="bfdc"><p>txt1.requestFocus();</p></blockquote><blockquote id="f5a3"><p>lbl2.setText(“”);</p></blockquote><blockquote id="ffcc"><p>}</p></blockquote><blockquote id="4313"><p>}</p></blockquote><blockquote id="041d"><p>}</p></blockquote><blockquote id="f72a"><p>}</p></blockquote><h1 id="1ebb">Handling Windows Events</h1><figure id="d102"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*NLjulIbn6cjbJTlScMudPQ.png"><figcaption>Handling Windows Events</figcaption></figure><p id="3862"><b>Program</b>:</p><blockquote id="38c8"><p>import java.awt.;</p></blockquote><blockquote id="cebf"><p>import javax.swing.;</p></blockquote><blockquote id="c65a"><p>import java.awt.event.;</p></blockquote><blockquote id="4e11"><p>class S8 extends JFrame{</p></blockquote><blockquote id="47bc"><p>Container c;</p></blockquote><blockquote id="dcc7"><p>JTextField txt1;</p></blockquote><blockquote id="456d"><p>JButton btn1;</p></blockquote><blockquote id="a068"><p>JLabel lbl1,lbl2;</p></blockquote><blockquote id="b2bf"><p>S8(){</p></blockquote><blockquote id="5224"><p>c=getContentPane();</p></blockquote><blockquote id="7933"><p>c.setLayout(new FlowLayout());</p></blockquote><blockquote id="4b28"><p>lbl1= new JLabel(“Enter a number”);</p></blockquote><blockquote id="0319"><p>txt1= new JTextField(10);</p></blockquote><blockquote id="abd0"><p>btn1= new JButton(“Answer”);</p></blockquote><blockquote id="d304"><p>lbl2= new JLabel();</p></blockquote><blockquote id="1c16"><p>c.add(lbl1);</p></blockquote><blockquote id="ef01"><p>c.add(txt1);</p></blockquote><blockquote id="cabf"><p>c.add(btn1);</p></blockquote><blockquote id="cc74"><p>c.add(lbl2);</p></blockquote><blockquote id="aeb0"><p>btn1.addActionListener(new L2());</p></blockquote><blockquote id="f132"><p>btn1.addKeyLisener(new L3());</p></blockquote><blockquote id="8db6"><p>this.addWindowListener(new L4());</p></blockquote><blockquote id="2a70"><p>}</p></blockquote><blockquote id="862c"><p>public static void main(String args[]){</p></blockquote><blockquote id="54e1"><p>S8 frm= new S8();</p></blockquote><blockquote id="d3c7"><p>frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);</p></blockquote><blockquote id="6bd4"><p>frm.setSize(250,150);</p></blockquote><blockquote id="25fb"><p>frm.setTitle(“Square Finder”);</p></blockquote><blockquote id="d8ad"><p>frm.setVisible(true);</p></blockquote><blockquote id="73ce"><p>}</p></blockquote><blockquote id="7583"><p>class L4 implements WindowListener{</p></blockquote><blockquote id="c0b5"><p>public void windowActivated(WindowEvent e){}</p></blockquote><blockquote id="98e6"><p>public void windowDeactivated(WindowEvent e){}</p></blockquote><blockquote id="2f46"><p>public void windowOpened(WindowEvent e){</p></blockquote><blockquote id="2879"><p>JOptionPane.showMessageDialog(c,”Welcome to Square Calculator”);</p></blockquote><blockquote id="c1b1"><p>}</p></blockquote><blockquote id="38aa"><p>public void windowClosed(WindowEvent e){}</p></blockquote><blockquote id="7958"><p>public void windowClosing(WindowEvent e){</p></blockquote><blockquote id="1204"><p>int output= JOptionPane.showConfirmDialog(c,”Do you want to exit”);</p></blockquote><blockquote id="33a0"><p>if(output==JOptionPane.YES_OPTION){</p></blockquote><blockquote id="c894"><p>System.exit(1);</p></blockquote><blockquote id="bfda"><p>}</p></blockquote><blockquote id="6bb0"><p>}</p></blockquote><blockquote id="08d0"><p>public void windowIconified(WindowEvent e){}</p></blockquote><blockquote id="27e1"><p>public void windowDeiconified(WindowEvent e){}</p></blockquote><blockquote id="e582"><p>}</p></blockquote><blockquote id="6a6e"><p>class L2 implements ActionListener{</p></blockquote><blockquote id="e0f4"><p>public void actionPerformed(ActionEvent ae){</p></blockquote><blockquote id="35fc"><p>try{</p></blockquote><blockquote id="4711"><p>int a=Integer.parseInt(txt1.getText());</p></blockquote><blockquote id="ccd5"><p>float s=aa;</p></blockquote><blockquote id="293e"><p>lbl2.setText(“Square=”+s);</p></blockquote><blockquote id="754a"><p>}</p></blockquote><blockquote id="7889"><p>catch(NumberFormatException e){</p></blockquote><blockquote id="9aec"><p>JOptionPane.showMessageDialog(c,”Please enter integer”);</p></blockquote><blockquote id="c57f"><p>txt1.setText(“”);</p></blockquote><blockquote id="4621"><p>txt1.requestFocus();</p></blockquote><blockquote id="d13a"><p>lbl2.setText(“”);</p></blockquote><blockquote id="83d1"><p>}</p></blockquote><blockquote id="2088"><p>}</p></blockquote><blockquote id="5fa1"><p>}</p></blockquote><blockquote id="1781"><p>class L3 implements KeyListener{</p></blockquote><blockquote id="9be3"><p>public void keyPressed(KeyEvent ke){</p></blockquote><blockquote id="4e74"><p>if(ke.getKeyCode()==KeyEvent.VK_ENTER){</p></blockquote><blockquote id="dab9"><p>try{</p></blockquote><blockquote id="7373"><p>int a= Integer.parseInt(txt1.getText());</p></blockquote><blockquote id="02fa"><p>float s=aa;</p></blockquote><blockquote id="dc3e"><p>lbl2.setText(“Square=”+s);</p></blockquote><blockquote id="bea0"><p>}</p></blockquote><blockquote id="449d"><p>catch(NumberFormatException e){</p></blockquote><blockquote id="57a4"><p>JOptionPane.showMessageDialog(c,”Please enter integer”);</p></blockquote><blockquote id="7266"><p>txt1.setText(“”);</p></blockquote><blockquote id="f059"><p>txt1.requestFocus();</p></blockquote><blockquote id="d678"><p>lbl2.setText(“”);</p></blockquote><blockquote id="de83"><p>}</p></blockquote><blockquote id="996c"><p>}</p></blockquote><blockquote id="aa0c"><p>}</p></blockquote><blockquote id="948c"><p>public void keyReleased(KeyEvent ke){}</p></blockquote><blockquote id="b3f3"><p>public void keyTyped(KeyEvent ke){}</p></blockquote><blockquote id="d602"><p>}</p></blockquote><blockquote id="ddca"><p>}</p></blockquote><h1 id="2855">Conclusion</h1><p id="14ff">Swing is developed in pure java and is widely used since it has the same look and feel on all the platforms and offers a pluggable look and feel. I hope you liked this article, do let me know if there is any feedback. :) ❤️</p></article></body>

How to Create a GUI app in Java

Overview

In java, swings are used for building a GUI application. It has different features and is popularly used for building GUI applications in java. In this article, we will be covering all about the swing library in Java.

Java Swing

Swing is developed in pure java. The swing components can have the same look and feel on all the platforms and offer a pluggable look and feel. The swing components are light-weighted.

Example:

A java program to display a creative and display a frame

import javax.swing.*;

class F1 extends JFrame{

F1() {

setSize(200,200);

setTitle(“GUI DEMO”);

setLocation(100,200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public static void main(String args[]){

F1 f= new F1();

}

}

Output:

javac F1.java

java F1

Container hierarchy

Top-level container:

  • Place for other swing components to paint themselves
  • Eg. JFrame, JDialog, JApplet

Intermediate container:

  • Simplify positioning of atomic components
  • Eg, JPanel, JSplitPane, JTabbedPane

Atomic components:

  • Self-sufficient components that present information to and get input from the user
  • Eg, JButton, JLabel, JComboBox, JTextField, JTable

JButton

  • JButton is an implementation of a “push” button
  • Buttons can be configured and to some degree controlled by actions

JOptionPane

  • JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something.
  • While the class may appear complex because of the large number of methods, almost all uses of this class are one-line calls to one of the static methods shown below:

Nested classes

Like a class can have variables and methods as members, the class can also contain another class which is called a Nested class.

The concept of inner classes was introduced in java 1.1

There are several benefits of using inner classes:

  • Making your code more readable
  • Allowing for utility classes to be encapsulated within the class using it
  • Simplifying the process of writing a class, thereby actually encouraging developers to be more object-oriented.

A common use of inner classes is for event handlers. An event handler is the type of object that often needs access to the members of its outer class but is likely won't be refused y another class, making it a good candidate for an inner class.

The types of inner class are as follows:

  • The member inner class
  • Static inner class
  • The local inner class
  • The anonymous class

Below is the table that explains the concept much easier:

Event handling using member inner class:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class M1 extends JFrame

{

Container c;

JButton b1, b2;

class L1 implements ActionListener

{

public void actionPerformed(ActionEvent ae){

if(ae.getSource()==b1)

{

JOptionPane.showMessageDialog(c, “You have clicked button 1”);

}

if(ae.getSource()==b2)

{

JOptionPane.showMessageDialog(c,”You have clicked button 2”);

}

}

}

M1()

{

c=getContentPane();

c.setLayout(new FlowLayout());

b1=new JButton(“Click 1”);

b2= new JButton(“Click 2”);

c.add(b1);

c.add(b2);

L1 a= new L1();

b1.addActionListener(a);

b2.addActionListener(a);

}

public static void main(String args[]){

M1 f= new M1();

f.setSize(200,200);

f.setTitle(“Button Click”);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

Layout managers

Java’s layout manager determines how components will be arranged when they are added to the frame. AWT package includes the following layout classes:

  • FlowLayout
  • BorderLayout
  • GridLayout

The above are the classes that implement the LayoutManager interface which is defined in java.awt package.

FlowLayout

A flow layout arranges components in a directional flow, much like lines of text in a paragraph, until there is no more room, then onto the next line.

By default, the components are centered. The programmer can specify the alignment using one of the following arguments with the constructor:

  • FlowLayout.LEFT
  • FlowLayout.CENTER
  • FlowLayout.RIGHT

BorderLayout

A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center.

Each region may contain no more than one component and is defined by a corresponding constant: NORTH, SOUTH, EAAST, WEST, and CENTER.

The programmer can specify the region using the following in the add method:

  • add(“North”, b1);
  • add(“South”,b2);
  • add(“East”,b3);
  • add(“West”,b4);
  • add(“Center”,b5);

OR

  • add(BorderLayout.NORTH,b1);
  • add(BorderLayout.SOUTH,b2);
  • add(BorderLayout.EAST,b3);
  • add(BorderLayout.WEST,b4);
  • add(BorderLayout.CENTER,b5);

Note1: JVM raises IllegalArgumentException if the region name is not as shown.

Note2: Compilation fails if the constant name is not in upper case as shown.

Note3: Above can be combined also.

JLabel

JLabel is a display area for a short text string or an image, or both. A label does not react to input events.

Program:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class S2 extends JFrame{

Container c;

JTextField txt1;

JButton btn1;

JLabel lbl1, lbl2;

S2(){

c=getContentPane();

c.setLayout(new FlowLayout());

lbl1= new JLabel(“Enter a number”);

txt1= new JTextField(10);

btn1= new JButton(“Answer”);

lbl2= new JLabel();

c.add(lbl1);

c.add(txt1);

c.add(btn1);

c.add(lbl2);

btn1.addActionListener(new L2());

}

public static void main(String args[]){

S2 frm= new S2();

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frm.setSize(250,150);

frm.setTitle(“Square Finder”);

frm.setVisible(true);

}

class L2 implements ActionListener{

public void actionPerformed(ActionEvent ae){

try{

int a= Integer.ParseInt(txt1.getText());

float s= a*a;

lbl2.setText(“Square=”+s);

}

catch(NumberFormatException e){

JOptionPane.showMessageDialog(c,”Please enter integer”);

txt1.setText(“ “);

txt1.requestFocus();

}

}

}

}

Handling Key Board Events

Handling key board events

Program:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class S7 extends JFrame{

Container c;

JTextField txt1;

JButton btn1;

JLabel lbl1,lbl2;

S7(){

c=getContentPane();

c.setLayout(new FlowLayout());

lbl1= new JLabel(“Enter a number”);

txt1= new JTextField(10);

btn1= new JButton(“Answer”);

lbl2= new JLabel();

c.add(lbl1);

c.add(txt1);

c.add(btn1);

c.add(lbl2);

btn1.addActionListener(new L2());

btn1.addKeyListener(new L3());

}

public static void main(String args[]){

S7 frm= new S7();

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frm.setSize(250,150);

frm.setTitle(“Square Finder”);

frm.setVisible(true);

}

class L3 implements KeyListener

{

public void keyPressed(KeyEvent ke)

{

if(ke.getKeyCode()==KeyEvent.VK_ENTER);

try{

int a= Integer.parseInt(txt1.getText());

float s= a*a;

lbl2.setText(‘Square=’+s’);

}

catch(NumberFormatException e){

JOptionPane.showMessageDialog(c,”Please enter integer”);

txt1.setText(“”);

txt1.requestFocus();

lbl2.setText(“”);

}

}

}

public void keyReleased(KeyEvent ke){}

public void keyTyped(KeyEvent ke){}

}

class L2 implements ActionListener{

public void actionPerformed(ActionEvent ae){

try{

int a= Integer.parseInt(txt1.getText());

float s=a*a;

lbl2.setText(“Square=”+s);

}

catch(NumberFormatExcepption e){

JOptionPane.showMessageDialog(c,”Please enter integer”);

txt1.setText(“”);

txt1.requestFocus();

lbl2.setText(“”);

}

}

}

}

Handling Windows Events

Handling Windows Events

Program:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class S8 extends JFrame{

Container c;

JTextField txt1;

JButton btn1;

JLabel lbl1,lbl2;

S8(){

c=getContentPane();

c.setLayout(new FlowLayout());

lbl1= new JLabel(“Enter a number”);

txt1= new JTextField(10);

btn1= new JButton(“Answer”);

lbl2= new JLabel();

c.add(lbl1);

c.add(txt1);

c.add(btn1);

c.add(lbl2);

btn1.addActionListener(new L2());

btn1.addKeyLisener(new L3());

this.addWindowListener(new L4());

}

public static void main(String args[]){

S8 frm= new S8();

frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

frm.setSize(250,150);

frm.setTitle(“Square Finder”);

frm.setVisible(true);

}

class L4 implements WindowListener{

public void windowActivated(WindowEvent e){}

public void windowDeactivated(WindowEvent e){}

public void windowOpened(WindowEvent e){

JOptionPane.showMessageDialog(c,”Welcome to Square Calculator”);

}

public void windowClosed(WindowEvent e){}

public void windowClosing(WindowEvent e){

int output= JOptionPane.showConfirmDialog(c,”Do you want to exit”);

if(output==JOptionPane.YES_OPTION){

System.exit(1);

}

}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

}

class L2 implements ActionListener{

public void actionPerformed(ActionEvent ae){

try{

int a=Integer.parseInt(txt1.getText());

float s=a*a;

lbl2.setText(“Square=”+s);

}

catch(NumberFormatException e){

JOptionPane.showMessageDialog(c,”Please enter integer”);

txt1.setText(“”);

txt1.requestFocus();

lbl2.setText(“”);

}

}

}

class L3 implements KeyListener{

public void keyPressed(KeyEvent ke){

if(ke.getKeyCode()==KeyEvent.VK_ENTER){

try{

int a= Integer.parseInt(txt1.getText());

float s=a*a;

lbl2.setText(“Square=”+s);

}

catch(NumberFormatException e){

JOptionPane.showMessageDialog(c,”Please enter integer”);

txt1.setText(“”);

txt1.requestFocus();

lbl2.setText(“”);

}

}

}

public void keyReleased(KeyEvent ke){}

public void keyTyped(KeyEvent ke){}

}

}

Conclusion

Swing is developed in pure java and is widely used since it has the same look and feel on all the platforms and offers a pluggable look and feel. I hope you liked this article, do let me know if there is any feedback. :) ❤️

Java
Programming
Coding
GUI
Java Swing
Recommended from ReadMedium