Java 在 Swing 中设置 jLabel 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27102647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set size for jLabel in swing
提问by ddk
I want to set a jLabel
with dimension(50,75)
inside a JFrame
.
我想设置一个jLabel
带dimension(50,75)
内JFrame
。
I tried using
我尝试使用
label.setPreferredSize(new Dimension(50, 75));
But it doesnt work. How can I do this?
但它不起作用。我怎样才能做到这一点?
采纳答案by Naruto Biju Mode
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);
}
}
回答by Vishvesh Phadnis
Use JLabel setBounds(x, y, width, height)
method
使用 JLabelsetBounds(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 指定,新大小由宽度和高度指定。
回答by duffy356
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. 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.
公共无效包()
使此窗口的大小适合其子组件的首选大小和布局。如果任一维度小于先前调用 setMinimumSize 方法指定的最小尺寸,则生成的窗口宽度和高度将自动放大。如果窗口和/或其所有者尚不可显示,则在计算首选大小之前将它们都设置为可显示。窗口在计算其大小后进行验证。
回答by Junaid
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();
}