The button wouldn't show on the panel that's inside another panel
My problem is the the Button"test2" in the constructor won't show up. I have no idea what is wrong and there are no error at all.
My Goal is to create serveral classes that represent a part of my GUI, which could be put together in my main class. This is a simplify version of my code and has the exact same problem.
I am sure all I need is a tiny change in my code but I couldn't figure it out. Any help/suggestion will be appreciate. :)
Code :
import java.awt.*;
import javax.swing.*;
public class ResPanel extends JPanel{
public void ResPanel() {
setSize(100,100);
setLayout(new FlowLayout());
setBackground(Color.blue);
JButton test2 = new JButton("test2");
add(test2);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel lol = new JPanel();
JButton test1 = new JButton("test1");
ResPanel r = new ResPanel();
lol.setLayout(new FlowLayout());
lol.setBackground(Color.red);
lol.add(test1);
lol.add(r);
frame.add(lol);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Re: The button wouldn't show on the panel
There u go add the rest from here:
Code :
import javax.swing.*;
public class ResPanel extends JFrame{
private static final long serialVersionUID = 1L;
public JButton test1 = new JButton("test1");
public JButton test2 = new JButton("test2");
public JPanel lol = new JPanel();
public ResPanel(){
super("Title");
lol.add(test1);
lol.add(test2);
add(lol);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new ResPanel();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Re: The button wouldn't show on the panel
First of all, thanks for the quick reply.(for a moment I though my post was deleted lol)
I am sorry if I didn't make it clear, but I kinda want to keep a certain "style" for the code.
The new code works perfectly but it won't fit into all my other code unfortunatly.
What I am doing right now is making my own game and my structure of the code is like :
Class "game" that contain the main game/animation screen
class "resource panel" that handle everything about that resouce bar.
class "info panel" ....
etc.
and one last class that contain the frame and the main method which put all the other classes together.
also, to make the GUI looks sensible, I really want to have Panels inside Panels( just like in the code where I add the panel \'r\' into panel \'lol\' then add \'lol\' to the frame.
that\'s the reason I want to extent Jpanel for my ResPanel, so that I could isolate it from the other code.
Is there really no way to save my original code? I am so close to my goal :<
I hope I am not asking too much, feel free to say how stubborn I am XD
Re: The button wouldn\'t show on the panel
Quote:
Originally Posted by macko
There u go add the rest from here:
macko, please read the forum rules and the following link: http://www.javaprogrammingforums.com...n-feeding.html
Re: The button wouldn't show on the panel
@nggdowt, As an aside, FlowLayout is the default layout of a JPanel, so there isn't a need to set that layout (it doesn't hurt, I am just pointing this out for your benefit).
Add some println's in your methods, in particular near the code where the button that is not visible is added (in other words, in the method that looks like a constructor, hint hint)
Re: The button wouldn't show on the panel
Sorry, Keep getting carried away in helping others. Somethings though you do need to agree that viewing somebodys solution can benifit you to see how they have produced the outcome.
If you are really that stressed out you wouldnt want to code you would just want to get it done, Yes they need to learn but if its something thats due ASAP or they dont want to spend 2 weeks learning. Do you not think this can still help users?
Also i had only posted back his code, Simply modified the position of the data.. I had not added any panels inside other panels.
Hence why i had said "Work the rest out yourself"
I get the basic frame and content on the screen and they can learn the rest, you see where im going?
Re: The button wouldn't show on the panel
As for the rest adding panels inside panels etc..
try using a for loop
Code :
//for loop to initialize all panels.
...
...
//add the panels to another panel you want them in...
// display that panel on the screen...
Good luck.
Re: The button wouldn't show on the panel
Thanks for all the great advice!
right, so the real problem. The constructor of ResPanel didn't get called at all...
The first thing I tried is changing
Code :
ResPanel r = new ResPanel();
to
Code :
JPanel r = new ResPanel();
but it stay the same.
I could change the whole code up and make it work in another way, but I really want to keep the existing structure of the code.
is there something I am missing for me to call that constructor? do I need a getter method like
Code :
public ResPanel getResPanel(){
return new ResPanel();
}
or ...
===========
okay i was typing all that and suddenly I found out what is wrong D:
my code was almost perfect....except....THERE IS A FREAKING VOID IN MY CONSTRUCTOR D:
i must have been thinking about I need to add void because it returns nothing...
I removed the void tag and my codes runs fine now.
@copeg
thanks for the hint lol..I keep focusing on the main method and trying to find what's wrong and I totally forgot about the constructor.