
October 5th, 2002, 07:09 PM
|
|
Junior Member
|
|
Join Date: Oct 2002
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
frames
2 more questions... ^^;
1) System.exit(0) ... it's great for shutting down apps. How would i do this to an app let though?
2)
can someone pleaes tell me if they can *RUN* the MyFirstFrame class? Everything in the code is 100% correct because i downloaded it off the developers website:
Code:
import java.awt.*;
import java.awt.event.*;
/**
* MyFirstFrame class
*
* <p>
* A sample frame to illustrate the placing of GUI objects and event handling.
*/
class MyFirstFrame extends Frame implements ActionListener
{
//----------------------------------
// Data Members
//----------------------------------
/**
* Default frame width
*/
private static final int FRAME_WIDTH = 300;
/**
* Default frame height
*/
private static final int FRAME_HEIGHT = 200;
/**
* X coordinate of the frame default origin point
*/
private static final int FRAME_X_ORIGIN = 150;
/**
* Y coordinate of the frame default origin point
*/
private static final int FRAME_Y_ORIGIN = 250;
/**
* Default width for buttons
*/
private static final int BUTTON_WIDTH = 60;
/**
* Default height for buttons
*/
private static final int BUTTON_HEIGHT = 30;
/**
* The GUI button for Cancel
*/
private Button cancelButton;
/**
* The GUI button for OK
*/
private Button okButton;
//----------------------------------
// Constructors
//----------------------------------
/**
* Default constructor
*/
public MyFirstFrame()
{
//set the frame properties
setSize (FRAME_WIDTH, FRAME_HEIGHT);
setResizable (false);
setTitle ("Program MyFirstFrame");
setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setLayout (null);
//create and place two buttons on the frame
okButton = new Button("OK");
okButton.setBounds(100, 150, BUTTON_WIDTH, BUTTON_HEIGHT);
add(okButton);
cancelButton = new Button("CANCEL");
cancelButton.setBounds(170, 150, BUTTON_WIDTH, BUTTON_HEIGHT);
add(cancelButton);
//register this frame as an action listener of the two buttons
cancelButton.addActionListener(this);
okButton.addActionListener(this);
//register a ProgramTerminator object as the window listener of this frame
addWindowListener(new ProgramTerminator());
}
//-------------------------------------------------
// Public Methods:
//
// void actionPerformed ( ActionEvent )
//
//------------------------------------------------
/**
* Standard method to respond the action event.
*
* @param event the ActionEvent object
*
*/
public void actionPerformed(ActionEvent event)
{
Button clickedButton = (Button) event.getSource();
if (clickedButton == cancelButton) {
setTitle("You clicked CANCEL");
}
else { //the event source must be okButton
setTitle("You clicked OK");
}
}
}
Code:
/*
ProgramTerminator Class
A ProgramTerminator object terminates the program when its main window
generates a window closing event.
*/
import java.awt.event.*;
class ProgramTerminator implements WindowListener
{
/******************************
Window Event Handling
*******************************/
public void windowClosing( WindowEvent event )
{
System.exit(0);
}
public void windowActivated (WindowEvent event) { }
public void windowClosed (WindowEvent event) { }
public void windowDeactivated (WindowEvent event) { }
public void windowDeiconified (WindowEvent event) { }
public void windowIconified (WindowEvent event) { }
public void windowOpened (WindowEvent event) { }
}
It basically comes down to me not understanding how to run frames 
Last edited by gaspacho soup : October 6th, 2002 at 01:12 AM.
|