java 跑马灯代码,运行后一动也不动,急死人了,高手快来帮帮忙啊~

java 跑马灯代码,运行后一动也不动,急死人了,高手快来帮帮忙啊~,第1张

import java.awt.Color

import java.awt.Dimension

import java.awt.Point

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JLabel

import javax.swing.JWindow

import javax.swing.Timer

public class Marquee extends JWindow {

public static String marqueeText

public static Marquee myMarquee = new Marquee("呜呜~,跑李稿马灯动不了了~ ")

public static JLabel myJLabel = new JLabel(myMarquee.toString())

// 实例化,否则nullpoint

public static Point anchor = new Point()

public Marquee(String marquee) {

marqueeText = marquee

}

public static void main(String[] args) {

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

public void actionPerformed(ActionEvent e) {

Point extSize = myMarquee.getLocation()

Dimension viewSize = myJLabel.getPreferredSize()

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

myMarquee.setLocation(anchor)

if (anchor.x >viewSize.width) {

anchor.x = -extSize.x

}

}

})

myJLabel.setForeground(Color.RED)

myJLabel.setOpaque(true)

myJLabel.setPreferredSize(new Dimension(1024, 30))

myMarquee.setVisible(true)

myMarquee.setLocation(0, 0)

myMarquee.setAlwaysOnTop(true)

myMarquee.add(myJLabel)

myMarquee.pack()

// 关键一句空扰芦,你设置了定时器,但没有让它运行起来,start即可

marquee.start()

}

public String toString() {

return marqueeText

}

}

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()

}

}

}

}


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

原文地址: http://outofmemory.cn/yw/12510536.html

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

发表评论

登录后才能评论

评论列表(0条)

保存