• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 887
  • Last Modified:

format JLabel

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
komlaaa
Asked:
komlaaa
  • 4
  • 3
  • 2
  • +1
4 Solutions
 
CEHJCommented:
The size is dependent on the layout manager unless there is a null layout.
0
 
komlaaaAuthor Commented:
So how should i correct this code?
0
 
CEHJCommented:
It's difficult to say without really knowing what effect you want to create
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
objectsCommented:
aDie = new JLabel( String.valueOf(top)  );
aDie.setIOpaque(true);
aDie.setBackground( Color.white );
aDie.setPreferredSize( new Dimension(400, 200) );
0
 
komlaaaAuthor Commented:
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
 
objectsCommented:
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
 
billyeaCommented:
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
 
CEHJCommented:
>>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
 
CEHJCommented:
8-)
0
 
objectsCommented:
the accepted answer is a dupe of what I already said :D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now