如何在此 Canvas 上添加按钮?就像 float ...在顶部而不是使用 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.
}
}
最佳答案
为什么不使用 Swing 而使用 AWT。毕竟您正在导入 javax.swing.*。然后您将使用 JWindow。
如果您希望组件 float 在中心,那么您应该使用不同的布局管理器。也许是 GridBagLayout。
setLayout( new GridBagLayout() );
add(button, new GridBagConstraints());
关于java - 如何在Java Canvas 中添加这个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6670273/