在JLabel上显示状态消息而不是控制台[重复]

问题描述 投票:1回答:2

这个问题在这里已有答案:

我需要打印我的游戏状态消息,例如玩家的回合,玩家使用JLabel的分数或类似的东西。我可以在控制台(System.out.print())上这样做。

基本上,我有

JLabel statusBar, playerA, playerB; // Where I need to display scores and a message saying where each player have put their seeds on the grid
JPanel mainPanel; //Which holds the grid
JPanel leftPanel; // On which the status bar and other components are put

我可以在控制台中显示以下内容:

System.out.print("Path : " );
        for(Cell cell : this) {
            System.out.print(cell);
        }
        System.out.println("");

        List<Cell> capturedSeeds = getCapturedWithIn();     

        if(capturedSeeds.size() <= 1) {
            System.out.print(capturedSeeds.size() + " " + "seed captured at: " );
            for(Cell cell : capturedSeeds) {
            System.out.print(cell);
            }
            System.out.println("");
        } else
            if(capturedSeeds.size() >= 2) {
        System.out.print(capturedSeeds.size() + " " + " seeds captured at: " );
        for(Cell cell : capturedSeeds) {
            System.out.print(cell);
        }
        System.out.println("");

        }

有了上面的内容,我可以在控制台中打印出类似下面的东西:

>> PION_ROUGE placed at (10,15)
 >> PION_BLEU placed at (11,15)
 >> PION_ROUGE placed at (11,16)
 >> PION_BLEU placed at (10,16)
 >> PION_ROUGE placed at (12,15)
 >> PION_BLEU placed at (12,16)

Path : [11, 14]-PION_ROUGE[10, 15]-PION_ROUGE[11, 16]-PION_ROUGE[12, 15]-PION_ROUGE
1 seed captured at: [11, 15]-PION_BLEU

 >> PION_BLEU placed at (11,17)
 >> PION_ROUGE placed at (19,16)
 >> PION_BLEU placed at (16,14)
 >> PION_ROUGE placed at (17,14)

我根本没有经验,这个问题可能很愚蠢,但请耐心等待。可以随意询问任何澄清。

java swing jlabel
2个回答
2
投票

你只需要在任何你想要的地方放置一个JLabel,然后将调用System.out.println("...");替换为JLabel#setText(...)

例如:

myLoggingLabel.setText("Path : ");

您可以将其初始化为:

JLabel myLoggingLabel = new JLabel("");

所以它看起来像一个“看不见的”标签。

您还可以使用Swing Timer之后的tutorial在特定时间后隐藏事件


0
投票

您是否尝试过使用JLabel setText()方法?

https://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html

© www.soinside.com 2019 - 2024. All rights reserved.