java程序:KeyEvent的一个问题

来源:百度知道 编辑:UC知道 时间:2024/05/19 01:45:41
希望显示line“xl,yl,x,y”,初始点在frame中间,然后按键盘上的上下左右键,分别向上下左右画出一条10像素长的线。
(xl,yl)为起点坐标,(x,y)为线的终点。
谢谢各位了!小妹急!
package programmingExercises;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exercise14_4 extends JFrame{
/**
(Drawing lines using the arrow keys)
Write a program that draws line segments using the arrow keys.
The line starts from the center of the frame and
draws toward east, north, west, or south
when the right-arrow key, up-arrow key, left-arrow key,
or down-arrow key is clicked, as shown in Figure 14.19(c).
*/
int x=(int)(this.getWidth()/2);
int y=(int)(this.getHeight()/2);
int xl;
int yl;
public Exercise14_4(){
//
this.add(new LineDrawed());
}
public static void main(String[] args) {

Exercise14_4 frame=new Exercise14_4();
frame.setTitle("Exercise14_4");
frame.setSize(400

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Exercise14_4 extends JFrame {
/**
* (Drawing lines using the arrow keys) Write a program that draws line
* segments using the arrow keys. The line starts from the center of the
* frame and draws toward east, north, west, or south when the right-arrow
* key, up-arrow key, left-arrow key, or down-arrow key is clicked, as shown
* in Figure 14.19(c).
*/
int x = (int) (this.getWidth() / 2);
int y = (int) (this.getHeight() / 2);
int xl;
int yl;
LineDrawed line = new LineDrawed();
public Exercise14_4() {
//
this.add(line);
this.addKeyListener(new KeyAdapter() {

@Override
public void keyPressed(KeyEvent e) {

// TODO Auto-generated method stub
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
yl = y;
y -= 10;