在JLabel中将图像添加到JPanel [英] Add image to JPanel within JLabel

查看:201
本文介绍了在JLabel中将图像添加到JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;

    public class Interface {

        private JPanel panel;
        private JPanel buttonPane;
        private JLabel label;
        private JLabel label2;
        private JTextField textfield;
        private JTextField textfield2;
        private JTextField textfield3;
        private JTextField textfield4;
        private JTextField textfield5;
        private JButton button;
        private JButton button2;
        private JButton button3;
        private JButton button4;
        private JButton button5;
        private JButton button6;

        public static void main(String[] args) {
            new Interface();
        }

        public Interface() {
            JFrame frame = new JFrame("Vormen");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 300);
            frame.setLocationRelativeTo(null);

            panel = new JPanel();
            buttonPane = new JPanel();
            button = new JButton("cirkel");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {     
    JLabel label3 = new JLabel(new ImageIcon("images/cirkel.png"));
            panel.add(label3);
            panel.revalidate();
            panel.repaint();
        buttonPane.add(button);
        buttonPane.add(button2);
        buttonPane.add(button3);


        frame.add(buttonPane, BorderLayout.NORTH);
        frame.add(panel);
        frame.setVisible(true);

    }
}

但是,如果我运行它,图像就不会出现。

However, if I run it, the image doesn't appear.

为什么会这样?我是Java新手,所以我犯了一些小错误很容易就这样。

Why is that? I'm new to Java so I make little mistakes like this very easily.

我尝试了几个选项,但没有一个适合我。

I have tried several options but none of them worked out for me.

推荐答案

JLabel label3 = new JLabel(new ImageIcon("/images/cirkel.png"));

不要使用 / images /。 .. 。前导/告诉java查看驱动器的根目录。

Don't use /images/.... The leading "/" tells java to look at the root directory of your drive.

我猜你的images目录在你的源目录中,所以你应该使用:

I'm guessing your "images" directory is in your source directory so you should be using:

JLabel label3 = new JLabel(new ImageIcon("images/cirkel.png"));




我是Java新手所以我犯了这样的小错误很容易。

I'm new to Java so I make little mistakes like this very easily.

阅读如何使用图标以获取有关如何加载图像和工作示例的更好示例,以便为您的程序提供更好的结构。

Read the Swing tutorial on How to Use Icons for better examples of how to load images and working examples to give you a better structure to your program.

这个想法是从一个工作程序开始,并从该程序的结构中学习。然后做一些小改动。如果它停止工作,那么你知道你改变了什么以及导致问题的原因。

The idea is to start with a working program and learn from the structure of that program. Then make small changes. If it stops working then you know what you changed and what caused the problem.

这篇关于在JLabel中将图像添加到JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆