Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在jLabel中应用新行?_Java_Mysql_Swing_Newline_Jlabel - Fatal编程技术网

Java 如何在jLabel中应用新行?

Java 如何在jLabel中应用新行?,java,mysql,swing,newline,jlabel,Java,Mysql,Swing,Newline,Jlabel,我希望使用jLabel以多行形式显示数据库中的值。我尝试使用html技巧,但我不知道如何在我的代码中应用它。我只是犯了一些错误。也许我做得不对。哈哈 try{ //display doctor's name by selected classification String url = "jdbc:mysql://localhost/sched"; Connection conn = Drive

我希望使用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>";
finalText+=“+docsName+”(房间“+room+”,“+days+”,“+from+”-“+to+”)
”;
但这段代码只显示一行。我数据库中的第一行。我需要显示所有行

现在,下面的代码显示了这一切,但下一行仍然不起作用

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>");
} 
while(rs.next()){
String docsName=rs.getString(“docName”);
字符串房间=rs.getString(“房间”);
字符串天=rs.getString(“天”);
stringfrom=rs.getString(“timefrom”);
String to=rs.getString(“timeto”);
finalText+=docsName+“(房间“+房间+”,“+天+”,“+从+”-“+到+”)\n”;
}
jLabel10.setText(“+finalText+”
”); }
Whyyy

使用这样的代码

String finalText = "";
for (int i = 0; i < 10; i++) {
    finalText += "line:" + i;
    finalText += "<br>";
}
label.setText("<html>" + finalText + "</html>");
String finalText=“”;
对于(int i=0;i<10;i++){
finalText+=“行:”+i;
finalText+=“
”; } label.setText(“+finalText+”);
试试看{
//按所选分类显示医生姓名
String url=“jdbc:mysql://localhost/sched";
连接conn=DriverManager.getConnection(url,“根”,“根”);
语句stmt=conn.createStatement();
字符串分类=comboClass.getSelectedItem().toString();
String sqlSelect=“select*from doctorsched where class=”+classification+”;
ResultSet rs=stmt.executeQuery(sqlSelect);
字符串finalText=“”;
while(rs.next()){
String docsName=rs.getString(“docName”);
字符串房间=rs.getString(“房间”);
字符串天=rs.getString(“天”);
stringfrom=rs.getString(“timefrom”);
String to=rs.getString(“timeto”);
finalText+=(docsName+“(房间“+房间+”,“+天+”,“+从+”-“+到+”)\n);
finalText+=“
”; } jLabel10.setText(“+finalText+”); }捕获(例外情况除外){ Logger.getLogger(home.class.getName()).log(Level.SEVERE,null,ex); }
使用JTextArea或像这里显示的那样使用html,好的,所以我遵循了它。finalText+=“+docsName+”(房间“+room+”,“+days+”,“+from+”-“+to+”)
”但是,它只显示了我数据库中的第一行。谢谢!我已经完成了它而不使用循环tho。但是我在循环中遵循了你的代码。没问题,请考虑接受我的答案,这样别人就不会花时间去回答你已经回答的问题。
 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);
}