简体   繁体   English

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

[英]How to add JButton to Canvas()? Or how to add JButton to Panel() and make the Panel() background as transparent?

How can I add a JButton to my Canvas ?如何将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);
    }
}

Please don't mix AWT with Swing .请不要将AWTSwing混合使用。 Today, Swing code is required JPanel .今天,需要JPanel的 Swing 代码。 Sure, it is possible to mix AWT with Swing but with unexpected output to the GUI.当然,可以将 AWT 与 Swing 但意外的 output 混合到 GUI。 For transparency, you have to look at How to Create Translucent and Shaped Windows and search for examples here.对于透明度,您必须查看如何创建半透明和形状 Windows并在此处搜索示例。

EDIT: for the nicest and better output to the GUI check, how the LayoutManagers work.编辑:为了最好和更好的 output 到 GUI 检查, LayoutManagers是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM