如何在Java画布中添加这个按钮?
如何在画布上添加按钮?就像浮动...在顶部而不是使用 add(button) 将其放在网格中;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Myscreensaver extends Window
{
private static final Canvas canvas = new Canvas();
private Button button;
public Myscreensaver()
{
setLayout(new GridLayout(2,2));
canvas.setPreferredSize(new Dimension(200, 200));
add(canvas);
//add(button); no add the button in the canvas not in the grid, then it looks odd.
}
}
How can i add the button ontop of this canvas? like floating... on top instead of having it in the grid using add(button);
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Myscreensaver extends Window
{
private static final Canvas canvas = new Canvas();
private Button button;
public Myscreensaver()
{
setLayout(new GridLayout(2,2));
canvas.setPreferredSize(new Dimension(200, 200));
add(canvas);
//add(button); no add the button in the canvas not in the grid, then it looks odd.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用 Swing 而使用 AWT。毕竟您正在导入 javax.swing.*。然后您将使用 JWindow。
如果您希望组件浮动在中心,那么您应该使用不同的布局管理器。也许是 GridBagLayout。
Why don't use use Swing instead of AWT. After all you are importing javax.swing.*. Then you would use a JWindow.
If you want the component to float in the center then you should be using a different layout manager. Maybe a GridBagLayout.
MigLayout 还有一个您可以考虑的选项:
这将使按钮浮动在任何其他添加的内容之上,而无需绝对定位。请参阅他们的 演示、绝对位置、Glasspane 替代品
MigLayout also has an option you could consider:
This will float the button over the top of anything else added without absolute positioning. See their demo, Absolute Position, Glasspane Substitute
与上一张海报相同的想法。如果您在画布之后添加按钮,则按钮将位于画布的顶部。您应该记录自己关于轻量级和重量级组件的信息,因为这样您将在此类问题上快速获得结果。
一个简单的解释:http://www.blurtit.com/q132749.html
Same thought like the last poster. If you add the button after the canvas the button will be on the top of the canvas.You should document you're self about light and heavy weight components to because then you're will get fast results in this kind of problems.
A simple explanation : http://www.blurtit.com/q132749.html