5

I want to set a jLabel with dimension(50,75) inside a JFrame.

I tried using

label.setPreferredSize(new Dimension(50, 75)); 

But it doesnt work. How can I do this?

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
ddk
  • 159
  • 1
  • 4
  • 13

4 Answers4

5

setPreferredSize changes really the size of the label you should just try to draw it border using setBorder method to verify the new size, but the font size is not changed, if you want to have big font try to call setFont and set the new font size, here some code to start with:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

public class Test {
    public static void main(String[] args) {
        JFrame t = new JFrame();
        t.setBounds(100, 100, 500, 400);
        JLabel l = new JLabel("Hello");
        // new font size is 20
        l.setFont(new Font(l.getFont().getName(), l.getFont().getStyle(), 20));
        // draw label border to verify the new label size
        l.setBorder(new LineBorder(Color.BLACK));
        // change label size
        l.setPreferredSize(new Dimension(200, 200));
        t.getContentPane().setLayout(new FlowLayout());
        t.add(l);
        t.setVisible(true);
    }
}
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
  • @ddk no the label size is (200,200), it's the frame which have the size (500,400), setPreferredSize(new Dimension(200, 200)) method change the size label, but it depends on the layout used, you should use `FlowLayout` on the panel which contains your label by using `getContentPane().setLayout(new FlowLayout())`, if you let your default layout which is `BorderLayout` the label size will be the same as the frame. – Naruto Biju Mode Nov 24 '14 at 11:08
0

Simple Example:

class Testing extends JFrame  
{  
  int counter = 1;  
  javax.swing.Timer timer;  
  public Testing()  
  {  
    setSize(100,50);  
    setLocation(300,100);  
    setDefaultCloseOperation(EXIT_ON_CLOSE);  
    JPanel p = new JPanel();  
    final JLabel label = new JLabel("1",JLabel.CENTER);  
    label.setBorder(BorderFactory.createLineBorder(Color.BLACK));  
    Dimension d = label.getPreferredSize();  
    //label.setPreferredSize(new Dimension(d.width+60,d.height));//<-----------  
    p.add(label);  
    getContentPane().add(p);  
    ActionListener al = new ActionListener(){  
      public void actionPerformed(ActionEvent ae){  
        counter *= 10;  
        label.setText(""+counter);  
        if(counter > 1000000) timer.stop();}};  
    timer = new javax.swing.Timer(1000,al);  
    timer.start();  
  }  
Junaid
  • 2,572
  • 6
  • 41
  • 77
-1

Use JLabel setBounds(x, y, width, height) method

Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height.

Vishvesh Phadnis
  • 2,448
  • 5
  • 19
  • 35
  • 2
    `null` `LayoutManager` is bad idea. – alex2410 Nov 24 '14 at 10:49
  • Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). The last thing we need is more bad advice like this. -1 – Andrew Thompson Nov 24 '14 at 11:17
-1

You have to use a LayoutManager and then you have to call the pack Method.

The LayoutManager tries to arrange the subcomponents and pack() gets the preferred sizes of these subcomponents.

public void pack()

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method. If the window and/or its owner are not displayable yet, both of them are made displayable before calculating the preferred size. The Window is validated after its size is being calculated.

Community
  • 1
  • 1
duffy356
  • 3,678
  • 3
  • 32
  • 47
  • 1
    Why do you think `pack()` helps here? Size depends on `LayoutManager`.For solving that problem OP must post `MCVE`. – alex2410 Nov 24 '14 at 10:53