简体   繁体   English

如何在Java中向文本添加新行?

[英]How to add a new line to text in Java?

I am creating a game of checkers in Java for my A2 Computing coursework, I have recently working on the GUI of the Checkerboard, of which I wish to add in a menu bar, I have been able to achieve this as can be seen in my code, however for one of the menu items, known as Rules, I wish to add a couple of the basic rules within the text, this occurs on the line: 我正在为我的A2 Computing课程创建一个用Java编写的棋盘游戏,我最近在Checkerboard的GUI上工作,我希望在菜单栏中添加它,我已经能够实现这一点。代码,但是对于菜单项之一(称为“规则”),我希望在文本中添加几个基本规则,这在行上发生:
JMenuItem RulesText = new JMenuItem("")

Within this I wish to input the text for the rules, I have inputted some of these already has can be seen. 在此我希望输入规则的文字,我已经输入了其中一些已经可以看到的文字。

My problem is that, they are all on one line, so when I run the application, it appears as "Moves are always only diagonal. Single pieces are always limited to forward moves (toward the opponent). Kings are limited to moving diagonally, but may move both forward and backward". 我的问题是,它们全都在一条直线上,所以当我运行该应用程序时,它显示为“移动始终始终仅是对角线。单块始终限于向前移动(朝对手)。国王仅限于对角移动,但可以向前和向后移动”。 The problem is that this is not the format that I want it to be present in, I want it be present in a format similar to this 问题是这不是我想要的格式,我希望它的格式类似于

  • Moves are always only diagonal 动作总是对角线
  • Single pieces are always limited to forward moves (toward the opponent) ect...... 单件总是限于向前移动(向对手)等。

So how can I possibly achieve this, I have attempted to research this I couldn't find anything in my format, as I would prefer to not have to change the majority of the code. 因此,我如何才能实现这一目标,我试图对此进行研究,因为我不希望更改大多数代码,所以找不到任何格式的东西。 Any answers would be extremely appreciated. 任何答案将不胜感激。 Thank you. 谢谢。 My Code can be seen Below: 我的代码如下所示:

    JMenuBar bar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenu HelpMenu = new JMenu("Help");
    JMenuItem Exit = new JMenuItem("Exit");
    JMenuItem MainMenu = new JMenuItem("Main Menu");
    JMenu Rules = new JMenu("Rules of Checkers");
    JMenuItem RulesText = new JMenuItem("- Moves are always only diagonal \n Single pieces are always limited to forward moves (toward the opponent) \n Kings are limited to moving diagonally, but may move both forward and backward");
    Rules.add(RulesText);
    HelpMenu.add(Rules);
    bar.add(HelpMenu);
    fileMenu.add(MainMenu);
    fileMenu.addSeparator();
    fileMenu.add(Exit);
    HelpMenu.add(Rules);
    bar.add(fileMenu);
    bar.add(HelpMenu);
    window.setJMenuBar(bar);

Try wrapping the text in html tags and placing a <BR> tag where you want the line break. 尝试将文本包装在html标签中,并在要换行的位置放置<BR>标签。

However, I do agree with @rageandqq you should think about placing the rules in a JDialog or something similar 但是,我同意@rageandqq,您应该考虑将规则放在JDialog或类似的东西中

It doesn't look like this is possible, and is one of the limitations of using Swing to develop your application. 看起来这是不可能的,这是使用Swing开发应用程序的限制之一。 None of the constructors or behaviours on the JMenuItem seem to allow for String formatting. JMenuItem上的构造函数或行为似乎都不允许字符串格式设置。

Besides, it's not very wise nor user friendly to set an entire JMenuItem to display such a large block of text. 此外,设置整个JMenuItem来显示这么大的文本块不是很明智,也不是用户友好。
You should consider using a JDialog box or something similar to bring up a screen where you can display your formatted rules. 您应该考虑使用JDialog框或类似的东西来打开一个屏幕,您可以在其中显示格式化的规则。

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

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