通过添加按钮扩展类

时间:2013-01-08 21:27:40

标签: java button graphics awt extend

这可能是一个基本问题。但是,我已经完成了阅读Absolute Beginner的Java编程的第12章,并已经接近挑战部分。我无法让程序显示带有扩展类的带标签的按钮。

规范声明:

  

扩展JPRButton3D类以创建一个显示标签的按钮   你现在熟悉的AWT Button课程。作为额外的测试,覆盖   isFocusable()方法,以便您可以遍历您的按钮类并确保您   绘制一些特殊图形,使按钮具有焦点时显而易见。

如何编辑LabelButton3D和LabelButton3DTest的代码来完成此任务?

对此规范的回答可能有助于许多新Java程序员扩展自己的类。

非常感谢你的时间和合作,重申了这个问题。

这是JPRButton3D的代码:

package jpr.lightweight;
import java.awt.*;
import java.awt.event.*;

/**
 * A lightweight 3D Button class that fires actions when clicked.
 * When it is enabled it appears {@link #RAISED RAISED}, when
 * it is pressed it appears {@link #SUNK SUNK}, and when it is
 * not enabled, it appears {@link #FLAT FLAT}.
 */
public class JPRButton3D extends JPRRectComponent3D {
  private boolean pressed;
  /**
   * This <code>JPRButton3D</code>'s <code>ActionListener</code>.
   */
  protected ActionListener actionListener;
  private String actionCommand;

  /**
   * Constructs a new <code>JPRButton3D</code> with minimum size
   */
  public JPRButton3D() {
    this(ABSOLUTE_MIN_WIDTH, ABSOLUTE_MIN_HEIGHT, 1);
  }

  /**
   * Constructs a new <code>JPRButton3D</code> with the given dimensions.
   * @param wide the width
   * @param high the height
   */
  public JPRButton3D(int wide, int high) {
    this(wide, high, 1);
  }

/**
 * Constructs a new <code>JPRButton3D</code> with the given dimensions
 * and border magnitude.
 * @param wide the width
 * @param high the height
 * @param border_magnitude the border's magnitude
 */

public JPRButton3D(int wide, int high, int border_magnitude) {
  super(wide, high, RAISED, border_magnitude);
  enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}

public void processMouseEvent(MouseEvent e) {
  if (isEnabled() & e.getModifiers() == MouseEvent.BUTTON1_MASK) {
    switch(e.getID()) {
      case MouseEvent.MOUSE_PRESSED:
        pressed = true;
        current_appearance = SUNK;
        repaint();
        break;
      case MouseEvent.MOUSE_EXITED:
        if (pressed) {
          pressed = false;
          current_appearance = RAISED;
          repaint();
       }
       break;
      case MouseEvent.MOUSE_RELEASED:
        if (pressed) {
          current_appearance = RAISED;
          repaint();
          if (actionListener != null) {
            actionListener.actionPerformed(new ActionEvent(this,
              ActionEvent.ACTION_PERFORMED, actionCommand,
              e.getModifiers()));
          }
        }
        break;
     }
   }
   super.processMouseEvent(e);
}

/**
 * Adds the specified <code>ActionListener</code>
 * @param listener <code>ActionListener</code> to add
 */
 public void addActionListener(ActionListener listener) {
   actionListener = AWTEventMulticaster.add(actionListener, listener);
 }

/**
 * Removes the specified <code>ActionListener</code>
 * @param listener <code>ActionListener</code> to remove
 */
public void removeActionListener(ActionListener listener) {
  actionListener = AWTEventMulticaster.remove(actionListener,
                                                 listener);
}

/**
 * Sets the action command associated with action events.
 * @param command The action command.
 */
public void setActionCommand(String command) {
  actionCommand = command;
}
 /**
  * Gets the action command associated with action events.
  * @return the action command
  */
public String getActionCommand() {
  return actionCommand;
}
/**
 * Enables or disables this <code>JPRButton3D</code>.
 * @param b <code>true</code> to enable, <code>false</code> to disable
 */
public void setEnabled(boolean b) {
  if (b) current_appearance = RAISED;
  else current_appearance = FLAT;
  repaint();
  super.setEnabled(b);
}

}

这是LabelButton3DTest的我的代码(扩展JPRButton3D):

import java.awt.*;
import java.awt.event.*;
import jpr.lightweight.JPRButton3D;

public class LabelButton3DTest extends GUIFrame {

LabelButton3D[] b;
String s;


public LabelButton3DTest() {
    super("LabelButton3D Test");
    setLayout(new FlowLayout());
   b = new LabelButton3D[1];

   b[0] = new LabelButton3D("Favorite Button"); 
   b[0] = new LabelButton3D(75, 35, 1);
   add(b[0]);


   pack();
   setVisible(true);
}

   public static void main(String args[]) {
       new LabelButton3DTest();


   }

}

这是LabelButton3D的代码:

public class LabelButton3D extends JPRButton3D {

public LabelButton3D(String label) {

}
 public LabelButton3D(int wide, int high, int border_magnitude) {
 super(wide, high, border_magnitude); 
}




}

0 个答案:

没有答案