简体   繁体   English

如何在 jLabel 中应用新行?

[英]How can I apply new line in jLabel?

I want to display values from database in many lines using jLabel.我想使用 jLabel 在多行中显示数据库中的值。 I tried using the html trick but I don't know how to apply it here in my code.我尝试使用 html 技巧,但我不知道如何在我的代码中应用它。 I just get errors.我只是得到错误。 Maybe I'm doing it the wrong way.也许我做错了。 Haha.哈哈。

     try{
            //display doctor's name by selected classification
            String url = "jdbc:mysql://localhost/sched";
            Connection conn = DriverManager.getConnection(url,"root","");
            Statement stmt = conn.createStatement();
            String classification = comboClass.getSelectedItem().toString();
            String sqlSelect= "select * from doctorsched where class = '"+classification+"'";
            ResultSet rs = stmt.executeQuery(sqlSelect); 
            String finalText ="";
        while(rs.next()){
             String docsName= rs.getString("docName");
             String room = rs.getString("room");
             String days = rs.getString("day");
             String from = rs.getString("timefrom");
             String to = rs.getString("timeto");
             
             finalText += docsName+" (room "+room+", "+days+", "+from+"-"+to+") \\n";
             // i want to display values from database in many lines but using jLabel
             
        }
        jLabel10.setText(finalText);
      
} catch (Exception ex) {
  
    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}

Something like this should be my output像这样的应该是我的 output

doc riza (room 104, Every Thursday, 10:30AM-10:00AM)
doc j (room 101, null, 10:30AM-11:30AM)
doc lea (room 102, Every Saturday, 10:30AM-4:30PM)
frida (room 101, null, 8:00AM-9:30AM)

Please help me out:( The '\n' works in jTextArea but not in jLabel. I also tried '\n' in label, but doesn't work, too.请帮帮我:( '\n' 在 jTextArea 中有效,但在 jLabel 中无效。我也在 label 中尝试了 '\n',但也不起作用。

Okay so I tried this one好吧,我试过这个

finalText += "<html>"+docsName+" (room "+room+", "+days+", "+from+"-"+to+")<br></html>";

But this code only display one line.但是这段代码只显示一行。 The first row in my database.我数据库中的第一行。 I need to display all of the rows.我需要显示所有行。

Now, this next code shows it all, but the next line still doesn't work.现在,下一段代码显示了所有内容,但下一行仍然不起作用。

while(rs.next()){
             String docsName= rs.getString("docName");
             String room = rs.getString("room");
             String days = rs.getString("day");
             String from = rs.getString("timefrom");
             String to = rs.getString("timeto");
             
             finalText += docsName+" (room "+room+", "+days+", "+from+"-"+to+")\n";
        }
        jLabel10.setText("<html>"+finalText+"<br></html>");
} 

Whyyy为什么啊

use a code like this使用这样的代码

String finalText = "";
for (int i = 0; i < 10; i++) {
    finalText += "line:" + i;
    finalText += "<br>";
}
label.setText("<html>" + finalText + "</html>");
 try{
            //display doctor's name by selected classification
            String url = "jdbc:mysql://localhost/sched";
            Connection conn = DriverManager.getConnection(url,"root","");
            Statement stmt = conn.createStatement();
            String classification = comboClass.getSelectedItem().toString();
            String sqlSelect= "select * from doctorsched where class = '"+classification+"'";
            ResultSet rs = stmt.executeQuery(sqlSelect); 
            String finalText ="";

        while(rs.next()){
             String docsName= rs.getString("docName");
             String room = rs.getString("room");
             String days = rs.getString("day");
             String from = rs.getString("timefrom");
             String to = rs.getString("timeto");

             finalText += (docsName+" (room "+room+", "+days+", "+from+"-"+to+")\n") ;
             finalText += "<br>";
               
        }
         jLabel10.setText("<html>" + finalText + "</html>");

} catch (Exception ex) {
  
    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}

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

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