问一个JAVA repaint()的问题
packagechapter16;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publiccl...
package chapter16;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class java16_11 extends JFrame
{
public java16_11()
{
add(new MovingMessagePanel("LOVE"));
}
public static void main(String[] args)
{
JFrame frame=new java16_11();
frame.setTitle("java16_11");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class MovingMessagePanel extends JPanel
{
private String message="LOVE";
private int xCoordinate=0;
private int yCoordinate=20;
public MovingMessagePanel(String message)
{
this.message=message;
Timer timer=new Timer(100,new TimerListener());//创建计时器,100ms是周期
timer.start();//启动定时器
}
protected void paintComponent(Graphics g)//显示出string
{
super.paintComponent(g);
if(xCoordinate>getWidth())
{
xCoordinate=0;
}
xCoordinate+=5;
g.setFont(new Font("TimesRoman",Font.PLAIN,24));//应该先进行字体的设定,再对字体进行显示和移动,否则会按照默认的字体格式类显示字体
g.setColor(new Color(100,100,200));
g.drawString(message, xCoordinate, yCoordinate);//怎么改变字体的大小和颜色?
//repaint();
}
class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
repaint();//每次重新绘制时paintComponent会重新绘制一遍,而每一次调用都会改变xCoordinate,这就实现了字符的移动
}
}
}
}
如果把这段代码注释的repaint(),去掉会发现LOVE很快的移动,如果去掉repaing()就会以比较慢的速度移动?可以我已经设置了定时器了啊。 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class java16_11 extends JFrame
{
public java16_11()
{
add(new MovingMessagePanel("LOVE"));
}
public static void main(String[] args)
{
JFrame frame=new java16_11();
frame.setTitle("java16_11");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class MovingMessagePanel extends JPanel
{
private String message="LOVE";
private int xCoordinate=0;
private int yCoordinate=20;
public MovingMessagePanel(String message)
{
this.message=message;
Timer timer=new Timer(100,new TimerListener());//创建计时器,100ms是周期
timer.start();//启动定时器
}
protected void paintComponent(Graphics g)//显示出string
{
super.paintComponent(g);
if(xCoordinate>getWidth())
{
xCoordinate=0;
}
xCoordinate+=5;
g.setFont(new Font("TimesRoman",Font.PLAIN,24));//应该先进行字体的设定,再对字体进行显示和移动,否则会按照默认的字体格式类显示字体
g.setColor(new Color(100,100,200));
g.drawString(message, xCoordinate, yCoordinate);//怎么改变字体的大小和颜色?
//repaint();
}
class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
repaint();//每次重新绘制时paintComponent会重新绘制一遍,而每一次调用都会改变xCoordinate,这就实现了字符的移动
}
}
}
}
如果把这段代码注释的repaint(),去掉会发现LOVE很快的移动,如果去掉repaing()就会以比较慢的速度移动?可以我已经设置了定时器了啊。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询