📄 mouselistenerdemo.java
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MouseListenerDemo extends JFrame implements MouseListener{ private JButton button; public MouseListenerDemo() {/* A JButton is added to a JFrame. The button registers *//* a MouseListener. Since the MouseListenerDemo class *//* itself serves as the MouseListener, the addMouseListener() *//* method is passed the "this" reference. */ button = new JButton("help"); button.setBorder(BorderFactory.createRaisedBevelBorder()); button.setBackground(Color.lightGray); button.addMouseListener(this); JPanel panel = new JPanel(); panel.add(button); getContentPane().add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 200, 200); setVisible(true); }/* Since the MouseListenerDemo class serves as the MouseListener *//* all of the implementations of the methods declared in the *//* MouseListener interface are implemented inside the *//* MouseListenerDemo class. These methods are used to change *//* the background color of the JButton depending on what the *//* mouse is doing. The mouseClicked() method is not used, so *//* it is implemented as a stub method. */ public void mouseEntered(MouseEvent event) { button.setBackground(Color.pink); } public void mouseExited(MouseEvent event) { button.setBackground(Color.lightGray); } public void mousePressed(MouseEvent event) { button.setBackground(Color.red); } public void mouseReleased(MouseEvent event) { button.setBackground(Color.pink); } public void mouseClicked(MouseEvent event) {} public static void main(String args[]) { MouseListenerDemo demo = new MouseListenerDemo(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -