如何将JButton添加到Canvas()?或者如何将JButton添加到Panel()并使Panel()背景透明?

0 java linux user-interface swing swt

我怎样才能添加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);
    }
}
Run Code Online (Sandbox Code Playgroud)

mKo*_*bel 5

请不要将AWTSwing混合使用.今天,Swing代码是JPanel所必需的.当然,可以将AWT与Swing混合,但可以将意外的输出混合到GUI.为了透明,您必须查看如何创建半透明和形状Windows并在此处搜索示例.

编辑:对于GUI检查最好和更好的输出,LayoutManagers如何工作.