[英]How to add an Image to a JFrame
I'm trying to add an image to a JFrame
and set its location, I don't know why it just does not add into it, maybe I don't understand how the JFrame
class works since a normal text JLabel
adds into the JFrame
simply without any trouble and a JLabel
containing an image simply does not add in. 我正在尝试将图像添加到JFrame
并设置其位置,我不知道为什么它不添加到图像中,也许我不明白JFrame
类的工作原理,因为普通文本JLabel
添加到了JFrame
完全没有任何麻烦,并且包含图像的JLabel
根本不会添加。
I would appreciate if someone would explain the error in the code, and maybe even give me a short explanation of why my way does not work. 如果有人会解释代码中的错误,甚至可能给我简短解释为什么我的方法行不通,我将不胜感激。 Thanks! 谢谢!
import java.awt.*;
import javax.swing.*;
public class Walk {
public static void main(String[] args) {
JFrame f = new JFrame("Study");
f.setSize(3000,1000);
f.getContentPane().setBackground(Color.white);
f.getContentPane().add(new JLabel("test", JLabel.CENTER) );
JLabel l = new JLabel(new ImageIcon("C:\\Users\\leguy\\OneDrive\\Desktop\\Stuff\\stillsp"));
l.setBounds(100, 100, 100, 100);
l.setVisible(true);
f.add(l);
f.setVisible(true);
}
}
about the picture into jframe is easy one all what you have to do is 1. create a label by setting its size as you want on the jframe by dragging and dropping only 2. follow the pictures 关于将图片放入jframe的操作很容易,所有您需要做的就是1.通过拖放仅在jframe上设置其大小来创建标签2.跟随图片
Hope it helps 希望能帮助到你
Make sure the path to your image is valid. 确保图像路径有效。 All I did was point to a valid image on my PC and the code practically worked. 我所做的只是指向PC上的有效映像,并且该代码实际上有效。 There were a few things added and organized below. 下面添加并组织了一些内容。
import java.awt.Color;
import javax.swing.*;
public class Walk {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() { // Safety first...
@Override
public void run() {
String path = "C:\\Path\\To\\Image.png"; // Make sure it's correct
JFrame frame = new JFrame("Study");
JLabel label = new JLabel(new ImageIcon(path));
frame.setSize(3000, 1000);
frame.getContentPane().setBackground(Color.white);
frame.getContentPane().add(new JLabel("test", JLabel.CENTER));
label.setBounds(100, 100, 100, 100);
label.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack(); // Pack the frame's components.
frame.setVisible(true);
}
});
}
}
To make sure both labels show up, provide a layout and add them accordingly. 为了确保同时显示两个标签,请提供一个布局并相应地添加它们。
import java.awt.*;
import javax.swing.*;
public class Walk {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
String path = "C:\\Path\\To\\Image.png"; // Make sure it's correct
JFrame frame = new JFrame("Study");
Container container = frame.getContentPane();
JLabel imageLbl = new JLabel(new ImageIcon(path));
JLabel textLbl = new JLabel("test");
frame.setLayout(new BorderLayout());
frame.setSize(3000, 1000);
imageLbl.setBounds(100, 100, 100, 100);
imageLbl.setVisible(true);
container.setBackground(Color.WHITE);
container.add(textLbl, BorderLayout.NORTH);
container.add(imageLbl, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.