关于java中的事件触发问题: 为什么按下键盘并释放的时候KeyReleased方法没有被调用,代码如下:
importjava.awt.event.*;importjava.awt.*;importjavax.swing.*;publicclassTest{JTextFiel...
import java.awt.event.*;import java.awt.*;
import javax.swing.*;
public class Test {
JTextField text1;
JTextField text2;
public Test()
{
JFrame frame=new JFrame();
Container contain=frame.getContentPane();
text1=new JTextField(10);
text2=new JTextField(10);
MyKeyEvent e=new MyKeyEvent();
text1.addKeyListener(e);
contain.setLayout(new FlowLayout());
contain.add(text1);
contain.add(text2);
text1.setFocusable(true);
frame.pack();
frame.setVisible(true);
}
class MyKeyEvent extends KeyAdapter
{
public void KeyReleased(KeyEvent e)
{
System.out.println("I have been called");
text2.setText(text1.getText());
}
}
public static void main(String args[])
{
Test test=new Test();
}
} 展开
import javax.swing.*;
public class Test {
JTextField text1;
JTextField text2;
public Test()
{
JFrame frame=new JFrame();
Container contain=frame.getContentPane();
text1=new JTextField(10);
text2=new JTextField(10);
MyKeyEvent e=new MyKeyEvent();
text1.addKeyListener(e);
contain.setLayout(new FlowLayout());
contain.add(text1);
contain.add(text2);
text1.setFocusable(true);
frame.pack();
frame.setVisible(true);
}
class MyKeyEvent extends KeyAdapter
{
public void KeyReleased(KeyEvent e)
{
System.out.println("I have been called");
text2.setText(text1.getText());
}
}
public static void main(String args[])
{
Test test=new Test();
}
} 展开
展开全部
你写错事件了,是实现keylistener事件不是继承那个adapter类
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Test
{
JTextFieldtext1;
JTextFieldtext2;
public Test()
{
JFrame frame = new JFrame();
Container contain = frame.getContentPane();
text1 = new JTextField(10);
text2 = new JTextField(10);
MyKeyEvent e = new MyKeyEvent();
text1.addKeyListener(e);
contain.setLayout(new FlowLayout());
contain.add(text1);
contain.add(text2);
text1.setFocusable(true);
frame.pack();
frame.setVisible(true);
}
class MyKeyEvent implements KeyListener
{
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
System.out.println(e.getKeyChar());
}
}
public static void main(String args[])
{
Test test = new Test();
}
}
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Test
{
JTextFieldtext1;
JTextFieldtext2;
public Test()
{
JFrame frame = new JFrame();
Container contain = frame.getContentPane();
text1 = new JTextField(10);
text2 = new JTextField(10);
MyKeyEvent e = new MyKeyEvent();
text1.addKeyListener(e);
contain.setLayout(new FlowLayout());
contain.add(text1);
contain.add(text2);
text1.setFocusable(true);
frame.pack();
frame.setVisible(true);
}
class MyKeyEvent implements KeyListener
{
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
System.out.println(e.getKeyChar());
}
}
public static void main(String args[])
{
Test test = new Test();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询