חזרה
שאלה- כתוב אפליקציה ב Java העונה על ההוראות
/*File SampProg133.java Copyright 1997, R.G.Baldwin
Without viewing the solution that follows, write a Java
application that replicates the behavior of the application
named Event31.java (discussed in lesson 106) with the
following changes.
This is an upgrade to the original version of Event31.
In particular, in its original form, there is no visual
indication when the user clicks on the Label object
identified by the Click Me caption. Since this Label object
is being used to simulate a Button object, a visual
indication that the Label object has been clicked would be
appropriate.
In your upgraded version, the white Label object which
originally had the Click Me caption will appear as a
magenta colored rectangle when the program starts and will
not display a caption.
When the user clicks the magenta Label object, the caption
"ouch" will momentarily appear and disappear in synchronism
with the down stroke and the up stroke of the left mouse
button. The caption will appear on the down stroke and
will disappear on the upstroke.
In addition to the above, make your name appear in the
banner at the top of the Frame object.
The upgraded version of the program has been tested using
JDK 1.1.3 under Win95.
*/
//=========================================================
import java.awt.*;
import java.awt.event.*;
//=========================================================
public class SampProg133 extends Frame{
public static void main(String[] args){
//instantiate obj
SampProg133 displayWindow = new SampProg133();
}//end main
//-------------------------------------------------------
public SampProg133(){//constructor
setTitle("Copyright 1997, R.G.Baldwin");
setLayout(new FlowLayout());
Button clickMeButton = new Button("Click Me");
Label colorMeLabel = new Label("Color Me");
Label clickMeLabel = new Label(" ");
clickMeLabel.setBackground(Color.magenta);
add(clickMeButton);//add components to the Frame object
add(colorMeLabel);
add(clickMeLabel);
setSize(300,100);//set frame size
setVisible(true);//display the frame
//Register listener objects
clickMeLabel.addMouseListener(
new MyMouseListener(clickMeButton,clickMeLabel));
clickMeButton.addActionListener(
new MyActionListener(colorMeLabel));
//terminate when Frame is closed
this.addWindowListener(new Terminate());
}//end constructor
}//end class SampProg133
//=========================================================
/*This MouseListener class is used to monitor for mouse
clicks on a Label object. Whenever the user clicks on the
label, the code in an object of this class creates a
counterfeit ActionEvent object and posts it to the
SystemEventQueue. The source of the event is specified to
be a particular Button object that is passed in when an
object of this class is instantiated. Thus, the Label
object "claims" to be the Button object and posts
ActionEvent objects that are interpreted by the runtime
system as originating at the Button object. The type of
ActionEvents generated are ACTION_PERFORMED events. The
events are automatically delivered to the actionPerformed()
method of an ActionListener object registered on the
button. */
class MyMouseListener extends MouseAdapter{
Button clickMeButton;//reference to the Button
Label clickMeLabel;//reference to the Label
//-------------------------------------------------------
//constructor
MyMouseListener(Button inButton,Label inLabel){
clickMeButton = inButton;//save reference to Button
clickMeLabel = inLabel;//save reference to Label
}//end constructor
//-------------------------------------------------------
//overridden mouseClicked() method
public void mouseClicked(MouseEvent e){
//Note that the following is a single statement
Toolkit.getDefaultToolkit().
getSystemEventQueue().
postEvent(new ActionEvent(clickMeButton,
ActionEvent.
ACTION_PERFORMED,
"counterfeit"));
}//end overridden mouseClicked() method
//------------------------------------------------------
//overridden mousePressed() method
public void mousePressed(MouseEvent e){
clickMeLabel.setText(" ouch ");
}//end overridden mousePressed() method
//------------------------------------------------------
//overridden mouseReleased() method
public void mouseReleased(MouseEvent e){
clickMeLabel.setText(" ");
}//end overridden mousePressed() method
}//end MyMouseListener
//=========================================================
/*This ActionListener class is used to instantiate a
Listener object for the Button object. Whenever the button
is clicked, or a counterfeit ActionEvent is posted with the
Button as the specified source object, the code in an
object of this class toggles the background color of a
Label object back and forth between yellow and blue.*/
class MyActionListener implements ActionListener{
int toggle = 0;
Label myLabel;
//-------------------------------------------------------
MyActionListener(Label inLabel){//constructor
myLabel = inLabel;
}//end constructor
//-------------------------------------------------------
public void actionPerformed(ActionEvent e){
if(toggle == 0){
toggle = 1;
myLabel.setBackground(Color.yellow);
}else{
toggle = 0;
myLabel.setBackground(Color.blue);
}//end else
}//end actionPerformed()
}//end class myActionListener
//=========================================================
class Terminate extends WindowAdapter{
public void windowClosing(WindowEvent e){
//terminate the program when the window is closed
System.exit(0);
}//end windowClosing
}//end class Terminate
//=========================================================