如何在循环中添加带有背景图像的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) {
    }
}

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

xlabel.setIcon(new ImageIcon(yourImage));

您可以使用

setIcon(Icon icon)

JLabel类的方法。

最新更新