import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Shixun5 extends JFrame{
private JLabel lx,ly,l1,l2;
private JTextField tx,ty;
Container container;
public Shixun5(){
this.setTitle("实训5");
this.setSize(600, 450);
this.setVisible(true);
container=this.getContentPane();
container.setLayout(new FlowLayout());
lx=new JLabel("X坐标");
ly=new JLabel("Y坐标");
l1=new JLabel();
l2=new JLabel();
tx=new JTextField(10);
ty=new JTextField(10);
container.add(lx);
container.add(tx);
container.add(ly);
container.add(ty);
container.add(l1);
container.add(l2);
this.addMouseListener(new mLis());
this.addMouseMotionListener(new mmLis());
this.addKeyListener(new keyLis());
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
class mLis extends MouseAdapter{
public void mouseClicked(MouseEvent e){
l1.setText("单击鼠标"+e.getClickCount()+"次");
}
public void mousePressed(MouseEvent e){
l1.setText("鼠标按钮按下 ");
}
public void mouseReleased(MouseEvent e){
l1.setText("鼠标按钮松开 ");
}
public void mouseEntered(MouseEvent e){
l1.setText("鼠标进入窗口");
}
public void mouseExited(MouseEvent e){
l1.setText("鼠标不在窗口");
}
}
class mmLis extends MouseMotionAdapter{
public void mouseMoved(MouseEvent e){
int x=e.getX();
int y=e.getY();
tx.setText(String.valueOf(x));
ty.setText(String.valueOf(y));
}
public void mouseDragged(MouseEvent e){
l1.setText("拖动鼠标");
}
}
class keyLis extends KeyAdapter{
public void keyTyped(KeyEvent e){
char c=e.getKeyChar();
l2.setText("你按下的键盘键是"+c+"");
}
public void KeyPressed(KeyEvent e){
if(e.getKeyCode()==27)
System.exit(0);
}
}
public static void main(String[]args){
new Shixun5();
}
}
2 个解决方案
#1
运行的时候光标在文本框了,键盘事件冲突了,这里加两句代码就可以了
tx = new JTextField(10);
ty = new JTextField(10);
tx.setEnabled(false);
ty.setEnabled(false);
#2
说实话,额米看懂,楼主解决了的话记得回来
回复啊.
#1
运行的时候光标在文本框了,键盘事件冲突了,这里加两句代码就可以了
tx = new JTextField(10);
ty = new JTextField(10);
tx.setEnabled(false);
ty.setEnabled(false);
#2
说实话,额米看懂,楼主解决了的话记得回来
回复啊.