2012-08-05 71 views
0

我需要使用带背景的空间JLabel。这种背景下它是 聊天室状态正是我想要的方式来改变这种颜色,图像,循环 我做酒店预订房间和其他一些服务,我不知道如何做到这一点如何在循环中添加带有背景图像的JLabel

private void checkstatus() { 
    jPanel2.removeAll(); 
    int dx = 1; 
    ResultSet Rroom; 
    Rroom = DB.RunQuery("Select * From rooms"); 

    final ImageIcon imageIcon = new ImageIcon("c://Des.jpg"); 

    try { 
     while(Rroom.next()) { 
      String Rstatus = Rroom.getString(3); 
      rou+=40; 

      Label xlabel; 
      xlabel = new Label("Label 1",Label.CENTER); 
      Font bigFont = new Font("SanSerif", Font.BOLD, 11); 
      xlabel.setFont(bigFont); 

      if(Rstatus.equals("Busy")) 
       xlabel.setBackground(Color.red); 
      else if (Rstatus.equals("Test")) 
       xlabel.setBackground(Color.CYAN); 
      else 
       xlabel.setBackground(Color.green); 

      jPanel2.add(xlabel); 
      xlabel.setForeground(Color.black); 
      xlabel.setBounds(new Rectangle(rou, rou2, 35, 35)); 
      xlabel.setText("1"); 

      if(rou==365) { 
       rou=-35; 
       // El 2rtfa3 
       rou2 +=40; 
      } 

      dx++; 
     } // end of loop 

     rou = -35; // for new check 
     rou2=10; // for new check 

    }// try 
    catch (SQLException e) { 
    } 
} 
+0

当您发布代码时,请确保它使用一致的格式正确缩进!这使我们更容易阅读,因此可以帮助您。 – Jamie 2012-08-05 17:37:29

回答

1

要显示带有图标的JLabel,您应该使用JLabel#setIcon(Icon)

xlabel.setIcon(new ImageIcon(yourImage)); 
+0

错误(635,19):无法找到方法setIcon(javax.swing.ImageIcon) – 2012-08-05 17:34:33

+0

再次查看您的代码后,我注意到您使用Label而不是JLabel。我回答了你的问题,这是关于JLabel的问题。你实际使用哪个?如果你实际上使用java.awt.Label,那将会导致没有setIcon方法出现。要将图像添加到awt Label,您需要做更多的工作,所以我建议尽可能使用JLabel。 – Vulcan 2012-08-05 17:42:00

+0

你是对的,但请重新编码它和过去 – 2012-08-05 17:52:12

1

您可以使用

setIcon(Icon icon) 

JLabel类的方法。