java 小程序按钮
好的,我有这个小程序,就像这样
- BorderLayout.CENTER - (其中是 JPanel)
- BorderLayout.EAST -(其中是一个新的 GridLayout (4,5)
- BorderLayout.SOUTH -(其中是一个 TextArea)
无论如何,在小程序,我必须将鼠标悬停在按钮上才能看到它们,我猜它们不会在那里绘制,但我将它们添加到 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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想通过所有
setBounds()
调用来实现什么目的?您可以让pack()
根据内部内容设置面板的大小,或者将边界一次设置为您希望看到该面板所在的位置。尤其是尺寸为 32x32 像素的调用根本没有帮助。编辑:
我发现了这些问题:
正如另一张海报提到的,您正在混合 Swing 和 AWT 组件。那效果不太好。本质上,如果您使用的某些组件开头有“J”,那么您将希望所有组件都使用“J”。 AWT 现在被认为是“老派”。这有点令人困惑,因为 GUI 中使用的某些类和组件没有 J。我想您需要仔细研究好的示例或查找类。
出于某种原因,在我向 TextArea(现在称为 JTextArea)提供明确的行/列计数之前,小程序不想正常工作。我将
new TextArea()
更改为new JTextArea(3,20)
。最大的问题可能是空的
paint()
方法。我想知道小程序是如何显示任何内容的?您可以删除paint()
方法;我通过调用super.paint()
修复了这个问题。最后,类名(例如
bl
)应以大写字符开头。 IdeOne 中的编译器对此向我抱怨。这是我的固定代码。
快乐黑客!
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 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()
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!
发现一页(德语)描述了相同的问题: JButton 小部件仅在将鼠标悬停在其上方后才会显示。
问题在于 AWT 和 Swing 组件/小部件已混合在一起。我无法从您的代码片段中看出是否是这种情况,但如果您有
java.awt.*
导入,请禁用它们,重构您的代码以仅使用 Swing 类,然后重试/希望最好的。另一个建议是明确地为每个按钮执行一个
setVisible(true)
,但提问者说,这对他的情况没有帮助。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.在面板中添加所有组件后,您是否明确调用“pack()”(或“repaint()”)方法?
不调用这些方法可能会导致框架中出现图形问题...
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...
您正在 Applet 中使用 Swing 组件。你应该使用 JApplet。只需将
extends Applet
更改为extends JApplet
即可。You are using Swing components in an Applet. You should use JApplet. Just change
extends Applet
toextends JApplet
.