Solved

format JLabel

Posted on 2004-10-22
840 Views
Last Modified: 2008-03-06
with the piece of code below, i am trying to from my JLabel, but i ma not seeing any change (backgroud color, size...) Please help.

========================== CODE =========================
 public Die()
    {
       
           
      setPreferredSize( new Dimension(350,350) );
      setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
      setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
     
      aDie = new JLabel( String.valueOf(top)  );
      aDie.setBackground( Color.white );
      aDie.setSize( new Dimension(400, 200) );
                               .
                               .
                               .
0
Question by:komlaaa
    10 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    The size is dependent on the layout manager unless there is a null layout.
    0
     

    Author Comment

    by:komlaaa
    So how should i correct this code?
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    It's difficult to say without really knowing what effect you want to create
    0
     
    LVL 92

    Assisted Solution

    by:objects
    aDie = new JLabel( String.valueOf(top)  );
    aDie.setIOpaque(true);
    aDie.setBackground( Color.white );
    aDie.setPreferredSize( new Dimension(400, 200) );
    0
     

    Author Comment

    by:komlaaa
    I was finally able to change the color with the rectification you provided.
    but i am unable to increase the size, i would like to be a square.

    =================Code======================
    setPreferredSize( new Dimension(350,350) );
          setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
          setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
         
         aDie = new JLabel( String.valueOf(top)  );
         aDie.setOpaque(true);
         aDie.setBackground( Color.white );
         aDie.setSize(300,  300);

    Thanks.
    0
     
    LVL 92

    Assisted Solution

    by:objects
    the size depends on the layout manager you are using
    you should be setting the preferred size (and/or man and max) and not using setSize().
    0
     
    LVL 2

    Assisted Solution

    by:billyea
    objects is right so here is a slight followup. If you are using a GUI editor, and if you aren't GET ONE 'cause they're GREAT, just set the values, if not, do not use a layout AT ALL (null layout) and use absolute positioning to move your component across the JFrame (or panel, or tabbedpane, or internalframe.....). Usually then the component will size to your specs.

    Hope this helped.
    0
     
    LVL 86

    Accepted Solution

    by:
    >>the size depends on the layout manager you are using

    That's already been said. Try something like the following:


    import java.awt.*;
    import javax.swing.*;

    public class F extends JFrame {

          private void setGui() {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                int top = 100;
                JLabel aDie = new JLabel(String.valueOf(top));
                aDie.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
                aDie.setOpaque(true);
                aDie.setBackground(Color.white);
                aDie.setPreferredSize(new Dimension(300, 300));
                getContentPane().add(aDie, BorderLayout.CENTER);
          }

          public static void main(String[] args) {
                F f = new F();
                f.setGui();
                f.pack();
                f.setVisible(true);
          }
    }
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0
     
    LVL 92

    Expert Comment

    by:objects
    the accepted answer is a dupe of what I already said :D
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    Suggested Solutions

    Title # Comments Views Activity
    Tips on building some plugins or small utility apps 3 40
    modThree challenge 4 42
    scoreUp challenge 14 26
    Fibonacci challenge 11 45
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now