English 中文(简体)
利用时间在阵列展示优势
原标题:Using Timer to display Strings in a string array

我正试图制造一个小刀,通过时间交换文字信息。 目前,我曾尝试在行动听众中 lo,但没有成功。

import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import java.awt.Rectangle;

public class TimerX extends JPanel{

 private static final long serialVersionUID = 1L;
 private JLabel jLabelNumber = null;
 private Timer timer = new Timer(100, null);
 private String [] messages = new String [4];{
  messages [0] = "HI";
  messages [1] = "How";
  messages [2] = "Are";
  messages [3] = "You";
 }


 public TimerX() {
  super();
  initialize();
 }


 private void initialize() {
  jLabelNumber = new JLabel();
  jLabelNumber.setBounds(new Rectangle(99, 85, 38, 16));
  this.setSize(300, 200);
  this.setLayout(null);
  this.add(jLabelNumber, null);
  ActionListener updater = new ActionListener(){
  public void actionPerformed(ActionEvent event) {
   for(int i = 0; i<messages.length;i++)
        jLabelNumber.setText(messages[i]);
     }
  };
  timer.addActionListener(updater);
  timer.start(); 
 }

}

该守则只显示阵列中最后的强势。 帮助 T.T

最佳回答

每当你更新的《生命之行动》时,它都会通过所有信息传递,给每个信息贴上标签。 在您的更新后,最后一个信息在标签上显示。 情况如此之快,你看不到头三条信息,然后才写完。

你们需要一个能够记住自己应该下台的信息的行动手段。

ActionListener updater = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    jLabelNumber.setText(messages[i]);

    i++;
    if (i >= messages.length) {
      i = 0;
    }
  }

  private int i = 0;
};
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签