רישום תוכנית
לשם יחידה זו המכילה פירוט שלם של התכנית עם הערות נוספות רבות הקש כאן. התייחס ליחידות קודמות לשם תיאור תפעולי של התכנית.
/* File LWButton.java Copyright 1997, R.G.Baldwin This program is designed to be compiled and run under JDK 1.1. The purpose of this program is to illustrate the creation and use of lightweight components.
This program creates a Frame object containing two LWButton objects and one standard Button object. The background of the Frame object is yellow. The transparent nature of the LWButton objects is apparent because the yellow background shows through them. The background does not show through the opaque standard button. Various outputs are produced on the screen as the buttons are manipulated using the mouse. A typical output might be as follows: Mouse entered for illustration only Mouse exited for illustration only Mouse entered for illustration only java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=LW Button A] on LWButton[,53,28,106x106] Mouse exited for illustration only Mouse entered for illustration only Mouse exited for illustration only java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=Std Button] on button0 Mouse entered for illustration only java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=LW Button B] on LWButton[,239,28,107x107] Mouse exited for illustration only When one of the LWButtons is pressed, the color of the button becomes darker to provide visual feedback to the user. When the LWButton is pressed and then the mouse button is released, an ActionEvent object is dispatched to all ActionListener objects. This program was tested using JDK 1.1 under Win95. */ //======================================================================= import java.awt.*; import java.awt.event.*; //========================================================================== public class Lightweight02 { public static void main(String[] args){ GUI gui = new GUI();//instantiate a Graphical User Interface object }//end main }//end class Container03 //========================================================================= class GUI{ public GUI(){//constructor Frame myFrame = new Frame("Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); myFrame.setBackground(Color.yellow); LWButton aLWButton = new LWButton("LW Button A"); Button myStdButton = new Button("Std Button"); LWButton bLWButton = new LWButton(""); bLWButton.setLabel("LW Button B"); aLWButton.addActionListener(new MyActionListener()); bLWButton.addActionListener(new MyActionListener()); myStdButton.addActionListener(new MyActionListener()); myFrame.add(aLWButton); myFrame.add(myStdButton); myFrame.add(bLWButton); myFrame.setSize(400,200); myFrame.setVisible(true); myFrame.addWindowListener(new Terminate()); }//end constructor }//end class GUI //======================================================================== //Class to respond to action events class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println(e); }//end actionPerformed }//end class MyActionListener //======================================================================== class Terminate extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);//terminate the program when the window is closed }//end windowClosing }//end class Terminate //######################################################################## // The following class produces the LW Button Component //======================================================================== class LWButton extends Component { String label; // The label on the button boolean pressed = false; // Becomes true if the button is pressed. ActionListener actionListener;//Refers to a list of ActionListener objects //----------------------------------------------------------------------- public LWButton() {//Constructs a LWButton with no label. this("");//invoke the next constructor with an empty string }//end constructor public LWButton(String label) {//Constructs a LWButton with label. this.label = label; enableEvents(AWTEvent.MOUSE_EVENT_MASK); }//end constructor //----------------------------------------------------------------------- //The following method constructs a list of ActionListener objects that // are registered on a particular LWButton object public void addActionListener(ActionListener listener) { actionListener = AWTEventMulticaster.add(actionListener, listener); }//end addActionListener() //----------------------------------------------------------------------- //The following method removes ActionListener objects from the list // described above public void removeActionListener(ActionListener listener) { actionListener = AWTEventMulticaster.remove(actionListener, listener); }//end removeActionListener //--------------------------------------------------------------------- //The purpose of this method is twofold: // 1. Modify the appearance of the LWButton object. // 2. Invoke the actionPerformed() method in the Listener object that // is registered to listen to this LWButton object. //This method is automatically called whenever a mouse event occurs on the // component and the method enableEvents(AWTEvent.MOUSE_EVENT_MASK) // has been previously invoked on the component. public void processMouseEvent(MouseEvent e) { Graphics g; switch(e.getID()) { case MouseEvent.MOUSE_PRESSED: //When the mouse is pressed on the LWButton object, set the "pressed" // state of the object to true and force it to be repainted to change // its appearance. pressed = true; repaint(); break; case MouseEvent.MOUSE_RELEASED: //When the mouse is released on the LWButton object, do two things: // 1. Invoke the actionPerformed() method in the Listener objects that // are registered to listen to this LWButton object. // 2. Confirm that the "pressed" state is true and if so, set it to // false and force the object to be repainted to change its // appearance. if(actionListener != null) {//if an ActionListener is registered actionListener.actionPerformed(new ActionEvent( this, ActionEvent.ACTION_PERFORMED, label)); }//end if on actionListener if(pressed == true) { pressed = false; repaint(); }//end if on pressed break; case MouseEvent.MOUSE_ENTERED: System.out.println("Mouse entered for illustration only"); break; case MouseEvent.MOUSE_EXITED: System.out.println("Mouse exited for illustration only"); break; }//end switch super.processMouseEvent(e); }//end paint //--------------------------------------------------------------------- //The following two methods provide the preferred size and the // minimum size to be used by the various layout managers. public Dimension getPreferredSize() {//overridden getPreferredSize() Font f = getFont(); if(f != null) { FontMetrics fm = getFontMetrics(getFont()); int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40); return new Dimension(max, max); } else return new Dimension(100, 100); }//end getPreferredSize() public Dimension getMinimumSize() {//overridden getMinimumSize() return new Dimension(100, 100); }//end getMinimumSize() //------------------------------------------------------------------------ //The following two methods are available to modify the label of // an LWButton object. public String getLabel() {//gets the label return label; }//end getLabel() public void setLabel(String label) {//sets the label this.label = label; invalidate(); repaint(); }//end setLabel() //--------------------------------------------------------------------- //The following overridden paint() method paints the LWButton, with the // appearance of the button being determined by whether the "pressed" // state of the button is true or false. public void paint(Graphics g) {//paints the LWButton int s = Math.min(getSize().width - 1, getSize().height - 1); // paint the interior of the button if(pressed) g.setColor(getBackground().darker().darker()); else g.setColor(getBackground()); g.fillArc(0, 0, s, s, 0, 360); // draw the perimeter of the button g.setColor(getBackground().darker().darker().darker()); g.drawArc(0, 0, s, s, 0, 360); // draw the label centered in the button Font f = getFont(); if(f != null) { FontMetrics fm = getFontMetrics(getFont()); g.setColor(getForeground()); g.drawString(label, s/2 - fm.stringWidth(label)/2, s/2 + fm.getMaxDescent()); }//end if on f!=null }//end overridden paint() method //-------------------------------------------------------------------------- //This method is supposed to determine if a mouse event is inside the // LWButton component. However, as of 3/21/97, there is a vertical // offset error in the Win95 implementation of JDK 1.1 and this method // returns true if the mouse is outside but below the object. It // returns false if the mouse is inside near the top of the object. // Otherwise, it seems to work OK. // NOTE: THE PROBLEM DESCRIBED ABOVE SEEMS TO HAVE BEEN RESOLVED IN // JDK 1.1.3. public boolean contains(int x, int y) { int mx = getSize().width/2; int my = getSize().height/2; return (((mx-x)*(mx-x) + (my-y)*(my-y)) <= mx*mx); }//end contains() //--------------------------------------------------------------------------- }//end class LWButton //===========================================================