我正在创建一个涉及来宾和选项卡式窗格的简短程序。我想对 JTabbedPane 进行编程,因此当我单击某个选项卡时,将使用 JComponent makeTextPanel 函数显示来宾的信息。但是,它在运行时似乎忽略了 '\n' (新行)。有没有什么办法解决这一问题?
这是我的图形用户界面:
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;
}