סקירה
ש- כתוב אפליקצית Java אשר מציגה במקור אובייקט מסגרת המכיל אובייקט כפתור בחלקו העליון ואובייקט שדה טקסט בתחתית. וגרום לכך שלשדה טקסט יהיו אותיות אדומות על גבי רקע צהוב.
כאשר תלחץ על אובייקט שדה הטקסט, הוא נעלם. כאשר תלחץ על אובייקט הכפתור, אובייקט שדה הטקסט חוזר.
השתמש רק באירועים מרמה נמוכה.
כאשר תלחץ על כפתור הסגירה בפינה הימנית עליונה של אובייקט המסגרת, התוכנית מסתיימת והשליטה מוחזרת כראוי למערכת ההפעלה.
ת- ראה תוכנית להלן.
/*From lesson 84 Copyright 1997, R.G.Baldwin Without viewing the following solution, write a Java application that originally displays a Frame object containing a button at the top and a TextField object at the bottom. Cause the TextField to have red letters on a yellow background. When you click on the TextField object, it disappears. When you click on the Button object, the TextField object reappears. Use only low level events. When you click on the close button in the upper right-hand corner of the Frame object, the program terminates and control is properly returned to the operating system. //========================================================= */ import java.awt.*; import java.awt.event.*; public class SampProg200 { public static void main(String[] args){ GUI gui = new GUI(); }//end main }//end class SampProg200 //========================================================= class GUI { public GUI(){//constructor //Create a visual TextField object TextField myTextField = new TextField("Initial String"); myTextField.setName("TextField1"); myTextField.setBackground(Color.yellow); myTextField.setForeground(Color.red); //Create a visual Button object Button myButton = new Button("Click me"); myButton.setName("Button1"); //Create a visual Frame object Frame myFrame = new Frame(); myFrame.setSize(300,100); myFrame.setTitle("Copyright 1997, R.G.Baldwin"); //Add the Button and the TextField to the Frame object myFrame.add("North",myButton); myFrame.add("South",myTextField); myFrame.setVisible(true); //Instantiate and register a MouseListener object which // will process mouse events on the Button object, and // the TextField object. MouseProc mouseProcCmd = new MouseProc( myButton,myTextField); myTextField.addMouseListener(mouseProcCmd); myButton.addMouseListener(mouseProcCmd); //Instantiate and register a Listener object which will // terminate the program when the user closes the // Frame object myFrame.addWindowListener(new WProc1()); }//end constructor }//end class GUI definition //========================================================= //Low-level event monitor. // This listener class monitors for low-level mousePressed() // events. Whenever a mousePressed() event occurs, the // event handler determines which object was the source of // the event and takes the appropriate action. class MouseProc extends MouseAdapter{ Button refToButton = null; TextField refToTextField = null; String refToButtonName = null; String refToTextFieldName = null; public MouseProc(//constructor Button inRefToButton, TextField inRefToTextField){ refToButton = inRefToButton; refToTextField = inRefToTextField; refToButtonName = inRefToButton.getName(); refToTextFieldName = inRefToTextField.getName(); }//end constructor public void mousePressed(MouseEvent e){ if(e.getComponent().getName().compareTo( refToTextFieldName) == 0) refToTextField.setVisible(false); if(e.getComponent().getName().compareTo( refToButtonName) == 0) refToTextField.setVisible(true); }//end mousePressed() }//end class MouseProc //======================================================== //The following listener class is used to terminate the // program when the user closes the Frame object. class WProc1 extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); }//end windowClosing() }//end class WProc1 //======================================================== |
ש- כתוב אפליקצית
Java אשר מציגה במקור אובייקט מסגרת המכיל אובייקט כפתור בחלקו העליון ואובייקט שדה טקסט בתחתית. גרום לכך שלשדה הטקסט יהיו אותיות אדומות על גבי רקע צהוב.
כאשר תלחץ על אובייקט שדה הטקסט הוא יעלם. כאשר תלחץ על אובייקט הכפתור, שדה הטקסט חוזר.
השתמש בתערובת של אירועים ברמה נמוכה ואירועים סמנטיים.
כאשר תלחץ על כפתור הסגירה בפינה הימנית עליונה של אובייקט המסגרת, התוכנית מסתיימת והשליטה מוחזרת כראוי למערכת ההפעלה.
ת- ראה תוכנית שלהלן.
/*From lesson 84
Copyright 1997, R.G.Baldwin
*/
import java.awt.*;
import java.awt.event.*;
public class SampProg201 {
public static void main(String[] args){
GUI gui = new GUI();
}//end main
}//end class SampProg201
//=========================================================
class GUI {
public GUI(){//constructor
//Create a visual TextField object
TextField myTxtField = new TextField("Initial String");
myTxtField.setBackground(Color.yellow);
myTxtField.setForeground(Color.red);
//Create a visual Button object
Button myButton = new Button("Click me");
//Create a visual Frame object
Frame myFrame = new Frame();
myFrame.setSize(300,100);
myFrame.setTitle("Copyright 1997, R.G.Baldwin");
//Add the Button and the TextField to the Frame object
myFrame.add("North",myButton);
myFrame.add("South",myTxtField);
myFrame.setVisible(true);
//Instantiate and register a MouseListener object which
// will process mouse events on the TextField object.
myTxtField.addMouseListener(new MouseProc(myTxtField));
//Instantiate and register an ActionListener object
// which will process action events on the Button
// object.
myButton.addActionListener(
new MyActionProcessor(myTxtField));
//Instantiate and register a Listener object which will
// terminate the program when the user closes the
// Frame object
myFrame.addWindowListener(new WProc1());
}//end constructor
}//end class GUI definition
//=========================================================
//Low-level event monitor.
// This listener class monitors for low-level
// mousePressed() events.
class MouseProc extends MouseAdapter{
TextField refToTextField = null;
public MouseProc(TextField inRefToTextField){
refToTextField = inRefToTextField;
}//end constructor
public void mousePressed(MouseEvent e){
refToTextField.setVisible(false);
}//end mousePressed()
}//end class MouseProc
//=========================================================
//Semantic event monitor.
// This listener class monitors for semantic action events.
class MyActionProcessor implements ActionListener{
TextField refToTextField = null;
MyActionProcessor(TextField inRefToTextField){//construct
refToTextField = inRefToTextField;
}//end constructor
public void actionPerformed(ActionEvent e){
refToTextField.setVisible(true);
}//end overridden actionPerformed method
}//end class MyActionProcessor
//=========================================================
//The following listener class is used to terminate the
// program when the user closes the Frame object.
class WProc1 extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}//end windowClosing()
}//end class WProc1
//=========================================================