[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

format JLabel

Posted on 2004-10-22
10
Medium Priority
?
878 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
Comment
Question by:komlaaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

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

Author Comment

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

Expert Comment

by:CEHJ
ID: 12385754
It's difficult to say without really knowing what effect you want to create
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Assisted Solution

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

Author Comment

by:komlaaa
ID: 12387360
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
objects earned 375 total points
ID: 12387448
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
billyea earned 75 total points
ID: 12387481
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:
CEHJ earned 300 total points
ID: 12388048
>>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
ID: 12388573
8-)
0
 
LVL 92

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.
Suggested Courses

650 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