Topic: 关于键盘事件

  Print this page

1.关于键盘事件 Copy to clipboard
Posted by: diystar
Posted on: 2003-03-04 09:21

请问能否将一个控件上产生的键盘事件传递给另一个控件

2.??? [Re: diystar] Copy to clipboard
Posted by: diystar
Posted on: 2003-03-04 14:28

没人知道吗?

3.Re:关于键盘事件 [Re: diystar] Copy to clipboard
Posted by: AsFlower520
Posted on: 2003-03-04 20:13

关注!

4.Re:关于键盘事件 [Re: diystar] Copy to clipboard
Posted by: floater
Posted on: 2003-03-04 23:19

Yea, you can do that.

5.Re:关于键盘事件 [Re: diystar] Copy to clipboard
Posted by: taogang
Posted on: 2003-03-04 23:30

I don't know why you ask such question, since you just need call the adapter with same key event instance:
try this:

package Test;

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

public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout = new BorderLayout();
JTextField textF1 = new JTextField();
JTextField textF2 = new JTextField();

//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
textF1.setText("Type something here...");
textF1.addKeyListener(new Frame1_textF1_keyAdapter(this));
contentPane.setLayout(borderLayout);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
textF2.setText("will be same as upper one..");
textF2.addKeyListener(new Frame1_textF2_keyAdapter(this));
contentPane.add(textF1, BorderLayout.NORTH);
contentPane.add(textF2, BorderLayout.SOUTH);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void textF1_keyTyped(KeyEvent e) {
textF2_keyTyped(e);
}

void textF2_keyTyped(KeyEvent e) {
textF2.setText(textF1.getText());
}

}

class Frame1_textF1_keyAdapter extends java.awt.event.KeyAdapter {
Frame1 adaptee;

Frame1_textF1_keyAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void keyTyped(KeyEvent e) {
adaptee.textF1_keyTyped(e);
}
}

class Frame1_textF2_keyAdapter extends java.awt.event.KeyAdapter {
Frame1 adaptee;

Frame1_textF2_keyAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void keyTyped(KeyEvent e) {

adaptee.textF2_keyTyped(e);
}
}

6.Re:关于键盘事件 [Re: diystar] Copy to clipboard
Posted by: diystar
Posted on: 2003-03-05 12:48

不好意思,我想我没解释清楚,一个JTable在聚焦时拥有诸多快捷键,如上、下、page up、page down。。。但我希望在不聚焦JTable的情况下比如现聚焦于一个文本框,但在其上使用的快捷键却能作用于JTable

7.Re:关于键盘事件 [Re: diystar] Copy to clipboard
Posted by: floater
Posted on: 2003-03-05 23:36

There is a way in jdk1.4 to do this, but it's dangerous in the sense of later maintainence.

There is a new interface, KeyEventDispatcher, and a class, KeyboardFocusManager.

You may use these two to pre-intercept the keyevent before it reaches down the chain. Then you trigger this to the JTable. However, if you do this, it will likely catch all the same events on all components and thus could screw up something.


   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