gpt4 book ai didi

java - 如何添加到以前的 JLabel

转载 作者:行者123 更新时间:2023-11-30 06:45:24 24 4
gpt4 key购买 nike

我已经更改了代码,因此解释起来可能会更简单一些,这就是我现在试图解决的最初问题。我首先创建一个 JLabel,然后尝试通过添加新行然后添加更多文本来更新其中包含的文本。我看过类似的问题,但解决方案没有帮助。这是一个时间表应用程序。

private void createAndShowGUI(String [] lessons) {

...

Container pane = frame.getContentPane();
GridBagConstraints c = new GridBagConstraints();
...

label1 = new JLabel("");
label1.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 2;
c.gridy = 2;
pane.add(label1, c);

for(int i=0; i<lessons.length; i++) {
String format = lessons[i];
String [] t = format.split(",");
if (t[0].equals("1")) {
if (t[1].equals("1")) {
label1.setText(label1.getText() + "lecture: " + t[3] + " unit: " + t[4] + " room: " + t[2]);

...

现在,如果我不更改其他地方写入的变量,我的输入将保持不变。我当前的输入是 Lessons[0] = "1,1,1,taqi,maths,bob", Lessons[1] = "1,1,2,john,physicals,jim"还有其他一些,但它们不相关,因为它们最终都会处于相同的情况。

在 label1.setText 中,我尝试在 label1.getText()“lecture:” 之间添加新行。这样,当我运行代码时,我有一个讲座、单元和房间,下面我有另一个讲座、单元和房间。现在为了更清楚地了解我的输出,t[0] 是讲座的日期,t[1] 是讲座的时间。我正在尝试在同一时间段和同一天在不同的房间中显示多个类(class)。

最佳答案

JLabel 无法识别换行符。文本显示在单行上,并且忽略换行符。

您可以在 JLabel 中使用 HTML:

label.setText("<html>line1<br>line2</html");

或者您可以使用 JTextArea 并附加多行文本:

textArea.append("\nline2");

关于java - 如何添加到以前的 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com