Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I order two components in a JFrame in order for transparency and ActionListeners to work?
    primarykey
    data
    text
    <p>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).</p> <p>This is the code for the Frame I am adding the components to.</p> <pre><code> 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(); </code></pre> <p>Here is the code for the JPanel (Stripped down for simplicity's sake)</p> <pre><code>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); } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload