Hi Everyone,
Whenever I add the a Swing object to a JApplet, the Swing object will fill the entire window no matter what I try.
The code at the end of the e-mail boils down my difficulty into a simple example. Please see the code and comments in the init() method.
I also included the html after the
java code so you can see what I'm experiencing for yourself.
Run the
applet, uncomment //pane.add(label); at line 33, then run it again and you'll see what I'm talking about.
Thank You in Advance for Your Help,
Lou
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.Font;
public class PaintApplet extends JApplet
{
private JLabel label = new JLabel("JLabel");
private Container pane;
public void init()
{
JPanel drawAreaJPanel = new TestCanvas();
pane = getContentPane();
pane.add(drawAreaJPanel);
drawAreaJPanel.setBackground(Color.white);
// Attempts to control the size are ignored whenever label is displayed.
label.setBounds(10, 250, 20, 10);
label.setMinimumSize(new Dimension(20, 10));
label.setPreferredSize(new Dimension(20, 10));
// Label occupies entire screen, with or without null layout
//pane.setLayout(null);
//pane.add(label);
// No effect, with or without null layout
//drawAreaJPanel.setLayout(null);
//drawAreaJPanel.add(label);
}
public Dimension getPreferredSize()
{
return new Dimension(400, 300);
}
class TestCanvas extends JPanel
{public Dimension getPreferredSize()
{
return new Dimension(400, 300);
}
public void paint( Graphics g )
{
g.setColor(Color.red);
g.setFont(new Font(null, Font.PLAIN, 100));
g.drawString("Help!", 30, 100);
}
}
}
<html>
<head>
<meta name="generator" content="Modelworks IDE">
<title>PaintApplet</title>
</head>
<body>
<h1>
Test page for PaintApplet Class</h1><hr>
<applet code=PaintApplet.class width=300 height=300></applet>
<hr>
</body>
</html>