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

时间:2021-12-28 11:48:19

This question already has an answer here:

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

I need to print my game status messages such as players' turns, players' scores using JLabel or something similar. I am able to do so on console (System.out.print()).

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

Basically, I have

基本上,我有

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

I can display those in the console with:

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

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("");

        }

With the above, I can print something like bellow in the console:

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

>> 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)

I'm not experienced at all, this question may be silly to ask, but please bear with me. Fell free to ask any clarification.

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

2 个解决方案

#1


2  

You just need to place a JLabel wherever you want it to be, then substitute the calls to System.out.println("..."); to JLabel#setText(...).

您只需将JLabel放在任何您想要的位置,然后将调用替换为System.out.println(“...”);到JLabel#setText(...)。

For example:

myLoggingLabel.setText("Path : ");

And you could initialize it as:

你可以将它初始化为:

JLabel myLoggingLabel = new JLabel("");

So it looks like an "invisible" label.

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

You can also hide the events after certain time using a Swing Timer following the tutorial

您也可以在教程后使用Swing Timer在一定时间后隐藏事件

#2


0  

Have you tried using JLabel setText() method?

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

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

#1


2  

You just need to place a JLabel wherever you want it to be, then substitute the calls to System.out.println("..."); to JLabel#setText(...).

您只需将JLabel放在任何您想要的位置,然后将调用替换为System.out.println(“...”);到JLabel#setText(...)。

For example:

myLoggingLabel.setText("Path : ");

And you could initialize it as:

你可以将它初始化为:

JLabel myLoggingLabel = new JLabel("");

So it looks like an "invisible" label.

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

You can also hide the events after certain time using a Swing Timer following the tutorial

您也可以在教程后使用Swing Timer在一定时间后隐藏事件

#2


0  

Have you tried using JLabel setText() method?

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

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