关于java的问题
程序功能:在窗口中用鼠标左键和右键单击时,窗口下方显示哪个键被点击,坐标是多少;当键盘上某个键被点击时,窗口下方显示这个键的名称。我自己写了这个程序,但是错误很多,谁帮修...
程序功能:在窗口中用鼠标左键和右键单击时,窗口下方显示哪个键被点击,坐标是多少;当键盘上某个键被点击时,窗口下方显示这个键的名称。
我自己写了这个程序,但是错误很多,谁帮修改下……
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseKeyEventDemo extends JFrame implements KeyListener,MouseListener {
JFrame f=new JFrame();
JPanel p=new JPanel;
TextField txt;
f.setSize(300,150);
f.setLayout(new BorderLayout());
f.add(p,"South");
f.setVisible(true);
p.setLayout(null);
p.add(txt);
f.addMouseListener(this); //为窗口增加鼠标事件监听器
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){
int x,y;
String Str;
if(e.getModifiers()==MouseEvent.BUTTON1_MASK)
{
x=e.getX();y=e.getY();
text.setText("鼠标左键被点击"+x+","+y)
}
if(e.getModifiers()==MouseEvent.BUTTON3_MASK)
{
x=e.getX();y=e.getY();
text.setText("鼠标右键被点击"+x+","+y);
}
}
public void keyTyped(KeyEvent e) {};
public void keyPressed(KeyEvent e) {
String key = String.fromCharCode(event.keyCode)
text.setText("键盘"+Key+"键被按下");
};
public void keyReleased(KeyEvent e) {};
public class static void main(String[] args){
MouseKeyEventDemo M=new MouseKeyEventDemo;
}
} 展开
我自己写了这个程序,但是错误很多,谁帮修改下……
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseKeyEventDemo extends JFrame implements KeyListener,MouseListener {
JFrame f=new JFrame();
JPanel p=new JPanel;
TextField txt;
f.setSize(300,150);
f.setLayout(new BorderLayout());
f.add(p,"South");
f.setVisible(true);
p.setLayout(null);
p.add(txt);
f.addMouseListener(this); //为窗口增加鼠标事件监听器
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){
int x,y;
String Str;
if(e.getModifiers()==MouseEvent.BUTTON1_MASK)
{
x=e.getX();y=e.getY();
text.setText("鼠标左键被点击"+x+","+y)
}
if(e.getModifiers()==MouseEvent.BUTTON3_MASK)
{
x=e.getX();y=e.getY();
text.setText("鼠标右键被点击"+x+","+y);
}
}
public void keyTyped(KeyEvent e) {};
public void keyPressed(KeyEvent e) {
String key = String.fromCharCode(event.keyCode)
text.setText("键盘"+Key+"键被按下");
};
public void keyReleased(KeyEvent e) {};
public class static void main(String[] args){
MouseKeyEventDemo M=new MouseKeyEventDemo;
}
} 展开
1个回答
展开全部
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MouseKeyEventDemo implements KeyListener,
MouseListener {
JFrame f = new JFrame("MouseKeyEventDemo");
JPanel p = new JPanel();
JTextField text = new JTextField(14);
{
f.setSize(300, 150);
f.setLayout(new BorderLayout());
f.add(p, BorderLayout.CENTER);
p.add(text);
text.setEditable(false);
f.addMouseListener(this); // 为窗口增加鼠标事件监听器
text.addKeyListener(this);//
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void mousePressed(MouseEvent e) {
int x= e.getX(), y =e.getY();
if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
text.setText("鼠标左键被点击:(" + x + "," + y+")");
}else if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
text.setText("鼠标右键被点击:(" + x + "," + y+")");
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void keyTyped(KeyEvent e) {
};
public void keyPressed(KeyEvent e) {
String key = e.getKeyChar()+"";
text.setText("键盘" + key + "键被按下");
};
public void keyReleased(KeyEvent e) {
};
public static void main(String[] args){
MouseKeyEventDemo M=new MouseKeyEventDemo();
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MouseKeyEventDemo implements KeyListener,
MouseListener {
JFrame f = new JFrame("MouseKeyEventDemo");
JPanel p = new JPanel();
JTextField text = new JTextField(14);
{
f.setSize(300, 150);
f.setLayout(new BorderLayout());
f.add(p, BorderLayout.CENTER);
p.add(text);
text.setEditable(false);
f.addMouseListener(this); // 为窗口增加鼠标事件监听器
text.addKeyListener(this);//
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void mousePressed(MouseEvent e) {
int x= e.getX(), y =e.getY();
if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
text.setText("鼠标左键被点击:(" + x + "," + y+")");
}else if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
text.setText("鼠标右键被点击:(" + x + "," + y+")");
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void keyTyped(KeyEvent e) {
};
public void keyPressed(KeyEvent e) {
String key = e.getKeyChar()+"";
text.setText("键盘" + key + "键被按下");
};
public void keyReleased(KeyEvent e) {
};
public static void main(String[] args){
MouseKeyEventDemo M=new MouseKeyEventDemo();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询