各位大侠好:
我在做一个Swing程序的时候,想完成这一一个功能:在 JPanel上动态显示多行文本,显示一行后,各2秒左右显示下一行。但是下面有问题,只能显示两行:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextPanel extends JPanel implements Runnable
{
String seq="12345678901234567890123456789012345678901234567890ABCDEFGHI";
public static int index=0;
public TextPanel()
{
System.out.println("TextPanel is Constructed");
this.setBackground(Color.BLACK);
Dimension size=new Dimension(800,500);
this.setPreferredSize(size);
}
public void paintComponents(Graphics g1)
{
super.paintComponents(g1);
}
public void paint(Graphics g)
{
super.paint(g);
// Graphics2D g=(Graphics2D)g1;
Font seqFont = new Font("Courier New", Font.BOLD, 16);
g.setFont(seqFont);
g.setColor(Color.WHITE);
System.out.println(index+" In paint");
g.drawString(seq,2,15*index+20);
}
public void run()
{
try{
for(int i=0;i<5;i++) //在这里我想显示5行,但只有最后一次循环调用了repaint ?
{
index++;
Rectangle rec=new Rectangle(20,20*(index+1),700,20);
repaint(rec);
}
}catch(Exception err){err.printStackTrace();}
}
}import java.lang.Runnable;
public class T1 extends JFrame
{
TextPanel p=null;
public T1()
{
setTitle("Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Toolkit toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
this.setSize(size.width-50,size.height-100);
setLocation(size.width/2 - getWidth()/2,
size.height/2 - getHeight()/2);
p=new TextPanel();
this.getContentPane().setLayout(new FlowLayout());
JButton btn=new JButton("OK");
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Thread t=new Thread(p);
t.start();
//t.run();
}
}
);
this.add(p);
this.add(btn); }
public static void main(String [] args)
{
T1 obj=new T1();
obj.setVisible(true);
}
}请各位帮忙,还可以另开贴给分!
我在做一个Swing程序的时候,想完成这一一个功能:在 JPanel上动态显示多行文本,显示一行后,各2秒左右显示下一行。但是下面有问题,只能显示两行:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextPanel extends JPanel implements Runnable
{
String seq="12345678901234567890123456789012345678901234567890ABCDEFGHI";
public static int index=0;
public TextPanel()
{
System.out.println("TextPanel is Constructed");
this.setBackground(Color.BLACK);
Dimension size=new Dimension(800,500);
this.setPreferredSize(size);
}
public void paintComponents(Graphics g1)
{
super.paintComponents(g1);
}
public void paint(Graphics g)
{
super.paint(g);
// Graphics2D g=(Graphics2D)g1;
Font seqFont = new Font("Courier New", Font.BOLD, 16);
g.setFont(seqFont);
g.setColor(Color.WHITE);
System.out.println(index+" In paint");
g.drawString(seq,2,15*index+20);
}
public void run()
{
try{
for(int i=0;i<5;i++) //在这里我想显示5行,但只有最后一次循环调用了repaint ?
{
index++;
Rectangle rec=new Rectangle(20,20*(index+1),700,20);
repaint(rec);
}
}catch(Exception err){err.printStackTrace();}
}
}import java.lang.Runnable;
public class T1 extends JFrame
{
TextPanel p=null;
public T1()
{
setTitle("Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Toolkit toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
this.setSize(size.width-50,size.height-100);
setLocation(size.width/2 - getWidth()/2,
size.height/2 - getHeight()/2);
p=new TextPanel();
this.getContentPane().setLayout(new FlowLayout());
JButton btn=new JButton("OK");
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Thread t=new Thread(p);
t.start();
//t.run();
}
}
);
this.add(p);
this.add(btn); }
public static void main(String [] args)
{
T1 obj=new T1();
obj.setVisible(true);
}
}请各位帮忙,还可以另开贴给分!
解决方案 »
- Java的switch语句支持byte、short、char、int、long、String和enum(枚举)吗?
- StringBuffer sb = new StringBuffer("a"),问这句话创建了几个对象,不算引用
- 请问能不能得到从List中取出的Object对象的类型?
- 倒序输出
- 如何设置树状结构的外观?
- java连接sql server2000数据库最好的方式是什么?
- 这两个程序为什么会有区别..?
- 请问哪里下载 Eclipse 的多国语言包?
- 送分题
- 在JFC中,有沒有類可以像MFC中的Listcontrol的功能一樣
- 判断出栈序列是否合法?
- Java 连接到一台通过代理上网的电脑
import java.awt.Graphics;
import java.awt.Insets;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.JFrame;public class Demo extends JFrame{ private static int DELAY = 300; Insets insets;
String seq = "12345678901234567890123456789012345678901234567890ABCDEFGHI";
public static int index = 0;
Color colors[] = {Color.RED,Color.ORANGE,Color.YELLOW,Color.GREEN,Color.BLUE,Color.MAGENTA}; public void paint(Graphics g){
super.paint(g);
if(insets == null){
insets = getInsets();
}
synchronized(colors){
for(int i = 0;i < colors.length;i++){
g.setColor(colors[i]);
g.drawString(seq,3, 10 * index +20 );
if(index > 50)
index = 0;
}
}
} public void go(){
TimerTask task = new TimerTask() {
public void run(){
Color c = colors[0];
synchronized(colors){
System.arraycopy(colors,1,colors,0,colors.length - 1);
colors[colors.length - 1] = c;
index ++ ;
}
repaint();
}
};
Timer timer = new Timer();
timer.schedule(task,0,DELAY);
} public static void main(String args[]){
Demo f = new Demo();
f.setBounds(300,100,600,500);
f.setVisible(true);
f.go();
}
}
Thread成了同时执行了