⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise12_9.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
// Exercise12_9.java: Display a moving label
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Exercise12_9 extends JFrame {
  public Exercise12_9() {
    this.getContentPane().add(new MoveLabel());
  }
  
  // Main method
  public static void main(String[] args) {
    // Create a frame
    Exercise12_9 frame = new Exercise12_9();
    frame.setTitle("Exercise12_9: Move Label");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    // Display the frame
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}

class MoveLabel extends JPanel implements ActionListener, MouseListener {
  private int x = getSize().width;
  private int y = 40;
  private Timer timer = new Timer(200, this);
  
  public MoveLabel() {
    addMouseListener(this);
    timer.start();
  }
  
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (x < -20)
      x = getSize().width;
    else
      x -= 10;
    g.drawString("Welcom to Java!", x, y);
  }
  
  public void mouseClicked(MouseEvent e) {
  }
  
  public void mousePressed(MouseEvent e) {
    timer.stop();
  }
  
  public void mouseReleased(MouseEvent e) {
    timer.start();
  }
  
  public void mouseEntered(MouseEvent e) {
  }
  
  public void mouseExited(MouseEvent e) {
  }
  
  public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
    repaint();}
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -