使用Java做一个走马灯,源代码?

使用Java做一个走马灯,源代码?,第1张

import java.awt.Color

import java.awt.Dimension

import java.awt.Font

import java.awt.HeadlessException

import java.awt.Point

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.ComponentAdapter

import java.awt.event.ComponentEvent

 

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JOptionPane

import javax.swing.JViewport

import javax.swing.Timer

 

public class Test84 extends JFrame {

  private Timer timer

  private JLabel view

  private JViewport window

  

  public static void main(String[] args)

  {

    JFrame frm = new Test84("跑马灯")

    frm.setDefaultCloseOperation(EXIT_ON_CLOSE)

    frm.pack()

    frm.setVisible(true)

  }

 

  public Test84(String title) throws HeadlessException

  {

    super(title)

    

    initComponents()

    

    addComponentListener(new ComponentAdapter() {

      public void 滚烂componentResized(ComponentEvent e)

      {

        anchor = new Point()

        anchor.x = -window.getExtentSize().width

        timer.start()

      }

    })

    

    timer = new Timer(100, new ActionListener() {

      public void actionPerformed(ActionEvent e)

      {

        animate()

      }

    })

    timer.setInitialDelay(0)

  }

 

  private void initComponents()

 梁盯 {

   String s = JOptionPane.showInputDialog(null, "请橡备和输入要实现效果的文字:")

    view = new JLabel(s)

    view.setFont(Font.decode("Dialog-BOLD-36"))

    view.setForeground(Color.BLUE)

    

    window = new JViewport()

    window.setView(view)

    getContentPane().add(window)

  }

  

  Point anchor

  private void animate()

  {

    Dimension extSize = window.getExtentSize()

    Dimension viewSize = view.getPreferredSize()

    anchor.x += 5//设置移动的速度

    window.setViewPosition(anchor)

    if (anchor.x > viewSize.width)

      anchor.x = -extSize.width

  }

}

Java怎么滚唯编写走马灯程序代码:

package clock

import java.awt.*

import java.awt.event.*

import javax.swing.*

import java.util.Calendar

import java.util.Date

import java.text.*

public class removingLight extends JFrame {

public removingLight() {

Font font1 = new Font("幼圆", Font.BOLD, 16)

Calendar cal = Calendar.getInstance()

SimpleDateFormat formatter = new SimpleDateFormat(

"EEEE,MMMMdd日,没迟yyyy年 HH:mm:ss")

String mDateTime = formatter.format(cal.getTime())

MovingMessagePanel messagePanel = new MovingMessagePanel(mDateTime)

messagePanel.setFont(font1)

messagePanel.setBackground(Color.BLACK)

messagePanel.setForeground(Color.PINK)

add(messagePanel)

}

public static void main(String[] args) {

removingLight frame = new removingLight()

JLabel label = new JLabel("枯备李开始调试时间:5月5日 结束调试时间:5月6日")

label.setBackground(Color.black)

frame.setTitle("软件1班 XXX 3107006757")

frame.setLocationRelativeTo(null)

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

frame.setSize(320, 120)

frame.setVisible(true)

frame.add(label, BorderLayout.SOUTH)

}

static class MovingMessagePanel extends JPanel {

private String message = " "

private int xCoordinate = 0

private int yCoordinate = 40

public MovingMessagePanel(String message) {

this.message = message

Timer timer = new Timer(100, new TimerListener())

timer.start()

}

public void paintComponent(Graphics g) {

super.paintComponent(g)

if (xCoordinate >getWidth()) {

xCoordinate = -100

}

xCoordinate += 5

g.drawString(message, xCoordinate, yCoordinate)

}

class TimerListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

repaint()

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12331927.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存