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) );
                               .
                               .
                               .
komlaaaAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>>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:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
It's difficult to say without really knowing what effect you want to create
0
 
objectsConnect With a Mentor Commented:
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
 
objectsConnect With a Mentor Commented:
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
 
billyeaConnect With a Mentor Commented:
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:
8-)
0
 
objectsCommented:
the accepted answer is a dupe of what I already said :D
0
All Courses

From novice to tech pro — start learning today.