简体   繁体   English

在“ for”循环中设置JLabel的文本

[英]Setting text of a JLabel in a 'for' loop

I do have a problem setting text of a JLabel from a for loop. 我确实在从for循环设置JLabel文本时遇到问题。 The loopMessage() method just setting the first index of the list, but when I print out I can see all indexes: loopMessage()方法只是设置列表的第一个索引,但是当我打印出来时,我可以看到所有索引:

meep spring ep春
meep2 winter meep2冬天

I want the label olso setting the whole list on the window 我想要标签olso在窗口上设置整个列表

public class ControllerMessage {
    private ModelMessage mm;
    private ViewMessage vm;

    public ControllerMessage(ModelMessage mm, ViewMessage vm) {
        this.mm = mm;
        this.vm = vm;
        loopMessage();
        addMessage();
        loopMessage();
    }

    public void loopMessage() {
        for (Message s : mm.getAllMessages()) {
            System.out.println(s.getName() + " " + s.getDate());
            vm.setLabel(s.getName() + " " + s.getDate());   
        }
    }

    public Message addMessage() {
        return this.mm.addMessage(new Message(2, "meep2", "winter"));
    }
}

the view class: 视图类:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ViewMessage extends JFrame{

private JLabel additionLabel = new JLabel();

public ViewMessage() {

    JPanel calcPanel = new JPanel();

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600, 600);


    calcPanel.add(additionLabel);

    this.add(calcPanel);
 }

 public void setLabel(String m){
    additionLabel.setText(m);
}
}

A JLabel can only hold a String, usually a single line of text. JLabel只能包含一个字符串,通常是一行文本。 If you want to display text in a loop, but show it for a defined period of time, and then show the next text -- use a Swing Timer. 如果要循环显示文本,但要在定义的时间段内显示文本,然后显示下一个文本,请使用Swing计时器。 This will save you from having to explicitly create a background thread requiring you to take care that all Swing code calls in the background thread be queued onto the event thread, since the Timer's ActionListener code is guaranteed to be called on the Swing event thread. 这将使您不必显式创建后台线程,而需要确保将后台线程中的所有Swing代码调用都排队到事件线程中,因为可以确保在Swing事件线程上调用Timer的ActionListener代码。

If you want to show multiple lines of text, use a JTextArea or a JList. 如果要显示多行文本,请使用JTextArea或JList。 My guess is that you really want to use a JList. 我的猜测是您确实要使用JList。

eg, 例如,

// better to extend JPanel than JFrame, since this makes your code more flexible.
public class ViewMessage extends JPanel {
   private static final int LIST_WIDTH = 40;
   private static final int VISIBLE_ROWS = 20;
   private DefaultListModel<String> listModel = new DefaultListModel<>();
   private JList<String> messageList = new JList<>(listModel);
   private JLabel additionLabel = new JLabel();

   public ViewMessage() {
      // set the width of the JList
      String listWidth = String.valueOf(LIST_WIDTH);
      String prototypeValue = String.format("%" + listWidth + "s", " ");
      messageList.setPrototypeCellValue(prototypeValue);

      // set the number of JList rows visible in the scrollpane 
      messageList.setVisibleRowCount(VISIBLE_ROWS);

      setLayout(new BorderLayout());
      add(new JScrollPane(messageList));
   }

   public void appendMessage(String message) {
      listModel.addElement(message);
   }

}

Since there is no delay, the JLabel is being changed with all the messages, but it is so fast that it is not visible. 由于没有延迟,因此将更改所有消息的JLabel,但是它是如此之快以至于它不可见。 To fix this, create a thread and make the thread sleep with Thread.sleep(time) . 要解决此问题,请创建一个线程,并使用Thread.sleep(time)使该线程进入睡眠状态。


If you want to print the entire list of messages do this for changing the JLabel: label.setText(label.getText() + " " + messageYouWantToAdd); 如果要打印整个消息列表,请执行以下操作以更改JLabel:label.setText(label.getText()+“” + messageYouWantToAdd);

I do not know what you view message class is so make that code where ever you are adding text to your label. 我不知道您所查看的消息类是什么,因此无论您在何处添加文本到标签,都可以使用该代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM