如何将JButton
添加到Canvas
中?
public class mywindow extends Window
{
private static final Canvas canvas = new Canvas();
private JButton button;
public mywindow
{
super(new Frame());
button = new JButton("close");
setLayout (new BorderLayout ());
//Step 1 - failed
add("North", canvas);
canvas.setSize(300,300);
canvas.setLocation(0,0);
// button = new JButton(my, "close"); will not work
// How can I add the button to the canvas?
//Step 2 - works, but it gets the background color, instead of real transparency.
//JPanel p = new JPanel(); p.setOpaque(false);
//p.setSize(300,300); p.setLocation(0,0);
//add("North", p);
//p.add("Left", button);
}
}
请不要把AWT和Swing混在一起。现在,Swing代码是必需的JPanel。当然,可以将AWT与Swing混合使用,但会向GUI输出意想不到的输出。对于透明度,您必须查看如何创建半透明和形状窗口,并在这里搜索示例。
编辑:为了最好的和更好的输出到GUI检查,如何LayoutManagers的工作。