简体   繁体   English

如何在循环中添加带背景图像的JLabel

[英]How can I add JLabel with background image in loop

I need to make room JLabel with background. 我需要在背景中为JLabel腾出空间。 This background it's state of room just I want way to change this colors to images in loop I make a hotel booking room and some other service and I don't know how to do this 这个背景它的房间状态只是我想要把这种颜色改成循环中的图像我做了一个酒店预订房间和其他一些服务,我不知道该怎么做

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

You can use 您可以使用

setIcon(Icon icon)

method of JLabel class. JLabel类的方法。

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

xlabel.setIcon(new ImageIcon(yourImage));

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

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