The document discusses how to create a windowed program in Java. It explains that while applets are commonly used with AWT, it is also possible to create standalone AWT applications by creating window instances like frames in the main method. An example program is provided that creates a frame window with key and mouse listeners to display input. The program demonstrates how to launch the GUI window from main rather than within an applet.
2. Objective
On completion of this period, you would be
able to know
• Creating a Windowed program
http://improvejava.blogspot.in/
2
3. Recap
In the previous class, you have leant
• The steps to create a Frame with in an Applet
• Relevant program
http://improvejava.blogspot.in/
3
4. Creating a Windowed Program
• Creating applets is the most common use for
Java’s AWT
• But, it is possible to create stand-alone AWT-
based applications
• To do this
– simply create an instance of the window or
windows you need inside main( )
– e.g. Create Frame window with in main()
http://improvejava.blogspot.in/ 4
5. Example Program
// Create an AWT-based application.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
// Create a frame window.
public class AppWindow extends Frame {
String keymsg = "This is a test.";
String mousemsg = "";
int mouseX=30, mouseY=30;
public AppWindow() {
addKeyListener(new MyKeyAdapter(this));
addMouseListener(new MyMouseAdapter(this));
addWindowListener(new MyWindowAdapter());
}
http://improvejava.blogspot.in/ 5
6. Example Program contd..
public void paint(Graphics g) {
g.drawString(keymsg, 10, 40);
g.drawString(mousemsg, mouseX, mouseY);
}
// Create the window.
public static void main(String args[]) {
AppWindow appwin = new AppWindow();
appwin.setSize(new Dimension(300, 200));
appwin.setTitle("An AWT-Based Application");
appwin.setVisible(true);
}
}
http://improvejava.blogspot.in/ 6
7. Example Program contd..
class MyKeyAdapter extends KeyAdapter {
AppWindow appWindow;
public MyKeyAdapter(AppWindow appWindow) {
this.appWindow = appWindow;
}
public void keyTyped(KeyEvent ke) {
appWindow.keymsg += ke.getKeyChar();
appWindow.repaint();
};
}
http://improvejava.blogspot.in/ 7
8. Example Program contd..
class MyMouseAdapter extends MouseAdapter {
AppWindow appWindow;
public MyMouseAdapter(AppWindow appWindow) {
this.appWindow = appWindow;
}
public void mousePressed(MouseEvent me) {
appWindow.mouseX = me.getX();
appWindow.mouseY = me.getY();
appWindow.mousemsg = "Mouse Down at " +
appWindow.mouseX +
", " + appWindow.mouseY;
appWindow.repaint();
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
} http://improvejava.blogspot.in/ 8
9. Example Program contd..
• Sample output from this program is shown here
Fig. 67.1 Output of AppWindow program
http://improvejava.blogspot.in/ 9
10. Example Program contd..
• When creating a windowed application, you will
use main( ) to launch its top-level window
• After that, your program will function as a GUI-
based application
– not like the console-based programs used
earlier
http://improvejava.blogspot.in/ 10
11. Summary
• In this class we have seen
– The creation of a windowed program
– The relevant program
http://improvejava.blogspot.in/ 11
12. Quiz
1. Can Window object be used as a standalone
GI component?
– Yes
– No
http://improvejava.blogspot.in/ 12