1

这大概是个初级问题。但是,我已经阅读了《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); 
}




}
4

0 回答 0