开发者

How do I order two components in a JFrame in order for transparency and ActionListeners to work?

开发者 https://www.devze.com 2023-03-18 01:17 出处:网络
When adding two components to a JFrame, where one sits inside another, If I add them in the order, Fullscreen Object, then JPanel, the JPanel displays correctly, but is essentially invisible, i.e it\'

When adding two components to a JFrame, where one sits inside another, If I add them in the order, Fullscreen Object, then JPanel, the JPanel displays correctly, but is essentially invisible, i.e it's action listener won't work and the clicks register on the fullscreen object. If I add them the other way round The JPanel works as it should, but doesn't display correctly (It has transparent areas).

This is the code for the Frame I am adding the components to.

            gameOBJ = new gameClass(width, height);
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(0);
            frame.add(gameOBJ.UIPanel);
            frame.add(gameOBJ);
            frame.validate();
            frame.setUndecorated(true);
            frame.setBounds(0, 0, width, height);
            frame.setResizable(false);

            frame.addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent we)
                        {
                            new exitWindow("Don't forget to save your game! \n Are you sure you want to Exit?", true);
                        }
                });
            frame.setVisible(true);
            gameOBJ.start();

Here is the code for the JPanel (Stripped down for simplicity's sake)

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class UserInterface extends JPanel implements ActionListener
    {
        private static final long serialVersionUID = 1L;
    private Image image;

    private int xBound = 800;
    private int yBound = 177;

    private JButton mainMenuButton = new JButton(new ImageIcon("res/images/MainMenuButton.gif"));

    private int buttonWidth = 179;
    private int buttonHeight = 52;

    public UserInterface()
        {
            this.setLayout(null);
            this.image = new ImageIcon("res/images/UIPanelImage.gif").getImage();
            this.setOpaque(false);
            this.setSize(this.xBound, this.yBound);
            mainThreeButtons(); //ONLY ONE SHOWN FOR SIMPLICITY
        }

    public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this); //IMAGE CONTAINS TRANSPARENCY
        }

    @Override
    public void actionPerformed(ActionEvent event)
        {
 开发者_StackOverflow社区           else if (event.getSource() == mainMenuButton)
                {
                    new mainMenuWindow();
                }
        }

    private void mainThreeButtons()
        {
            this.add(mainMenuButton);
            mainMenuButton.addActionListener(this);
            //mainMenuButton.setOpaque(false);
            mainMenuButton.setBorderPainted(false);
            mainMenuButton.setContentAreaFilled(false);
            mainMenuButton.setBounds(617, 6, buttonWidth, buttonHeight);
        }
}

I would show an image but I'm not allowed to, The area which is meant to be transparent isn't showing the frame, because it is grey, whatever I set as the Frame's background, OR the panel's background, as again it is grey whatever I set the panel's background colour as.


You probably want to use JLabel instead of JPanel. I know it sounds a bit unintuitive, but I'm not sure JPanel is suited to the purpose you are using it for. Also, JLabel can have a native ImageIcon set, so try using that.

public UserInterface() { // extends JLabel
    this.setImageIcon(new ImageIcon("res/images/UIPanelImage.gif"));
    // or super(~imageicon~)
}

Unlikely, but it could be that the image is not yet loaded when it gets drawn. You should use MediaTracker to manage that more carefully (although I'm not sure ImageIcon if takes care of this for you).

final static protected MediaTracker mediatracker = new MediaTracker(new Canvas());
static protected void checkImageIsReady(Image i) {
    mediatracker.addImage(i, 1);
    try {
        mediatracker.waitForAll();
    } catch (InterruptedException e) { }
    mediatracker.removeImage(i);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号