test
test

Reputation: 18200

java applet buttons

OK so I have this applet thats like this

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();

Upvotes: 2

Views: 1255

Answers (4)

Denis Tulskiy
Denis Tulskiy

Reputation: 19167

You are using Swing components in an Applet. You should use JApplet. Just change extends Applet to extends JApplet.

Upvotes: 1

Carl Smotricz
Carl Smotricz

Reputation: 67750

What are you trying to accomplish with all the setBounds() calls? Either you let pack() 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 32x32 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() to new 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 the paint() method; I fixed it by calling super.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!

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114757

Found one page (in german language) which describes the same problem: JButton widgets only show up after hovering over them.

The problem there was that AWT and Swing components/widgets have been mixed. I can't see from your code fragment if this is the case, but if you have java.awt.* imports, disable them, refactor your code to only use Swing classes and try again / hope for the best.

Another suggestion was to explicitely do a setVisible(true) for every button, but the questioner said, that this didn't help in his case.

Upvotes: 2

Benoit Courtine
Benoit Courtine

Reputation: 7064

After adding all your components in the panel, do you explicitely call the "pack()" (or the "repaint()") method ? Not calling theses methods can result in graphic troubles in your Frames...

Upvotes: 1

Related Questions