简体   繁体   English

Java小程序按钮

[英]java applet buttons

OK so I have this applet thats like this 好,我有这样的小程序

  • BorderLayout.CENTER - (Within this is JPanel) BorderLayout.CENTER-(在这是JPanel)
  • BorderLayout.EAST - (Within this is a new GridLayout (4,5) BorderLayout.EAST-(这是一个新的GridLayout(4,5)
  • BorderLayout.SOUTH - (Within this is a TextArea) BorderLayout.SOUTH-(在其中是一个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. 我猜他们没有在那儿绘画,但是我在init()方法中添加了它们……所以我不知道我做错了什么以及为什么这样做了。

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? 您试图通过所有setBounds()调用完成什么? 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. 您可以让pack()根据内部内容设置面板的大小,或者一次将边界设置到要查看该面板所在的位置。 Especially the calls with a size of 32x32 pixels are not helping at all. 尤其是32x32像素大小的呼叫完全没有帮助。


EDIT: 编辑:

I found these problems: 我发现了以下问题:

  • As one other poster mentioned, you're mixing Swing and AWT components. 正如另一位张贴者所提到的,您正在混合使用Swing和AWT组件。 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. 本质上,如果您使用的某些组件的开头都带有“ J”,则所有组件都将使用“ J”。 AWT is now considered "old school". AWT现在被认为是“老派”。 It's a bit confusing because some classes and components used in GUIs don't have J's. 这有点令人困惑,因为GUI中使用的某些类和组件没有J。 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). 出于某种原因,直到我为TextArea(现在称为JTextArea)提供了明确的行/列计数,小程序都无法正常工作。 I changed new TextArea() to new JTextArea(3,20) . 我将new TextArea()更改为new JTextArea(3,20)

  • The biggest problem may have been the empty paint() method. 最大的问题可能是空的paint()方法。 I wonder how the applet displayed anything at all? 我想知道小程序怎么显示什么? You could have removed the paint() method; 您可能已经删除了paint()方法; I fixed it by calling super.paint() . 我通过调用super.paint()修复了它。

  • Finally, class names (such as bl ) should start with uppercase characters. 最后,类名(例如bl )应以大写字母开头。 The compiler in IdeOne grumbled at me for that. 为此,IdeOne中的编译器对我发牢骚。

Here's my fixed code . 这是我的固定代码

Happy hacking! 骇客入侵!

Found one page (in german language) which describes the same problem: JButton widgets only show up after hovering over them. 找到了描述相同问题的一页(德语) :JButton小部件仅在将它们悬停后才显示。

The problem there was that AWT and Swing components/widgets have been mixed. 问题在于AWT和Swing组件/小部件混合在一起。 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. 如果是这种情况,我从代码片段中看不到,但是如果您有java.awt.*导入,请将其禁用,将代码重构为仅使用Swing类,然后重试/希望达到最佳效果。

Another suggestion was to explicitely do a setVisible(true) for every button, but the questioner said, that this didn't help in his case. 另一个建议是显式地为每个按钮执行setVisible(true) ,但发问者说,这对他的情况没有帮助。

After adding all your components in the panel, do you explicitely call the "pack()" (or the "repaint()") method ? 在面板中添加了所有组件之后,是否显式调用“ pack()”(或“ repaint()”)方法? Not calling theses methods can result in graphic troubles in your Frames... 不调用这些方法可能会导致Frames中的图形问题...

You are using Swing components in an Applet. 您正在Applet中使用Swing组件。 You should use JApplet. 您应该使用JApplet。 Just change extends Applet to extends JApplet . 只是改变extends Applet extends JApplet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM