0

I have an Applet program, which can be run from Eclipse directly for testing. I am setting window size of this Applet. But, I am seeing sometimes the applet opens with smaller window rather than the actual window size is set, and sometimes it opens with the proper set size setSize(550, 650);

I couldn't get fix for why sometimes it opens with the smaller window. Could someone advise me to fix this issue?

    public class HomeApplet extends Applet implements ActionListener
{   

public void init() {


        titleStr = "Welcome to Application Home page!";

        connectBtn = new Button("Submit");

        connectBtn.addActionListener(this);
        add(connectBtn);  
        connectBtn.setBounds(100, 120, 90, 20);
        connectBtn.setEnabled(true);
        setLayout( null );

        setSize(550, 650);

        sharedImage = new ImageIcon("sameer15.jpg" ).getImage(); 

}
public void paint (final Graphics g)
    {
        //super.paint(g);
        int x = getSize().width;
        int c1 = x/2;

        Font titleFont = new Font("Arial", Font.BOLD, 20);
        g.setFont(titleFont);
        g.drawString(titleStr, c1-170, 20);

        Font connectFont = new Font("Arial", Font.BOLD, 15);
        g.setFont(connectFont);
        g.drawString(connectStr, c1-190, 80);

        g.drawImage(sharedImage, 100, 100, this);
        System.out.println("drawImage"); 
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Stella
  • 1,728
  • 5
  • 41
  • 95
  • Maybe the answer to [this question](http://stackoverflow.com/questions/19367320/java-applet-windows-size-is-not-increasing) could help. – cindyxiaoxiaoli Mar 24 '14 at 20:48
  • That's great! I need to test it many times though. If Applet not run thru Eclipse, may be run it thru html, then the same problem won't occur? – Stella Mar 25 '14 at 04:04
  • `connectBtn.setBounds(100, 120, 90, 20);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 25 '14 at 07:18

2 Answers2

2

Could someone advise me to fix this issue?

That applet can be tested in AppletViewer by including an applet element in a code block at the top of the source code.

I.E. change:

public class HomeApplet extends Applet implements ActionListener

To something like:

/* <applet code=HomeApplet width=550 height=650></applet> */
public class HomeApplet extends Applet implements ActionListener

Then to compile and run:

prompt> javac HomeApplet.java
prompt> appletviewer HomeApplet.java

Note

setSize(550, 650);

This is just plain wrong for an applet. The size of an applet should be set in HTML or by other means. The applet (which is effectively a guest in a web page), does not have the right to resize itself (that would be like your guest visiting, and knocking out a wall for 'a little more space').

Questions

These are not rhetorical questions. Another way to put that is: I expect to see answers to these questions.

  1. Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.
  2. Why AWT rather than Swing? See my answer on Swing extras over AWT for many good reasons to abandon using AWT components.

Future problems

sharedImage = new ImageIcon("sameer15.jpg" ).getImage(); 

You'll begin to discover just how wrong this is when you see AccessControlException in the Java Console when testing in Applet Viewer from the command line, or embedded in a web page. ..But we can deal with that in a separate Q&A. ;)

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

The simple answer in Eclipse is:

If you have multiple java classes open and you "run" the program from a class other than the one with init() and setSize(550,650) then the window will default to small.

Open the class with the setSize() and run it from there. Problem solved.

This drove me nuts until I figured it out.