简体   繁体   English

如何将ImageIcon添加到JLabel

[英]How to add ImageIcon to a JLabel

My first project in java is Deal or No Deal and I am stack at adding a picture to a JLabel which is in the for loop. 我在Java中的第一个项目是Deal或No Deal,我正打算将图片添加到for循环的JLabel中。 Here is my code so far: 到目前为止,这是我的代码:

import javax.swing.*;

import java.awt.*;

import javax.swing.border.Border;
import javax.swing.text.html.ImageView;
import javax.swing.ImageIcon;
/**import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;**/

public class Screen extends JFrame {

    public Screen(){

            //sağ ve sol panel oluşturuldu layoutları belirlendi
            JPanel sagPanel= new JPanel();
            sagPanel.setLayout(new GridLayout(13,1));
            sagPanel.setBorder(BorderFactory.createLineBorder(Color.black));
            JPanel solPanel= new JPanel();
            solPanel.setLayout(new GridLayout(13,1));
            solPanel.setBorder(BorderFactory.createLineBorder(Color.black));



            //orta panel oluşturuldu ve arka plana resim eklendi
            JPanel ortaPanel= new JPanel();

            JLabel background= new JLabel( new ImageIcon(getClass().getResource("backgroundIcon.png")));
            ortaPanel.add(background);


            //paneller frame eklendi
            add(sagPanel,BorderLayout.WEST);
            add(solPanel, BorderLayout.EAST);
            add(ortaPanel, BorderLayout.CENTER);

            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0.5;
            c.weighty = 1;

            // sagPanel ve solPanel para miktarları ekrana yazdırıldı
            JLabel[] money= new JLabel[26];

            money[0]= new JLabel("1 TL");
            money[1]= new JLabel("2 TL");
            money[2]= new JLabel("5 TL");
            money[3]= new JLabel("10 TL");
            money[4]= new JLabel("25 TL");
            money[5]= new JLabel("50 TL");
            money[6]= new JLabel("100 TL");
            money[7]= new JLabel("200 TL");
            money[8]= new JLabel("300 TL");
            money[9]= new JLabel("400 TL");
            money[10]= new JLabel("500 TL");
            money[11]= new JLabel("750 TL");
            money[12]= new JLabel("1000 TL");
            money[13]= new JLabel("2500 TL");
            money[14]= new JLabel("5000 TL");
            money[15]= new JLabel("10000 TL");
            money[16]= new JLabel("25000 TL");
            money[17]= new JLabel("50000 TL");
            money[18]= new JLabel("75000 TL");
            money[19]= new JLabel("100000 TL");
            money[20]= new JLabel("200000 TL");
            money[21]= new JLabel("300000 TL");
            money[22]= new JLabel("400000 TL");
            money[23]= new JLabel("500000 TL");
            money[24]= new JLabel("750000 TL");
            money[25]= new JLabel("1000000 TL");

            Border border = BorderFactory.createLineBorder(Color.black, 5);


            // paneller frame e eklendi
            for(int x= 0; x<13; x++){
                c.gridx = 0;
                c.gridy = x;
                sagPanel.add(money[x], c);
                money[x].setOpaque(true);
                money[x].setVisible(true);
                money[x].setIcon((Icon) (new ImageIcon("1.png").getImage()));


            }

            for(int x= 13; x<26; x++){
                c.gridx = 0;
                c.gridy = x;
                solPanel.add(money[x], c);
                money[x].setOpaque(true);
                money[x].setBackground(Color.red);
                money[x].setBorder(border);
            }   
    }   
    public static void main(String [] args){

        Screen frame= new Screen();
        frame.setTitle("Deal or No Deal");
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);//to center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();   
        //frame.setResizable(false);

    }

}

There is no error in my code but when i try to execute it, it does not work. 我的代码没有错误,但是当我尝试执行它时,它不起作用。 Thank you for your help in advance. 提前谢谢你的帮助。

You can add the image like that 您可以像这样添加图像

Sample Code 样例代码

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SwingExampleDemo {
    public static void main(String[] args) {
        JFrame f = new JFrame();// creating instance of JFrame
        ImageIcon imageIcon = new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg"); // here your Image Path
        JLabel jLabel = new JLabel(imageIcon);
        jLabel.setBounds(130, 250, 100, 40);
        f.add(jLabel);
        f.setSize(400, 500);// 400 width and 500 height
        f.setLayout(null);// using no layout managers
        f.setVisible(true);// making the frame visible
    }
}

try this one. 试试这个。

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

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