将图像添加到具有多个面板的 JFrame 内的 JPanel
我将图像直接添加到 jframe 上,但如果我添加另一个面板,图像就会消失, 我尝试将图像添加到标签并将其添加到面板,但这不起作用这里是我用于获取图像的代码:
这是我更新的程序:
public class imgload extends JFrame implements ActionListener {
private JButton b1;
public imgload() {
makeframe();
}
public void makeframe() {
JFrame f = new JFrame();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JLabel lb = new JLabel();
b1 = new JButton("Confirm");
b1.addActionListener(this);
JTextArea q1 = new JTextArea();
ImageIcon icon = createImageIcon("sample.jpg");
lb.setIcon(icon);
q1.setLocation(10,10);
q1.setSize(50,50);
p2.add(q1);
b1.setLocation(100,105);
b1.setSize(80,30);
p2.add(b1);
f.add(p1.add(lb));
// f.add(p2);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800,600);
f.setAlwaysOnTop(true);
f.setResizable(false);
}
public ImageIcon createImageIcon(String path) {
URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1) {
System.exit(0);
}
}
public static void main(String[] args) {
new imgload();
}
}
I added my image straight onto the jframe but then if i add another panel the image disappears,
i have tried to add image to a label and added this to a panel but this doesn't work either here is my code used to get my image:
here is my updated program:
public class imgload extends JFrame implements ActionListener {
private JButton b1;
public imgload() {
makeframe();
}
public void makeframe() {
JFrame f = new JFrame();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JLabel lb = new JLabel();
b1 = new JButton("Confirm");
b1.addActionListener(this);
JTextArea q1 = new JTextArea();
ImageIcon icon = createImageIcon("sample.jpg");
lb.setIcon(icon);
q1.setLocation(10,10);
q1.setSize(50,50);
p2.add(q1);
b1.setLocation(100,105);
b1.setSize(80,30);
p2.add(b1);
f.add(p1.add(lb));
// f.add(p2);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800,600);
f.setAlwaysOnTop(true);
f.setResizable(false);
}
public ImageIcon createImageIcon(String path) {
URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1) {
System.exit(0);
}
}
public static void main(String[] args) {
new imgload();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要研究在 Swing 中使用布局(BorderLayout、FlowLayout、GridLayout、GridBagLayout 等)。默认情况下,大多数东西都有一个 BorderLayout,当您使用不带参数的
add
时,它会将项目放置在中心。后续调用只是替换中心的项目。You need to look into using layouts (BorderLayout, FlowLayout, GridLayout, GridBagLayout, etc) in Swing. By default, most things have a BorderLayout, and when you use
add
with no arguments, it places the item in the center. Subsequent calls just replace the item in the center.