OK so I have this applet thats like this
- BorderLayout.CENTER – (Within this is JPanel)
- BorderLayout.EAST – (Within this is a new GridLayout (4,5)
- BorderLayout.SOUTH – (Within this is a TextArea)
Anyway, on the applet, I have to HOVER over the buttons to see them. They don’t paint there I guess but I’m adding them in the init() method… so I don’t know what I am doing wrong and why it’s doing this.
setLayout( new BorderLayout() );
JPanel invOne = new JPanel(new GridLayout(5,4));
JPanel game = new JPanel();
add(invOne, BorderLayout.EAST);
add(game, BorderLayout.CENTER);
add(c, BorderLayout.SOUTH);
invOne.setBounds(416,0, 60, 28);
for (int i = 0, j = 20; i < 20; i = i+1, j = j-1) {
invOne.add(new JButton("SLOT " + j));
invOne.setBounds(32,32,100,100);
invOne.setFocusable(false);
}
game.setBounds(0,0, 416, 288);
repaint();
What are you trying to accomplish with all the
setBounds()calls? Either you letpack()set your panel’s size according to what’s inside, or you set bounds once to where you want to see that panel sit. Especially the calls with a size of 32×32 pixels are not helping at all.EDIT:
I found these problems:
As one other poster mentioned, you’re mixing Swing and AWT components. That doesn’t work well. Essentially, if some of the components you use have a “J” at the beginning, you’ll want to go with “J”‘s for all of them. AWT is now considered “old school”. It’s a bit confusing because some classes and components used in GUIs don’t have J’s. I guess you need to work carefully with good examples or look the classes up.
For some reason, the applet didn’t want to work well until I gave explicit row/column counts to the TextArea (now called JTextArea). I changed
new TextArea()tonew JTextArea(3,20).The biggest problem may have been the empty
paint()method. I wonder how the applet displayed anything at all? You could have removed thepaint()method; I fixed it by callingsuper.paint().Finally, class names (such as
bl) should start with uppercase characters. The compiler in IdeOne grumbled at me for that.Here’s my fixed code.
Happy hacking!