如何在jlabel中应用新行?

2sbarzqh  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(406)

我想使用jlabel在许多行中显示来自数据库的值。我试着使用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";
             // 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);
}

像这样的东西应该是我的输出

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)

请帮帮我:“\n”在jtextarea中有效,但在jlabel中无效。我也在label中尝试了'\n',但也不起作用。
好吧,我试过这个

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

但是这个代码只显示一行。我数据库的第一行。我需要显示所有行。
现在,下一行代码显示了这一切,但下一行仍然不起作用。

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公司

xoshrz7s

xoshrz7s1#

使用这样的代码

String finalText = "";
for (int i = 0; i < 10; i++) {
    finalText += "line:" + i;
    finalText += "<br>";
}
label.setText("<html>" + finalText + "</html>");
n7taea2i

n7taea2i2#

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);
}

相关问题