Results 1 to 6 of 6
  1. #1
    y2k
    y2k is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Swing -> Applet size problem

    My problem is:
    i am setting my JApplet's size to : WIDTH=400 and HEIGHT=300
    on applet tag in html page.

    when i execute my code on internet explorer 8, the sizes are:
    java.awt.Dimension[width=380,height=285]
    java.awt.Dimension[width=140,height=140]

    but in mozilla 10.0.2 the sizes are:
    java.awt.Dimension[width=400,height=300]
    java.awt.Dimension[width=140,height=140]

    this is my code:
    Java Code:
    public class MainClass extends JApplet implements Runnable {
    		JPanel MainPanel = new JPanel();
    		public void init() {
    		MainPanel.setLayout(null);
    		MainPanel.setOpaque(true);
    		MainPanel.setBackground(Color.BLACK);
    		JLabel xLabel = new JLabel("Hi");
    		MainPanel.add(xLabel);
    		xLabel.setBounds(0, 0, 140, 140);
    		//xLabel.setPreferredSize(new Dimension(140, 140));
    		xLabel.setOpaque(true);
    		xLabel.setBackground(Color.WHITE);
    		this.add(MainPanel);
    		Dimension a = getSize();
    		System.out.println(a.toString());
    		Dimension b = xLabel.getSize();
    		System.out.println(b.toString());
    		}
    		public void run() { }
    };
    how can i fix this ?

    or how could i size my JLabel object?
    (as you can see, there is a wrong about applet and JLabel sizes.)
    Note : i must use setLayout nulled.

    thanks.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Swing -> Applet size problem

    First off, learn how to format code and name variables for readability: Code Conventions for the Java Programming Language: Contents

    Next, learn to use layout managers and stop relying on absolute positioning: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Swing -> Applet size problem

    Quote Originally Posted by y2k View Post
    Note : i must use setLayout nulled.
    'must'? is that a precondition or just a sign of laziness to acquire and use relevant knowledge?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    y2k
    y2k is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Swing -> Applet size problem

    Quote Originally Posted by DarrylBurke View Post
    First off, learn how to format code and name variables for readability: Code Conventions for the Java Programming Language: Contents
    Next, learn to use layout managers and stop relying on absolute positioning: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    * First off, learn how to format code and name variables for readability:
    -> yes java programmers USUALLY using camel variable names, but i don't wanna use. I'm not a newbe,
    so don't say to me: "learn how to format code". If there is a forum rule;
    So sorry for this format.

    * Next, learn to use layout managers and stop relying on absolute positioning:
    -> if you are making a tank game, a table game or a card game, Which layout could be use?
    Note : you can use layout managers, but view of game can be very bad, and i don't wanna program a bad game (for view).

    if you will say to me "use layout manager" again and again; You can solve my problem FROM A WAY. And you know (i think), there can be a lot of possible ways on programming ?

    i am making an applet "game".. so i "must" use setLayout null, i think.

    Thanks for reply anyway.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default Re: Swing -> Applet size problem

    Programmers stick to the Java naming standard the same reason writers don't write in all caps. Sure, you could go against the grain, but it's only going to make it harder for other people to read your code, and it's going to be harder for you to understand, debug, and maintain your code on your own. Getting an attitude about it is pretty pointless.

    Programming a game does not automatically mean you have to use a null layout. In fact, it almost NEVER makes sense to use a null layout. If you're having a problem with the relative size of your components, aka the layout, then I'm not sure why you're scoffing at the suggestion to research layout managers.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    y2k
    y2k is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Swing -> Applet size problem

    my images have sizes and they are not standard (photoshop images). I don't wanna resize with image resize methods.
    i don't wanna make a sizeable applet (i mean, i want to use a standard size of main applet)

    anyway, thanks for your replies...
    i understood, you can't help me.

    thanks.

Similar Threads

  1. Swing applet problem
    By link6790 in forum New To Java
    Replies: 9
    Last Post: 05-24-2011, 03:31 PM
  2. Java applet size problem
    By madnaelo in forum Java Applets
    Replies: 1
    Last Post: 12-06-2010, 02:39 AM
  3. Applet JTextField Size Problem
    By a13w in forum New To Java
    Replies: 4
    Last Post: 09-19-2009, 08:52 AM
  4. [SOLVED] swing dialogbox size
    By MK12 in forum New To Java
    Replies: 26
    Last Post: 02-13-2009, 06:08 PM
  5. setting applet's size
    By nikita in forum Java Applets
    Replies: 9
    Last Post: 10-01-2008, 05:41 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
  •