• Post Reply Bookmark Topic Watch Topic
  • New Topic

Jlabel pushing graphics panel out of view  RSS feed

 
Hugh Roarty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi can anybody help with this java swing problem. Here is the code...

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;


public class MyApplet extends JApplet
{
public void init()
{
Container content = getContentPane();
content.setLayout(new BorderLayout());
content.add(new ButtonPanel(), BorderLayout.WEST);
content.add(new LabelPanel(), BorderLayout.CENTER);
content.add(new GridPanel(), BorderLayout.EAST);
}


class ButtonPanel extends JPanel
{
ButtonPanel()
{
this.add(new JButton("Press"));
}
}


class LabelPanel extends JPanel
{
LabelPanel()
{
this.add(new JLabel("Hello World"));
}
}


class GridPanel extends JPanel
{
public void paint (Graphics g)
{
Graphics2D g2D = (Graphics2D)g;
g2D.setColor(Color.WHITE);

for(int i = 0; i < 450; i += 10)
g2D.draw(new Line2D.Float(i, 0, i, 450));
for(int i = 0; i < 450; i += 10)
g2D.draw(new Line2D.Float(0, i, 450, i));
}
}
}


What I expected to see was a JButton on the left of the applet, a label saying "Hello World" and then a cross-hatched grid on the right of the applet measuring 450 x 450 pixels. Instead it appears that the "Hello World" label has expanded across the applet display area and pushed the grid to the extreme right.

Has anybody got any idea how I can stop the JLabel from pushing my grid off the applet.

Thanks.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Hugh Roarty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Craig,

That worked fine
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!