📄 mouseeventapplet.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class MouseEventApplet extends JApplet {
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField();
GridLayout gridLayout1 = new GridLayout();
JTextArea jTextArea1 = new JTextArea();
BorderLayout borderLayout1 = new BorderLayout(10,10);
public void init() {
try {
myInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void myInit() throws Exception {
this.setSize(new Dimension(350, 300));
this.getContentPane().setLayout(new BorderLayout(10,10));
jButton1.setText("jButton1");
jButton2.setText("jButton2");
jButton3.setText("jButton3");
jButton4.setText("jButton4");
jLabel1.setText("X轴");
jTextField1.setPreferredSize(new Dimension(50, 25));
jLabel2.setText("Y轴");
jTextField2.setPreferredSize(new Dimension(50, 25));
gridLayout1.setColumns(1);
gridLayout1.setHgap(5);
gridLayout1.setRows(4);
gridLayout1.setVgap(5);
jPanel1.setLayout(gridLayout1);
jPanel1.add(jButton1, null);
jPanel1.add(jButton2, null);
jPanel1.add(jButton3, null);
jPanel1.add(jButton4, null);
this.getContentPane().add(jPanel1, BorderLayout.WEST);
jPanel2.setLayout(new FlowLayout());
jPanel2.add(jLabel1, null);
jPanel2.add(jTextField1, null);
jPanel2.add(jLabel2, null);
jPanel2.add(jTextField2, null);
this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
jPanel3.setLayout(borderLayout1);
this.getContentPane().add(jPanel3, BorderLayout.CENTER);
jTextArea1.setColumns(20);
JScrollPane jsp=new JScrollPane(jTextArea1);//制作一scrollpane以承装textarea
jPanel3.add(jsp, BorderLayout.CENTER);
JLabel jLabel3=new JLabel("事件发生显示区");
JPanel jPanel4=new JPanel();
jPanel4.add(jLabel3);
jPanel3.add(jPanel4, BorderLayout.NORTH);
//老鼠事件倾听
jButton1.addMouseListener(new ButtonMouseListener(this));
jButton1.addMouseMotionListener(new ButtonMouseMotionListener(this));
jButton2.addMouseListener(new ButtonMouseListener(this));
jButton2.addMouseMotionListener(new ButtonMouseMotionListener(this));
jButton3.addMouseListener(new ButtonMouseListener(this));
jButton3.addMouseMotionListener(new ButtonMouseMotionListener(this));
jButton4.addMouseListener(new ButtonMouseListener(this));
jButton4.addMouseMotionListener(new ButtonMouseMotionListener(this));
}
class ButtonMouseListener implements MouseListener {
public MouseEventApplet japplet=new MouseEventApplet() ;
public JButton source=new JButton();
public ButtonMouseListener(MouseEventApplet japplet1){
this.japplet=japplet1;
}
public void mouseEntered(MouseEvent event) {
source=(JButton)event.getSource();
japplet.jTextArea1 .append ("进入"+source.getActionCommand()+"\n");
if (source.getActionCommand()=="jButton1"){setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));}
if (source.getActionCommand()=="jButton2"){setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));}
if (source.getActionCommand()=="jButton3"){setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));}
if (source.getActionCommand()=="jButton4"){setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));}
}
public void mouseExited(MouseEvent event) {
JButton source=(JButton)event.getSource();
japplet.jTextArea1 .append ("离开"+source.getActionCommand()+"\n");
japplet.jTextField1.setText("");
japplet.jTextField2.setText("");
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public void mousePressed (MouseEvent event) {
JButton source=(JButton)event.getSource();
japplet.jTextArea1 .append ("在"+source.getActionCommand()+"上将老鼠压下去\n");}
public void mouseClicked (MouseEvent event) {
JButton source=(JButton)event.getSource();
japplet.jTextArea1 .append ("在"+source.getActionCommand()+"上将老鼠敲一下\n"); }
public void mouseReleased(MouseEvent event) {
JButton source=(JButton)event.getSource();
japplet.jTextArea1 .append ("在"+source.getActionCommand()+"上的老鼠被释放了!\n");}
}
class ButtonMouseMotionListener implements MouseMotionListener {
public MouseEventApplet japplet=new MouseEventApplet() ;
public ButtonMouseMotionListener(MouseEventApplet japplet1){
this.japplet=japplet1;
}
public void mouseDragged(MouseEvent event){
}
public void mouseMoved(MouseEvent event){
japplet.jTextField1.setText(Integer.toString(event.getX()));
japplet.jTextField2.setText(Integer.toString(event.getY()));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -