I am currently doing my first applet. While testing the results I want to be able to run it in eclipse in the preview windows instead of always deploying the applet into a jar and opening the page in the browser (the browser cache kills me! I always need to restart the browser...)
Anyway, when I try to run the app with "run as -> Java Applet" I got the preview but its always very small (guess below 200x200). When I change the size per hand, the window grows but the content stay that small. When I call setSize(width, height)
the window starts bigger, the content stays small. Small doesn't mean its scaled down, it means that I only see the black panel, the white one (which is visible in the browser) is not visible so its not seemed to be scaled...
What am I missing?
My code so far (which works like expected in the broswer with width of 560 and height of 500)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Benchmark extends JApplet {
private static final long serialVersionUID = -8767182603875435760L;
GridLayout gridLayout = new GridLayout(7, 1);
JButton startTests = new JButton("Start");
JPanel testPanel = new JPanel();
JPanel topPanel = new JPanel();
@Override
public void init() {
super.init();
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
invalidate();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void initComponents() {
setSize(660, 500);
topPanel.setBackground(Color.BLACK);
topPanel.setSize(500, 500);
testPanel.setBackground(Color.WHITE);
testPanel.setSize(160, 500);
getContentPane().add(topPanel, BorderLayout.WEST);
getContentPane().add(testPanel, BorderLayout.EAST);
testPanel.setLayout(gridLayout);
testPanel.add(new JLabel("Triangles"));
testPanel.add(new JLabel("Pyramids"));
testPanel.add(new JLabel("Cubes"));
testPanel.add(new JLabel("Blending"));
testPanel.add(new JLabel("Spheres"));
testPanel.add(new JLabel("Lights"));
testPanel.add(new JLabel("Mass"));
}
}
The screenshot should show the problem. If the window has the size of 660x500 (set with setSize()
the visible area stays small:
Your size of the window is set to 500,500 and so it this size of your black box. The panel on the right is visible if you widen your screen.
Remove the code for setting a size and a min,max, and preferred size for the topPanel
. Then instead of adding it to BorderLayout.WEST
, use BorderLayout.CENTER
instead. This will allow the test panel to stay on the left and will resize your black box as the window resizes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With