Making Windows Assignment Help

Assignment Help: >> Controls and Events - Making Windows

Making Windows:

Although creating applets is the most common use for Java's AWT, that is possible to create stand alone AWT based applications, too. For do this simply creates an object of the window or windows you need inside main ( ). For instance, the following program creates a frame window which responds to mouse clicks and keystrokes.

/ / Create an AWT based aplication.

Import java.awt.*;

Import java.awt.event.*;

Import java.applet.*;

/ / Create a frame window.

public class Appwindow extends Frame  {

String Keymsg = * *;

String mousemsg = * * ;

int mouseX=30, mouseY=30;

public Appwindow ( ) {

addKeyListener (new MyKeyAdapter (this ) ;

addMouseListener (new MyMouseAdapter (this );

addWindowListener (new MywindowAdapter ( ) );

}

public void paint (Graphics g ) {

g.drawString(keymsg,10,40 );

g.drawString (mouseG, mouseX,mouseY);

}

/ / Create the window.

Public static void main  (String args [ ] ); {

AppWindow appwin = new Appwindow ( );

Appwin.setSize (new Dimension (300,200) );

Appwin.setTitle ("AnAWT-Based Appocation");

Appwin.setVisible (true );

}

}

class MyKeyAdapter extends KeyAdapter {

appWindowappWindow ;

public MyKeyAdapter (AppWindow appWindow ) {

this.appWindow = appWindow:

}

public void keyTyped (KeyEvent Ke) {

appWindow.keymsg + =key.get.KetChar ( );

appWindow.repaint ( );

};

}

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" + appWinxdow.mouseX +

"," + appWindow.mouseY;

appWindow.repaint ( );

}

}

class MyWindowsAdapter extends WindowAdapter {

public void windowClosing (WindowEvent we ) {

System.exit (0);

}

}

At one created a frame, window takes on a life of its own.  Notice that main() ends along with the call to appwin.setVisble (true).  Therefore, the program keeps running until you close the window.  Within essence, when creating a windowed application, you will use main( ) to launch it top level window. After that, your program will function as a GUI-based application not such as the console-based programs used earlier.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd