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

时间:2012-08-05 17:18:30

标签: java swing loops jlabel imageicon

我需要为背景留出空间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) {
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用

setIcon(Icon icon)

JLabel类的方法。

答案 1 :(得分:1)

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

xlabel.setIcon(new ImageIcon(yourImage));