我正在创建一个简短的程序,其中涉及访客和选项卡式 Pane 。我想对 JTabbedPane 进行编程,以便当我单击某个选项卡时,将使用 JComponent makeTextPanel 函数显示访客的信息。但是,它在运行时似乎忽略了“\n”(新行)。有什么办法可以解决这个问题吗?
这是我的 GUI:
JTabbedPane overview= new JTabbedPane();
JComponent accountinfo= makeTextPanel (guest.toString());
overview.addTab ("Account Overview", accountinfo);
overview.setMnemonicAt(0, KeyEvent.VK_1);
JFrame tabbed= new JFrame("AIR Reservation");
tabbed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbed.add(overview);
tabbed.setSize(500,300);
tabbed.setLocationRelativeTo(null);
tabbed.setVisible(true);
这是我的 Customer 类的 toString 方法:
public String toString()
{
customerstring= "Name: " + getName();
customerstring+= "\nGender: " + getGender();
customerstring+= "\nDate of Birth: " + getBirthDate();
customerstring+= "\nPassport number: " + getPassportNumber();
customerstring+= "\nBalance: " + getMoney();
return customerstring;
}
感谢您的帮助! 编辑:这是 makeTextPanel 方法:
protected JComponent makeTextPanel(String text)
{
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
最佳答案
您的面板包含JLabel
其中包含您在 makeTextPanel
中传递的文本作为一个论点。一个JLabel
如果文本是 html 格式,则可以有多行。
所以你的文本应该封装在 <html></html>
内行分隔符应为 <br>
.
其他选项是使用 JTextArea
或JEditorPane
而不是JLabel
。如果需要,您可以将它们设置为不可编辑。
关于java - 如何在 JTabbedPane JComponent 面板中创建新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15818201/