当前位置: 代码迷 >> J2SE >> 修女重逢解决方案
  详细解决方案

修女重逢解决方案

热度:267   发布时间:2016-04-24 13:25:10.0
修女重逢
设计个鼠标键盘控制程序.JAVA版.谢谢了!

如果在窗口中移动鼠标,窗口的底部将显示鼠标的当前坐标位置;
如果移动鼠标的同时按按住Ctrl或Shift键,窗口低部会显示出C或S,
如果用户按下键盘上的键时,程序窗口的底部显示字母"D";
当用户松开键盘的时候,程序窗口底部显示字母"U".

------解决方案--------------------
package mousedemo;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class MouseMove extends JFrame {
public static void main(String args[]){
new MouseMove();
}
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JLabel jLabel1 = new JLabel();
public MouseMove() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
this.setBounds(300,200,400,300);
setTitle("Frame Title");
this.setVisible(true);
this.addMouseMotionListener(new MouseMove_this_mouseMotionAdapter(this));
this.addKeyListener(new MouseMove_this_keyAdapter(this));
this.addKeyListener(new MouseMove_this_keyReleased(this));
jLabel1.setText("jLabel1");
contentPane.add(jLabel1, java.awt.BorderLayout.SOUTH);
}

public void this_keyPressed(KeyEvent e) {
if(e.getKeyCode()==16){
jLabel1.setText("S");
}else if(e.getKeyCode()==17){
jLabel1.setText("C");
}else{
jLabel1.setText("D");
}
}

public void this_keyReleased(KeyEvent e) {
jLabel1.setText("U");
}

public void this_mouseMoved(MouseEvent e) {
jLabel1.setText("X= " + e.getX() + "\t Y= " + e.getY());
}

}

class MouseMove_this_keyReleased extends KeyAdapter {
private MouseMove adaptee;
MouseMove_this_keyReleased(MouseMove adaptee) {
this.adaptee = adaptee;
}
public void keyReleased(KeyEvent e) {
adaptee.this_keyReleased(e);
}
}
class MouseMove_this_keyAdapter extends KeyAdapter {
private MouseMove adaptee;
MouseMove_this_keyAdapter(MouseMove adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.this_keyPressed(e);
}
}


class MouseMove_this_mouseMotionAdapter extends MouseMotionAdapter {
private MouseMove adaptee;
MouseMove_this_mouseMotionAdapter(MouseMove adaptee) {
this.adaptee = adaptee;
}

public void mouseMoved(MouseEvent e) {
adaptee.this_mouseMoved(e);
}
}

------解决方案--------------------
写好了,不明白楼主为何自称修女??:
Java code
import javax.swing.*;import java.awt.*;import java.awt.event.*;public class MouseDetector extends JFrame implements MouseMotionListener, KeyListener {        private JPanel detectingArea;    private JLabel coordinates;    private JLabel modifierStatus;    private JLabel keyStatus;        public MouseDetector() {        super("鼠标侦测器");        setSize(400, 300);        setupPanels();        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }        private void setupPanels() {        Container contentPane = getContentPane();        contentPane.setLayout(new BorderLayout());                detectingArea = new JPanel();        detectingArea.addMouseMotionListener(this);        addKeyListener(this);        contentPane.add(detectingArea, BorderLayout.CENTER);                JPanel statusBar = new JPanel();        coordinates = new JLabel("X: , Y: ");        modifierStatus = new JLabel();        keyStatus = new JLabel("U");        statusBar.add(coordinates);        statusBar.add(modifierStatus);        statusBar.add(keyStatus);        contentPane.add(statusBar, BorderLayout.SOUTH);    }        public static void main(String[] args) {        new MouseDetector().setVisible(true);    }        public void mouseMoved(MouseEvent me) {        coordinates.setText("X: " + me.getXOnScreen() + ", Y: " + me.getYOnScreen());        detectingKeys(me);    }    public void mouseDragged(MouseEvent me) {        mouseMoved(me);    }        public void keyReleased(KeyEvent ke) {        keyStatus.setText("U");        detectingKeys(ke);    }    public void keyPressed(KeyEvent ke) {        keyStatus.setText("D");        detectingKeys(ke);    }        public void keyTyped(KeyEvent ke) {    }    private void detectingKeys(InputEvent ie) {        if (ie.getModifiers() == InputEvent.CTRL_MASK) modifierStatus.setText("C");        else if (ie.getModifiers() == InputEvent.SHIFT_MASK) modifierStatus.setText("S");        else if (ie.getModifiers() == InputEvent.CTRL_MASK + InputEvent.SHIFT_MASK) modifierStatus.setText("CS");        else modifierStatus.setText("");    }}
  相关解决方案