简体   繁体   English

如何订购 JFrame 中的两个组件以使透明度和 ActionListeners 工作?

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

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, ie it's action listener won't work and the clicks register on the fullscreen object.将两个组件添加到 JFrame 时,其中一个位于另一个内部,如果我按顺序添加它们,全屏 Object,然后是 JPanel,JPanel 显示正确,但基本上是不可见的,即它的动作侦听器不起作用并且点击注册在全屏 object 上。 If I add them the other way round The JPanel works as it should, but doesn't display correctly (It has transparent areas).如果我以相反的方式添加它们 JPanel 可以正常工作,但无法正确显示(它具有透明区域)。

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)这是 JPanel 的代码(为简单起见,已删除)

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)
        {
            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.您可能想使用 JLabel 而不是 JPanel。 I know it sounds a bit unintuitive, but I'm not sure JPanel is suited to the purpose you are using it for.我知道这听起来有点不直观,但我不确定 JPanel 是否适合您使用它的目的。 Also, JLabel can have a native ImageIcon set, so try using that.此外,JLabel 可以有一个本地 ImageIcon 集,所以尝试使用它。

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).您应该使用 MediaTracker 来更仔细地管理它(尽管我不确定 ImageIcon 是否会为您解决这个问题)。

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

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

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