JButton issue, please help
so im coming up with a program that im wanting to have a button, but it isnt showing with my graphics on the frame and i dont know what to do.
Im trying to pretty much combine these two pictures below and have the button on the bottom.
http://i268.photobucket.com/albums/j...c52/button.png
http://i268.photobucket.com/albums/j...52/graphic.png
Here is the code i have for putting these into the frame.
Code :
public void showInterface()
{
//FRAME
frame.setSize(700, 800);
frame.setTitle("HangMan");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//PULLS IN STARTING GRAPHICS
display = new DisplayComponent();
JPanel panel = new JPanel();
//BUTTON NEEDING TO BE IN THE FRAME, SHOWING GRAPHICS
button = new JButton("Guess");
AddButtonListener listener = new AddButtonListener();
panel.add(button);
frame.add(display);
frame.add(panel);
button.addActionListener(listener);
frame.setVisible(true);
}
Re: JButton issue, please help
I'm not completely sure about this, but I know I did a go-around when I had this problem when I made a card program a long time ago.
Basically, have 2 JPanels. 1 JPanel will hold your drawing and the other will hold your JButton. Then you just have to add both of them to the JFrame.
If I am not mistaking, the issue is that the JButton is effectively overriding the JPanel's draw methods when you add it to the JPanel,or hiding the drawing or something. I'm not entirely sure of what exactly is happening, but the JButton trumps the drawing.
Re: JButton issue, please help
This part
Code :
frame.add(display);
frame.add(panel);
adds the two components to the default CENTER position of the default BorderLayout of a JFrame. Naturally, only the last added is seen.
Recommended reading:
Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db
Re: JButton issue, please help
Quote:
Originally Posted by
aussiemcgr
If I am not mistaking, the issue is that the JButton is effectively overriding the JPanel's draw methods when you add it to the JPanel,or hiding the drawing or something. I'm not entirely sure of what exactly is happening, but the JButton trumps the drawing.
Sorry, you're mistaken. The situation you describe can arise out of wrongly and inappropriately overriding paint(...) of a JComponent. This one's a simple (lack of understanding of ) layout problem.
db