Table of Contents
hide
Example : An AWT GUI program in Java to close the AWT window.
import java.awt.*;
import java.awt.event.*;
class CloseWindow
{
public static void main(String[] args)
{
Frame fr = new Frame("AWT Window Close Operation");
Label lbl = new Label("Welcome U All in Codershelpline",Label.CENTER);
fr.add(lbl);
fr.setSize(400,400);
fr.setVisible(true);
fr.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
----------- OR -----------
import java.awt.*;
import java.awt.event.*;
class AWTFrame extends Frame
{
Button b1;
AWTFrame()
{
setTitle("How to Close an AWT Window");
setSize(400,400);
setLayout(new FlowLayout());
setVisible(true);
b1=new Button("Submit"); //Optional Button Creation
add(b1);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
new AWTFrame();
}
}
Example : An AWT GUI program in Java to show the mouse click location(coordinates point) value from the frame.
import java.awt.*;
import java.awt.event.*;
class ClickLocation
{
public static void main (String[] args)
{
Frame frm = new Frame("Mouse Event Listener Program");
frm.setSize(500,500); //Size of Frame.
frm.setVisible(true); // To make Frame visible.
frm.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.println("Mouse clicked at location : ("+e.getX()+","+e.getY()+")");
}
}
);
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
Example : A simple AWT GUI program in Java to display a message on loading the frame.
import java.awt.*;
public class SimpleGUIExample
{
public static void main(String[] args)
{
// Create a Frame (a window) with a title
Frame frame = new Frame("Simple GUI Example");
// Create a Label component
Label label = new Label("Hello, World!");
// Add the Label to the Frame
frame.add(label);
// Set the size of the Frame
frame.setSize(300, 200);
// Set the layout manager for the Frame
frame.setLayout(new FlowLayout());
// Make the Frame visible
frame.setVisible(true);
}
}
----------- OR -----------
import java.awt.*;
public class AWTExample
{
public static void main(String[] args)
{
// Create a frame
Frame frame = new Frame("AWT Example");
// Create a label for message
Label label = new Label("Hello, World!");
// Create a button
Button button = new Button("Click Me to show message");
// Create a panel
Panel panel = new Panel();
panel.add(label);
panel.add(button);
// Add the panel to the frame
frame.add(panel);
// Set the size of the frame
frame.setSize(300, 200);
// Make the frame visible
frame.setVisible(true);
}
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class ButtonEffect implements ActionListener
{
Button btn;
TextField tf;
Frame fr;
ButtonEffect(String s)
{
fr = new Frame(s);
btn = new Button("OK");
tf = new TextField(12);
fr.setSize(400, 350);
fr.setVisible(true);
btn.addActionListener(this);
fr.add(tf);
fr.add(btn);
fr.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
fr.setLayout(new FlowLayout());
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btn)
{
tf.setText("Ok button is Pressed");
}
}
public static void main(String args[])
{
new ButtonEffect("Button Action Performed");
}
}
----------- OR -----------
import java.awt.*;
import java.awt.event.*;
public class AWTExample
{
public static void main(String[] args)
{
// Create a Frame (window) object with title
Frame frame = new Frame("AWT Program Example");
// Create a Label component
Label label = new Label("AWT Label");
// Create a Button component
Button button = new Button("Click Me");
// Add the Label and Button components to the created Frame
frame.add(label);
frame.add(button);
// Set the FlowLayout manager for the Frame that arranges the components in a horizontal flow
frame.setLayout(new FlowLayout());
// Set a message for label after an event listener worked for the Button
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
label.setText("Button clicked successfully");
}
});
// Set the size and make the Frame visible
frame.setSize(300, 200);
frame.setVisible(true);
}
}
NB: To handle button clicks, we add an ActionListener to the button using the addActionListener() method. When the button is clicked, the actionPerformed() method is invoked, and we update the label's text.
----------- OR -----------
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AWTExample
{
public static void main(String[] args)
{
// Create a frame (window) object with title message
Frame frame = new Frame("AWT Simple Input Example");
// Create a label
Label label = new Label("Enter your name in the text box :");
// Create a text field
TextField textField = new TextField();
// Create a button
Button button = new Button("Submit");
// Add an action listener to the button
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String name = textField.getText();
System.out.println("YOu entered the name = " + name + ".");
}
});
// Set the layout manager for the frame
frame.setLayout(new FlowLayout());
// Add the components to the frame
frame.add(label);
frame.add(textField);
frame.add(button);
// Set the size of the frame
frame.setSize(300, 200);
// Make the frame visible
frame.setVisible(true);
}
}
0 Comments