简体   繁体   English

设置jLabel的大小

[英]Set size for jLabel in swing

I want to set a jLabel with dimension(50,75) inside a JFrame . 我想在JFrame设置一个带有dimension(50,75) jLabel dimension(50,75)jLabel

I tried using 我尝试使用

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

But it doesnt work. 但它不起作用。 How can I do this? 我怎样才能做到这一点?

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: setPreferredSize确实会更改标签的大小,您应该尝试使用setBorder方法绘制边框以验证新大小,但字体大小不会更改,如果您想使用大字体,请尝试调用setFont并设置新字体大小,此处以以下代码开头:

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);
    }
}

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();  
  }  

Use JLabel setBounds(x, y, width, height) method 使用JLabel setBounds(x, y, width, height)方法

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. 左上角的新位置由x和y指定,新大小由宽度和高度指定。

You have to use a LayoutManager and then you have to call the pack Method. 您必须使用LayoutManager,然后必须调用pack方法。

The LayoutManager tries to arrange the subcomponents and pack() gets the preferred sizes of these subcomponents. LayoutManager尝试排列子组件,然后pack()获得这些子组件的首选大小。

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. 如果任意一个尺寸小于上一次调用setMinimumSize方法指定的最小尺寸,则窗口的宽度和高度将自动放大。 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. 计算窗口大小后,将对其进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM