doc - Dr. Aiman Hanna

mammetlizardlickSoftware and s/w Development

Nov 3, 2013 (3 years and 9 months ago)

62 views

// *******************************************************************

// Swing2.java By:
Aiman

Hanna

(C) 1993
-

2013

// This program provides further information on Swing. In particular, the

// the program provides information about the default close but
ton of JFrame.

// That is the X button, which is often located at the top right corner of a

// frame/window,

which is located

beside
the M
aximize and
M
inimize buttons.

// The program also illustrates how multiple windows can be opened and

// manipulat
ed by a single program.

//

//

// Key Points:

// 1) JFrame Close (X) Button.

// *******************************************************************



import

java.awt.event.*;

import

java.awt.*;

import

javax.swing.*;




class

ExitListener
implements

Acti
onListener

{


public

void

actionPerformed(ActionEvent e)


{



System.
exit
(0);


}

}



class

DisplayListener
implements

ActionListener

{


public

void

actionPerformed(ActionEvent e)


{



System.
out
.println(
"Want to Terminate Program? Click the X button on "

+






"this window or the Exit button the other window."
);


}

}





public

class

Swing2{




public

static

final

int

BUTTON_WIDTH

= 300;


public

static

final

int

BUTTON_HIGHT

= 200;




public

static

void

main(String[] args)


{




JFrame window1 =

new

JFrame(
"Welcome to Swing"
);



JFrame window2 =
new

JFrame(
"Welcome Again"
);



window1.setSize(
BUTTON_WIDTH
,
BUTTON_HIGHT
);



window2.setSize(
BUTTON_WIDTH
,
BUTTON_HIGHT
);









// Set the default close button of the first window to only hide



// th
e window. Program will not terminate when this button is clicked



// JFrame.HIDE_ON_CLOSE would hide the current window.



// If instead issue JFrame.DISPOSE_ON_CLOSE, this would hide and dispose



// that window.



window1.setDefaultCloseOperation(JFra
me.
HIDE_ON_CLOSE
);









// Set the default close to terminate the entire program



window2.setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE
);









JButton exitButton1 =
new

JButton(
"Exit Program."
);



exitButton1.setBackground(Color.
orange
);








JBut
ton displayButton1 =
new

JButton(
"Click Here for Info."
);



displayButton1.setBackground(Color.
yellow
);









// Now create the listeners, which will actually handle the events



ExitListener exitLsnr =
new

ExitListener();



DisplayListener disLsnr =
new

DisplayListener();






// Relate the buttons to these listeners



exitButton1.addActionListener(exitLsnr);



displayButton1.addActionListener(disLsnr);









// Add the buttons to the frames



window1.add(exitButton1);



window2.add(displayButton1);






// Finally, show the frames after everything is ready



window1.setVisible(
true
);



window2.setVisible(
true
);


}

}



/* The Output
-

You need to run the program to see its behavior



*/