000
19.05.2004, 00:04 Uhr
kaihua
|
In Core Java Band 1, Seite 424 steht ein Programm "SeparateGUITest.java"
Basis auf diesem Programm will ich zusätzlich auch ein Rectecke zeichnen zulassen, das is nur zu testen, wenn das schafft, dann kannn ich auch Kreis, Linien, u.s.w zu schaffen.
Folgendes ist von mir umgeschriebenes Programm, aber ich habe jetzt ein Problem, das Rectecke zeichnet nur einmal nähmlich blinkt einmal, dann sehe ich es nicht mehr. Kann jemand mir helfen, das Problem zu lösen?
Code: |
package separateguitest;
import java.awt.*; import java.awt.event.*; import javax.swing.*;
class Rectangle { int x; int y; int width; int height; Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } } class DrawAction extends AbstractAction { public DrawAction(String name, Icon icon, Rectangle rect, Component comp) { putValue(Action.NAME, name); putValue(Action.SMALL_ICON, icon); target = comp; } public void actionPerformed(ActionEvent evt) { target.getGraphics().drawRect(rect.x, rect.y, rect.width, rect.height); target.repaint(); } private Component target; Rectangle rect = new Rectangle(10,10,40,40); }
class ColorAction extends AbstractAction { public ColorAction(String name, Icon icon, Color c, Component comp) { putValue(Action.NAME, name); putValue(Action.SMALL_ICON, icon); putValue("Farbe", c); target = comp; } public void actionPerformed(ActionEvent evt) { Color c = (Color)getValue("Farbe"); target.setBackground(c); target.repaint(); } private Component target; } /* class ActionButton extends JButton { public ActionButton(Action a) { setText((String)a.getValue(Action.NAME)); Icon icon = (Icon)a.getValue(Action.SMALL_ICON); if(icon!=null) setIcon(icon); addActionListener(a); } } */ class SeparateGUIFrame extends JFrame { public SeparateGUIFrame() { setTitle("SeparateGUITest"); setSize(300,200); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); Action blueAction = new ColorAction("Blau", new ImageIcon("blue-ball.gif"), Color.blue, panel); Action yellowAction = new ColorAction("Gelb", new ImageIcon("yellow-ball.gif"), Color.yellow, panel); Action redAction = new ColorAction("Rot", new ImageIcon("red-ball.gif"), Color.red, panel);
Action rectAction = new DrawAction("Rectangle", null, new Rectangle(10,10,40,40), panel); /* panel.add(new ActionButton(yellowAction)); panel.add(new ActionButton(blueAction)); panel.add(new ActionButton(redAction)); */ panel.registerKeyboardAction(yellowAction, KeyStroke.getKeyStroke(KeyEvent.VK_G, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); panel.registerKeyboardAction(blueAction, KeyStroke.getKeyStroke(KeyEvent.VK_B, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); panel.registerKeyboardAction(redAction, KeyStroke.getKeyStroke(KeyEvent.VK_R, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
Container contentPane = getContentPane(); contentPane.add(panel);
JMenu m = new JMenu("Farbe"); m.add(yellowAction); m.add(blueAction); m.add(redAction); m.add(rectAction); JMenuBar mbar = new JMenuBar(); mbar.add(m); setJMenuBar(mbar); } }
public class SeparateGUITest { public static void main(String [] args) { JFrame frame = new SeparateGUIFrame(); frame.show(); } }
|
|