J
java
Good evening!
I don't manage to make an AWT-Frame with a GridBagLayout display a
Canvas. This Canvas will be displayed as soon as I use an other than
the damned GridBagLayout. Unfortunately I have to use that
GridBagLayout but do not know how.
An other posting I found suggested to extend the Canvas and override
it's method getPreferredSize. I did so but still the (My)Canvas does
not appear within the GridBagLayout. Can anybody give me a helping
hint?
Regards
Christian
import java.awt.*;
import java.awt.event.*;
public class PaintCanves extends Frame {
public PaintCanves() {
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
this.setSize(150, 200);
this.setLocation(100, 100);
this.setBackground(Color.gray);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridheight = GridBagConstraints.REMAINDER;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
MyCanvas canvas = new MyCanvas();
canvas.setBackground(Color.black);
this.add(canvas, constraints);
}
class MyCanvas extends Canvas {
public Dimension getPreferredSize() {
// canvas claims at least all the surrounding frame
return new Dimension(getWidth(), getHeight());
}
}
public static void main (String argv[]) {
PaintCanves maler = new PaintCanves();
maler.addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e) {
System.exit (0);
}
});
maler.setVisible (true);
}
}
I don't manage to make an AWT-Frame with a GridBagLayout display a
Canvas. This Canvas will be displayed as soon as I use an other than
the damned GridBagLayout. Unfortunately I have to use that
GridBagLayout but do not know how.
An other posting I found suggested to extend the Canvas and override
it's method getPreferredSize. I did so but still the (My)Canvas does
not appear within the GridBagLayout. Can anybody give me a helping
hint?
Regards
Christian
import java.awt.*;
import java.awt.event.*;
public class PaintCanves extends Frame {
public PaintCanves() {
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
this.setSize(150, 200);
this.setLocation(100, 100);
this.setBackground(Color.gray);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridheight = GridBagConstraints.REMAINDER;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
MyCanvas canvas = new MyCanvas();
canvas.setBackground(Color.black);
this.add(canvas, constraints);
}
class MyCanvas extends Canvas {
public Dimension getPreferredSize() {
// canvas claims at least all the surrounding frame
return new Dimension(getWidth(), getHeight());
}
}
public static void main (String argv[]) {
PaintCanves maler = new PaintCanves();
maler.addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e) {
System.exit (0);
}
});
maler.setVisible (true);
}
}