如何添加JButton
到我的Canvas
?如何將JButton添加到Canvas()?或者如何將JButton添加到Panel()並將Panel()背景設置爲透明?
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 GUI?而且爲什麼你不必要地將類字段靜態化? –