我用的版本是jdk1.6.0_02。下面是我从《JAVA2核心技术》上抄的一个例子,是用eclipse编译的,结果有个这样的问题,见注释。请各位帮忙看看,谢谢了先啊。
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class Sketch
{
public static void main (String[] args)
{
SketchFrame BTF = new SketchFrame("Sketch");
BTF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BTF.show();
}
}
class SketchPanel extends JPanel
{
public SketchPanel()
{
Last_Point = new Point2D.Double(100,100);
lines = new ArrayList<Line2D>();
HandleKey listener = new Handlekey(); //Handlekey 无法解析为类型 1192549964406 28
addkeylistener(listener); //没有为类型 SketchPanel 定义方法 addkeylistener(SketchPanel.HandleKey)1192549964406 29
setFocusable(true);
}
public void add_Line(int dx, int dy)
{
Point.Double end_Point = new Point2D.Double(Last_Point.getX() + dx, Last_Point.getY() + dy);
Line2D.Double line = new Line2D.Double(Last_Point, end_Point);
lines.add(line);
Last_Point = end_Point;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (int k = 0; k < lines.size(); k++)
{
g2.draw((Line2D)(lines.get(k)));
}
}
private Point2D Last_Point;
private ArrayList lines;
private static final int SMALL_INCREMENT = 1;
private static final int LARGE_INCREMENT = 5;
private class HandleKey implements KeyListener
{
public void keyPressed(KeyEvent event)
{
int keyCode = event.getKeyCode();
int d;
if (event.isShiftDown())
{
d = LARGE_INCREMENT;
}
else
{
d = SMALL_INCREMENT;
}
if (keyCode == KeyEvent.VK_UP)
{
add_Line(0, -d);
}
else if (keyCode == KeyEvent.VK_DOWN)
{
add_Line(0, d);
}
else if (keyCode == KeyEvent.VK_LEFT)
{
add_Line(-d, 0);
}
else if (keyCode == KeyEvent.VK_RIGHT)
{
add_Line(d, 0);
}
}
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_Line(0, -d);
}
else if (keyChar == 'j')
{
add_Line(0, d);
}
else if (keyChar == 'k')
{
add_Line(-d, 0);
}
else if (keyChar == 'l')
{
add_Line(d, 0);
}
}
}
}
class SketchFrame extends JFrame
{
public SketchFrame(String title)
{
Container contentPane = getContentPane();
SketchPanel SP = new SketchPanel();
contentPane.add(SP);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
setSize(screenWidth/2,screenHeight/2);
setLocation(screenWidth/4,screenHeight/4);
}
}
错误信息见注释,可是我定义了内部类HandleKey的啊,怎么会出现这种错误呢?
------解决方案--------------------
方法名你写错了吧,看看大小写啊
------解决方案--------------------
bd
------解决方案--------------------
这两个错误可以这样解决:
错误1. Handlekey 无法解析为类型 1192549964406 28