简体   繁体   English

将图像添加到 JLabel 中的 JPanel

[英]Adding an image to JPanel within JLabel

I'm new to Java, and I'm trying to make an JFrame, with a JPanel inside it and an image inside a JLabel.我是 Java 新手,我正在尝试制作一个 JFrame,里面有一个 JPanel,一个 JLabel 里面有一个图像。 Then by adding the JLabel to the JPanel it must work right?然后通过将 JLabel 添加到 JPanel 它必须正常工作吗? However, this is the way it's done on docs.oracle.com...但是,这是在 docs.oracle.com 上完成的方式...

This is the 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);

    }
}

Your application can't find the ""images/cirkel.png". You have few alternatives:您的应用程序找不到 ""images/cirkel.png"。您几乎没有其他选择:

  • Use an absolute path (like I do in the modified code below).使用绝对路径(就像我在下面修改后的代码中所做的那样)。
  • Use resources (there are hundreds of good tutorials how to do this).使用资源(有数百个很好的教程如何做到这一点)。

I use absolute path for quick hacks.我使用绝对路径进行快速破解。 For anything serious I would chose resources as they are bundled with your application.对于任何严肃的事情,我会选择资源,因为它们与您的应用程序捆绑在一起。


import java.awt.BorderLayout;
import java.awt.FlowLayout;
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(new FlowLayout());
        buttonPane = new JPanel();
        button = new JButton("cirkel");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Use absolute path here:
                JLabel label = new JLabel(new ImageIcon("/home/tyrion/circle.png"));
                panel.add(label);
                panel.revalidate();
                panel.repaint();

            }
        });

        buttonPane.add(button);
        // buttonPane.add(button2);
        // buttonPane.add(button3);

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

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

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