我用的版本是jdk1.6.0_02。下面是我从《JAVA2核心技术》上抄的一个例子,是用JCreator编译的,结果有个这样的问题,见注释。帮忙看看。
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class Sketch
{
public static void main()
{
SketchFrame frame = new SketchFrame();
frame.setDefaultOperation(EXIT_ON_CLOSE);//找不到符号
frame.setVisible(true);
}
}
class SketchFrame extends JFrame
{
public SketchFrame()
{
setTitle( "Sketch ");
setSize(W,H);
SketchPanel panel = new SketchPanel();
add(panel);
}
private final static int W = 300;
private final static int H = 200;
}
class SketchPanel extends JPanel
{
private Point2D last;
private ArrayList <Line2D> lines;
public SketchPanel()
{
last = new Point2D.Double(100,100);
lines = new ArrayList <Line2D> ();
KeyHandler listener = new KeyHandler();
addKeyListener(listener);
setFocusable(true);
}
public void add(int dx,int dy)
{
Point2D end = new Point2D.Double(last.getX()+dx,last.getY()+dy);
Line2D line = new Line2D.Double(last,end);
lines.add(line);
repaint();
last = end;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for(Line2D l:lines)
{
g2.draw(l);
}
}
private static final int SMALL_INCREMENT = 1;
private static final int LARGE_INCREMENT = 5;
private class KeyHandler implements KeyListener
{
public void keyPressed(KeyEvent e)
{
int KeyCode = event.getKeyCode();//找不到符号
int d;
if(event.isShiftDown())//找不到符号
d = LARGE_INCREMENT;
d = SMALL_INCREMENT;
if(KeyCode == KeyEvent.VK_RIGHT) add(-d,0);
if(KeyCode == KeyEvent.VK_LEFT) add(d,0);
if(KeyCode == KeyEvent.VK_UP) add(0,-d);
if(KeyCode == KeyEvent.VK_DOWN) add(0,d);
}
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event)
{
char keyChar = event.getKeyChar();
int d;
if(Character.isUpperCase(keyChar))
{
d = LARGE_INCREMENT;
keyChar = Character.toLowerCase(keyChar);
}
else
d = SMALL_INCREMENT;
if(keyChar == 'h ') add(-d,0);
else if(keyChar == 'l ') add(d,0);
else if(keyChar == 'k ') add(0,-d);
else if(keyChar == 'j ') add(0,d);
}
}
}
------解决方案--------------------