简体   繁体   English

在 JLabel 中将图像添加到 JPanel

[英]Add image to JPanel within JLabel

I have this code:我有这个代码:

    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.但是,如果我运行它,图像不会出现。

Why is that?这是为什么? I'm new to Java so I make little mistakes like this very easily.我是 Java 新手,所以我很容易犯这样的小错误。

I have tried several options but none of them worked out for me.我尝试了几种选择,但没有一个适合我。

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

Don't use /images/... .不要使用/images/... The leading "/" tells java to look at the root directory of your drive.前导“/”告诉 java 查看驱动器的根目录。

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

I'm new to Java so I make little mistakes like this very easily.我是 Java 新手,所以我很容易犯这样的小错误。

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.阅读有关如何使用图标的 Swing 教程, 获取有关如何加载图像和工作示例的更好示例,从而为您的程序提供更好的结构。

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.如果它停止工作,那么您就会知道您更改了什么以及导致问题的原因。

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

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