Reputation: 1
JButton is only appearing on MouseEnter I've tried multiple things but I can't seem to figure out how to get my JButton over the background graphics. . Does anybody know how I can paint over this background and have my button stay there? I would paste the code here but It would get a little messy, so heres some pastebin links! :)
My Main class: http://pastebin.com/DvVfCU03
My MenuPanel class: http://pastebin.com/uht8cabX
Here's a copy of the main class just in-case:
public class Main {
final static JFrame window = new JFrame();
final static ImageIcon imageIconOne = new ImageIcon(getConnectImage());
final static JButton connectB = new JButton(imageIconOne);
public static void main(String[] args) throws IOException {
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new MenuPanel());
window.addKeyListener(new MenuPanel());
window.setMinimumSize(new Dimension(1024,640));
window.pack();
window.setTitle("Realm of Pixels");
window.setVisible(true);
window.addMouseListener(new MouseHandler());
window.setVisible(true);
window.add(connectB);
connectB.setVisible(true);
}
public static Image getConnectImage(){
Image connectImage = null;
try {
connectImage = ImageIO.read(new File("Resources/Buttons/Connect.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connectImage;
}
public static JFrame getWindow() {
return window;
}
}
Upvotes: 0
Views: 45
Reputation: 347334
Call setVisible
of the JFrame
last, after you have constructed the UI, also, call pack
just before you call setVisible
, this way you're gurneteed to get a better size calculation as it is based the content.
Beware that KeyListener
is well know for only responding to key strokes when the component it is registered to is focusable AND has focus, it normally better to use the key bindings API. Also, adding a MouseListener
to the window directly could be ignored if any of the components higher in the component hierarchy also have MouseListener
s
Upvotes: 1