在java中插入换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20706206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert line break in java
提问by Fahmi Jufri
I have a problem with line breaks, I have made a code like this
我有换行符的问题,我做了这样的代码
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Welcome extends JFrame {
JMenu menuAdd, menuShow, menuEdit, menuHelp;
JMenuItem addCar, addRent, addUser, editCar, editCostumer, editUser, showCar, showRent, helpAbout;
JLabel lblWelcome;
JMenuBar mb;
public Welcome() {
setTitle("Car Tooner");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,400);
setLocationRelativeTo(null);
setResizable(false);
menuAdd = new JMenu("Add");
menuShow = new JMenu("Show");
menuEdit = new JMenu("Edit");
menuHelp = new JMenu("Help");
addCar = new JMenuItem("Car");
addRent = new JMenuItem("Rent");
addUser = new JMenuItem("User");
editCar = new JMenuItem("Car");
editCostumer = new JMenuItem("Costumer");
editUser = new JMenuItem("User");
showCar = new JMenuItem("Car");
showRent = new JMenuItem("Rent");
helpAbout = new JMenuItem("About");
mb = new JMenuBar();
lblWelcome = new JLabel("Welcome, Admin \nto Car Tooner", SwingConstants.CENTER);
setJMenuBar(mb);
mb.add(menuAdd);
mb.add(menuEdit);
mb.add(menuShow);
mb.add(menuHelp);
menuAdd.add(addCar);
menuAdd.add(addRent);
menuAdd.add(addUser);
menuEdit.add(editCar);
menuEdit.add(editCostumer);
menuEdit.add(editUser);
menuShow.add(showCar);
menuShow.add(showRent);
menuHelp.add(helpAbout);
add(lblWelcome);
}
public static void main(String[] args) {
Welcome wlc = new Welcome();
wlc.setVisible(true);
}
}
but when I run the program, line break can not be executed,
但是当我运行程序时,不能执行换行符,
lblWelcome = new JLabel("Welcome, Admin \nto Car Tooner", SwingConstants.CENTER);
i want to make text like this "Welcome, admin (\n new line) to Car Tooner" can anyone help me?
我想制作这样的文字“欢迎,管理员(\n 换行)到 Car Tooner” 任何人都可以帮助我吗?
采纳答案by Elliott Frisch
回答by mKorbel
i want to make text like this "Welcome, admin (\n new line) to Car Tooner" can anyone help me?
我想制作这样的文字“欢迎,管理员(\n 换行)到 Car Tooner” 任何人都可以帮助我吗?
use JTextAreain JScrollPaneinstead of JLabel
回答by Adam Short
public static final String NL = System.getProperty("line.separator");
You need to get line separator so it works cross platform as \n does not always work. \r\n is the correct way to do it in Windows for example. Just write the line.separator into a variable and add it any time you need it. This is why static is useful as it will be persistant across all instances of the class.
您需要获得行分隔符,以便跨平台工作,因为 \n 并不总是有效。\r\n 是例如在 Windows 中执行此操作的正确方法。只需将 line.separator 写入变量并在需要时随时添加。这就是为什么 static 很有用,因为它将在类的所有实例中持久存在。
To build it in:
构建它:
lblWelcome = new JLabel("Welcome, Admin" + NL + "to Car Tooner", SwingConstants.CENTER);
回答by gaurav5430
Surround the string with <html></html>
and break the lines with <br>
.
用 环绕字符串<html></html>
并用 断开线条<br>
。
JLabel l = new JLabel("<html>Welcome admin <br>to car Toner</html>", SwingConstants.CENTER);
also see here for an extended discussion
另请参阅此处进行扩展讨论