I am just starting with applets so I am not to good. I recently tried using this code:
public class AppletTest extends JApplet{
public void init(){
try{
SwingUtilities.invokeAndWait(new Runnable(){public void run(){
setSize(500,500);
JLabel lbl = new JLabel("Hello World");
add(lbl);
JTextPane pane = new JTextPane();
pane.setSize(200, 200);
add(pane);
}});
}catch(Exception e){
System.err.println("Error Occurred");
}
}
}
What I intended it to do was make a 500 by 500 applet with a text pane that is 200 by 200. However, when I ran the program, the text pane flashed for a moment in the correct size then proceeded to fill up the applet and cover anything else I put on in the applet. If I expanded the applet (manipulated it by dragging the corner) the text pane would grow as well. I tried this and got the same results with a JButton and a JPasswordField. Is there something I am missing? I tried setBounds(int i, int j, int k, int l) But I got the same results. Any help?
You need to change two things:
Explicitly set a
LayoutManager(FlowLayoutis simple and works fine here)Call
setPreferredSize()instead ofsetSize()for yourJTextPanelHere’s the modified code: