gpt4 book ai didi

java - 如何将 ImageIcon 添加到 JLabel

转载 作者:行者123 更新时间:2023-12-01 13:16:22 25 4
gpt4 key购买 nike

我的第一个 Java 项目是 Deal or No Deal,我正在向 for 循环中的 JLabel 添加图片。这是到目前为止我的代码:

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);

}

}

我的代码中没有错误,但是当我尝试执行它时,它不起作用。感谢您提前提供的帮助。

最佳答案

您可以像这样添加图像

示例代码

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

试试这个。

关于java - 如何将 ImageIcon 添加到 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22436779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com