Results 1 to 7 of 7
  1. #1
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Japplet setSize()

    I am trying to set the size of my JApplet and whenever i use the method setSize(); nothing happens.

    I do NOT get any errors and am wondering if there is a different method for this or not.

    any help is appreciated.

    Heres the Program:


    Java Code:
    package ContactApp;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    
    public class paintJApplet extends JApplet {
    
    	Color white = Color.WHITE;
    	Color grey = Color.gray;
    	Color c = Color.white;
    
    	JButton pen = new JButton("Pencil");
    	JButton era = new JButton("Erase");
    	JButton plus = new JButton("+");
    	JButton minus = new JButton("-");
    	JButton color = new JButton("Color");
    
    
    
    	public void init() {
    
    		Container frame = getContentPane();
    		
    		frame.setLayout(new FlowLayout());
    		frame.setBackground(Color.RED);
    		frame.setSize(200,100);
    		frame.add(pen);
    		
    		frame.setVisible(true);
    	}
    
    	public void paintComponent(Graphics g) {
    
    	}
    
    
    
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Japplet setSize()

    I am not an applet expert, but I believe that the applet's size is set from the HTML code that calls the applet.

  3. #3
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Japplet setSize()

    I know in the java.applet. there is a method (which is setSize) used to set the size of the applet. I also know that the Applet size can be changed from the html code. But i want my program to open to the proper size every time i run it. Also, the buttons added are not in the proper position when the applet is not sized properly.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Japplet setSize()

    Quote Originally Posted by ninjaturtlez View Post
    I know in the java.applet. there is a method (which is setSize) used to set the size of the applet.
    Yes, JApplet has this method, inherited from Component, but how do you know that it actually works, that it sets the size of the applet? The applet itself isn't the top level window, but rather a component held by a top-level window created by the browser, and so I believe that it's the browser that must set the applet's size, not the java code.

    I also know that the Applet size can be changed from the html code. But i want my program to open to the proper size every time i run it. Also, the buttons added are not in the proper position when the applet is not sized properly.
    One possible solution is to have your Java application call javascript code so that it could possibly talk to the browser, but I've never done this. As for improving components size and placement, a lot of this will depend on the layouts that your GUI uses. If you use flexible layout managers, you'll have an easier time with displaying your GUI in different sizes.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Japplet setSize()

    I also know that the Applet size can be changed from the html code. But i want my program to open to the proper size every time i run it.
    So, given Fubarable's point that the HTML code determines the size, isn't the conclusion that your HTML code should ensure the "proper size"?

    Javascript would only seem to be called for if the application decides it wants to change the size of the window within which it is appearing. If there is, in advance, a well defined "proper size", then that's what WIDTH=/HEIGHT= are for.

  6. #6
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Japplet setSize()

    Dontworry i figured it out myself. You need to do:

    Java Code:
    frame name.getContentPane().setSize(width,height);

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: Japplet setSize()

    Does that code compile? The syntax looks wrong.

Similar Threads

  1. setSize without frames
    By Javaer in forum New To Java
    Replies: 1
    Last Post: 01-09-2011, 02:41 AM
  2. setSize on BoxLayout
    By smray7 in forum New To Java
    Replies: 7
    Last Post: 12-15-2010, 11:18 PM
  3. setSize() not working?
    By AedonetLIRA in forum Java Applets
    Replies: 3
    Last Post: 12-02-2010, 05:17 AM
  4. When to use setSize()
    By Lil_Aziz1 in forum New To Java
    Replies: 7
    Last Post: 05-29-2010, 03:26 PM
  5. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 06:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •