How can I add a JButton to my 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. Today, Swing code is required JPanel. Sure, it is possible to mix AWT with Swing but with unexpected output to the GUI. For transparency, you have to look at How to Create Translucent and Shaped Windows and search for examples here.
EDIT: for the nicest and better output to the GUI check, how the LayoutManagers work.