Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:键盘事件问题 急 [Re:elminsterjimmy]
dejinzhao





发贴: 32
积分: 0
于 2006-11-05 23:02 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
/** 不要直接向panel中添加键盘事件(add方法)、应该用类似下面的方法
*KeyStroke shiftup = KeyStroke.getKeyStroke(KeyEvent.VK_UP,java.awt.event.InputEvent.SHIFT_DOWN_MASK);
panel.registerKeyboardAction(this,"shiftup",shiftup,JComponent.WHEN_FOCUSED);
*/
//运行结果如图

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

public class TestJPanel implements java.awt.event.ActionListener{
private JPanel panel;
private Point last;
private java.util.Vector lines;
private static final int SMALL_INCREMENT=4;
private static final int LARGE_INCREMENT=8;
int d=SMALL_INCREMENT;
public TestJPanel() {
last=new Point(100,100);
lines=new java.util.Vector();
JFrame frame=new JFrame();
panel=new JPanel(){
public void paintComponent(Graphics g){
super.paintComponentPresent;
Point pointStart,pointEnd;
g.setColor(java.awt.Color.red);
for(int i=0;i<lines.size()-1;i++){
pointStart=(Point)lines.elementAtLight Bulb;
pointEnd=(Point)lines.elementAt(i+1);
g.drawLine(pointStart.x,pointStart.y,pointEnd.x,pointEnd.y);
}
}
};
panel.setLayout(null);
javax.swing.JButton button=new javax.swing.JButton();
button.setBounds(10,10,100,30);
panel.add(button);//添加按钮
frame.getContentPane().add(panel,java.awt.BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

KeyStroke shiftup = KeyStroke.getKeyStroke(KeyEvent.VK_UP,java.awt.event.InputEvent.SHIFT_DOWN_MASK);
panel.registerKeyboardAction(this,"shiftup",shiftup,JComponent.WHEN_FOCUSED);
KeyStroke shiftdown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,java.awt.event.InputEvent.SHIFT_MASK);
panel.registerKeyboardAction(this,"shiftdown",shiftdown,JComponent.WHEN_FOCUSED);

KeyStroke up = KeyStroke.getKeyStroke(KeyEvent.VK_UP,0);
panel.registerKeyboardAction(this,"up",up,JComponent.WHEN_FOCUSED);
KeyStroke down = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0);
panel.registerKeyboardAction(this,"down",down,JComponent.WHEN_FOCUSED);
KeyStroke left = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0);
panel.registerKeyboardAction(this,"left",left,JComponent.WHEN_FOCUSED);
KeyStroke right = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0);
panel.registerKeyboardAction(this,"right",right,JComponent.WHEN_FOCUSED);
frame.setBounds(30,30,300,300);
frame.setVisible(true);
}
public void add(int dx, int dy) {
Point end=new Point(last.x+dx,last.y+dy);
lines.add(end);
panel.repaint();
last=end;
}
public static void main (String args[]) {
new TestJPanel();
}
public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
String actionName=actionEvent.getActionCommand();
System.out.println("actionName="+actionName);
if(actionName.equalsIgnoreCase("shiftup")){
d=SMALL_INCREMENT;
}
if(actionName.equalsIgnoreCase("shiftdown")){
d=LARGE_INCREMENT;
}
if(actionName.equalsIgnoreCase("up")){
add(0,-d);
}
if(actionName.equalsIgnoreCase("down")){
add(0,d);
}
if(actionName.equalsIgnoreCase("left")){
add(-d,0);
}
if(actionName.equalsIgnoreCase("right")){
add(d,0);
}
}
}




关于女孩子从事软件开发的问题

话题树型展开
人气 标题 作者 字数 发贴时间
13942 键盘事件问题 急 elminsterjimmy 49 2006-11-01 13:16
12142 Re:键盘事件问题 急 bingjiling1636 126 2006-11-01 22:12
12315 Re:键盘事件问题 急 liangx 18 2006-11-11 17:32
12441 Re:键盘事件问题 急 liangx 1879 2006-11-12 15:09
12753 Re:键盘事件问题 急 lijipeipp 122 2006-11-14 14:12
12056 Re:键盘事件问题 急 guochun 17 2006-11-03 16:56
12131 Re:键盘事件问题 急 bingjiling1636 38 2006-11-03 21:11
12162 Re:键盘事件问题 急 dejinzhao 3652 2006-11-05 23:02
12309 Re:键盘事件问题 急 dejinzhao 75 2006-11-06 07:34
12495 Re:键盘事件问题 急 liangx 51 2006-11-09 16:49
11877 Re:键盘事件问题 急 dejinzhao 111 2006-11-10 09:04
12425 Re:键盘事件问题 急 liangx 1394 2006-11-10 12:16
11847 Re:键盘事件问题 急 dejinzhao 2134 2006-11-11 00:11

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923