简体   繁体   English

如何在JTabbedPane JComponent面板中创建新行?

[英]How can I create a new line within a JTabbedPane JComponent Panel?

I'm creating a short program that involves a guest and a tabbed pane. 我正在创建一个包含来宾和选项卡式窗格的简短程序。 I want to program the JTabbedPane so when I click on a certain tab, the guest's information will be displayed using the JComponent makeTextPanel function. 我想对JTabbedPane进行编程,因此当我单击某个选项卡时,将使用JComponent makeTextPanel函数显示来宾的信息。 However, it seems to ignore the '\\n' (new line) when it runs. 但是,它似乎在运行时会忽略“ \\ n”(换行)。 Is there any way to fix this? 有没有什么办法解决这一问题?

This is my GUI: 这是我的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);    

This is the toString method for my Customer class: 这是我的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;

}

Thanks for the help! 谢谢您的帮助! Edit: here's the makeTextPanel method: 编辑:这是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;
 }

Your panel contains a JLabel that has as a text the one you pass in the makeTextPanel as an argument. 您的面板包含一个JLabel ,该JLabel具有您在makeTextPanel传递的参数作为文本。 A JLabel can have multiple lines if the text is in html format. 如果文本为html格式,则JLabel可以有多行。

So your text should be encapsulated inside <html></html> and the line separator should be <br> . 因此,您的文本应封装在<html></html> ,而行分隔符应为<br>

Other option would be to use a JTextArea or a JEditorPane instead of JLabel . 另一种选择是使用JTextAreaJEditorPane代替JLabel You can set them non-editable, if needed. 如果需要,可以将它们设置为不可编辑。

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

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