/* 6. Program to create a window when we press M or m the window displays Good
Morning, A or a the window displays Good After Noon E or e the window
displays Good Evening, N or n the window displays Good Night */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class programB6 implements KeyListener,ActionListener
{
static JFrame frame;
static JFrame frame2;
static JTextArea outputL;
static JLabel output2;
static JTextField output;
static JTextField input;
//Driver function
public static void main(String args[])
{
//Create a frame
frame=new JFrame("Keyboard Event");
frame2=new JFrame("KEY PRESSED");
frame.setBackground(Color.white);
frame.setSize(500,500);
frame.setLayout(null);
//Create a text field for output
outputL=new JTextArea();
outputL.setText(" Type : \n1. M-m to Say Good Morning \n2. A-a to Say Good afternoon \n3. E-e to Say Good Evening ");
outputL.setBounds(0,50,500,100);
outputL.setLineWrap(true);
frame.add(outputL);
//Create a text field for input
input=new JTextField();
input.setBounds(0,400,500,50);
frame.add(input);
//Create an exit button
JButton exit=new JButton("Exit");
exit.setBounds(220,200,60,30);
frame.add(exit);
//Create an object of the class
programB6 obj=new programB6();
//Associate KeyListener with input
input.addKeyListener(obj);
//Associate ActionListener with exit
exit.addActionListener(obj);
frame.setVisible(true);
output2=new JLabel("Good Afternoon");
frame2.add(output2);
}
//function to dispose the frame when exit button is clicked
@Override
public void actionPerformed(ActionEvent e)
{
frame.dispose();
frame2.dispose();
}
/*function to display the unicode of key released
and the character if it is a letter or a digit*/
@Override
public void keyReleased(KeyEvent e)
{
}
public void createwindow(char value){
if(value=='A'||value=='a')
{
frame2.setBackground(Color.white);
frame2.setSize(300,300);
frame2.setLayout(null);
output2.setText("Good Afternoon");
output2.setBounds(0,0,200,50);
frame2.setVisible(true);
}
if(value=='M'||value=='m')
{
frame2.setBackground(Color.white);
frame2.setSize(300,300);
frame2.setLayout(null);
output2.setText("Good Morning");
output2.setBounds(0,0,200,50);
frame2.invalidate();
frame2.setVisible(true);
}
if(value=='E'||value=='e')
{
frame2.setBackground(Color.white);
frame2.setSize(300,300);
frame2.setLayout(null);
output2.setText("Good Evening");
output2.setBounds(0,0,200,50);
frame2.invalidate();
frame2.setVisible(true);
}
}
@Override
public void keyPressed(KeyEvent e)
{
if(Character.isLetter(e.getKeyChar()))
{
createwindow(e.getKeyChar());
keyTyped(e);
}
if(Character.isDigit(e.getKeyChar()))
keyTyped(e);
}
//function to display the character of the key typed
@Override
public void keyTyped(KeyEvent e)
{
}
}