简体   繁体   中英

How can I add and remove JLabels to a JPane?

I am trying to make a little GUI. It has three checkboxes and a button. (picture below) I have an image of a head that I want other images to be put over the top of. This way as I select them and push the button they will change. Currently in my handler I have only one ImageIcon. When I run the code as it is the "coolBrows.png" image doesn't show up. I've seen some examples on here of adding a JFrame to a JFrame they said that I need to add a layout for it to work. I've done that and it still wont show up... any help is appreciated! Note: Each image has the same dimensions but the ones that will be overlayed have a transparent background.

Sorry if I broke with any conventions I've never posted here before.

The GUI

在此处输入图片说明

package AssignmentFace;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class AssignmentFace extends JFrame {

    private JPanel contentPane;
    private ImageIcon head;
    private JCheckBox checkBoxBrows;
    private static ImageIcon brows; 

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AssignmentFace frame = new AssignmentFace();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public AssignmentFace() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(1,2));
        setContentPane(contentPane);

        JPanel selectionPanel = new JPanel();
        selectionPanel.setLayout(new GridLayout(7,1));
        contentPane.add(selectionPanel, BorderLayout.WEST);

        // Contains the checkboxes
        checkBoxBrows= new JCheckBox("brows");
        selectionPanel.add(checkBoxBrows);

        JCheckBox checkBoxNose = new JCheckBox("Nose");
        selectionPanel.add(checkBoxNose);

        JCheckBox checkBoxMouth = new JCheckBox("Mouth");
        selectionPanel.add(checkBoxMouth);

        Handler handler = new Handler();

        JButton btnCycle = new JButton("Cycle ...");
        btnCycle.addActionListener(handler);
        selectionPanel.add(btnCycle);

        JPanel facePanel = new JPanel();
        facePanel.setOpaque(true);
        contentPane.add(facePanel, BorderLayout.CENTER);

        //  Head
        head = new ImageIcon(getClass().getResource("/images/head.png"));
        JLabel headLabel = new JLabel(head, JLabel.CENTER);
        headLabel.setLayout(new BorderLayout());
        facePanel.add( headLabel, BorderLayout.CENTER );

        //  Brows
        brows = new ImageIcon();
        JLabel browsLabel = new JLabel(brows, JLabel.CENTER);
        headLabel.add(browsLabel, BorderLayout.CENTER); 

        //  Nose
        //ImageIcon nose = new ImageIcon();
        //JLabel noseLabel = new JLabel(nose, JLabel.CENTER);
        //facePanel.add(noseLabel, BorderLayout.CENTER);

        // Mouth
        //ImageIcon mouth = new ImageIcon();
        //JLabel mouthLabel = new JLabel(nose, JLabel.CENTER);
        //facePanel.add(mouthLabel, BorderLayout.CENTER);
    }

    public class Handler implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e) 
        {
            if(checkBoxBrows.isSelected())
            {
                brows = new ImageIcon(getClass().getResource("/images/coolBrows.png"));
                System.out.println("here"); //For testing purposes. This does come through in the console
            }
        }
    }
}

So it seems like you need to layer Icons or JLabels .

So maybe you can use:

  1. A JLayeredPane to layer the labels. So you create a layer for each facial feature and add a label to the layer. Then you can add/remove the Icon from the label based on the value of the check box. Read the section from the Swing tutorial on How to Use Layered Panes for more information and working examples.

  2. Use the Compound Icon to layer the icons. This allows you to stack Icons on top of one another and then display the resulting Icon on a single JLabel . So every time an event is generated by clicking on a check box you can rebuild the CompoundIcon with the selected features and reset the Icon on the JLabel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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