在JLabel而不是控制台上显示状态消息

时间:2018-01-02 23:45:05

标签: java swing jlabel

我需要打印我的游戏状态消息,例如玩家'转身,球员'使用JLabel或类似的东西得分。我可以在控制台(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)

通过上述内容,我可以在控制台中打印出类似下面的内容:

data = testInstance.convert(r"/Users/PycharmProjects/Dane/{0}/{1}/{2}".format(Database, Station, Date), "{3}", conversionError).format(Time)

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

2 个答案:

答案 0 :(得分:2)

您只需将JLabel放置在您希望的位置,然后将来电System.out.println("...");替换为JLabel#setText(...)

例如:

myLoggingLabel.setText("Path : ");

您可以将其初始化为:

JLabel myLoggingLabel = new JLabel("");

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

您还可以在Swing Timer

之后使用tutorial在特定时间后隐藏事件

答案 1 :(得分:0)

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

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