Label的字体大小怎么设置?

Label的字体大小怎么设置?,第1张

表示要写的参数,如:new Font("genericFamily",15) 15表示字的大小
用惯了可视化编写,现在没有可视化工具,写一个简单的改变字体大小的程序都要费一番周折,不过也好对以后写组件有帮助

思路:首先判断默认字体是不是满足条件。不满足那就变小直到满足

经过上面的方法已经获取了合适的字体大小和间距然后在合适的时机调用即可

下面就可以开心的设置对应的label文本或者 富文本了 。到此结束

import javaawtContainer;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingButtonGroup;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJRadioButton;
import javaxswingJTextField;
public class Choice extends JFrame{
private JRadioButton button1 = null;
private JRadioButton button2 = null;
private JTextField field1 = null;
private JTextField field2 = null;
public Choice(){
setSize(500,500);
setLocation(100,50);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setContentPane(getPanel());
setVisible(true);
}
private Container getPanel() {
JPanel panel = new JPanel();
//控制比如按钮的位置的话,把按钮所在的面板(按钮添加到哪里,哪里的设置为null)布局设置为null,
panelsetLayout(null);
button1 = new JRadioButton("A");
button2 = new JRadioButton("B");
ButtonGroup group = new ButtonGroup();
field1 = new JTextField();
field2 = new JTextField();
groupadd(button1);
groupadd(button2);
//给按钮添加点击事件的监听
button1addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field1setText("选择了:A");
field2setText("选择了:A");
}
});
button2addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field1setText("选择了:B");
field2setText("选择了:B");
}
});

//设置按钮在面板的为主,第一是左上角的x坐标,第二个是左上角y左边,第三个是控件宽度,第四个是高度
button1setBounds(60, 10, 40,40);
button2setBounds(100, 10, 40,40);
field1setBounds(60, 50, 80,20);
field2setBounds(60, 80, 80,20);
paneladd(button2);
paneladd(button1);
paneladd(field2);
paneladd(field1);
return panel;
}
public static void main(String[] args) {
new Choice();
}
}

下面分两种情况考虑:
1、UILabel宽度不变,根据字体多少,自动调整UILabel的高度,并折行显示。
代码如下:
[cpp] view plaincopy
1 UILabel label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 200, 20)];
2 labelfont = [UIFont boldSystemFontOfSize:200f]; //UILabel的字体大小
3 labelnumberOfLines = 0; //必须定义这个属性,否则UILabel不会换行
4 labeltextColor = [UIColor whiteColor];
5 labeltextAlignment = NSTextAlignmentLeft; //文本对齐方式
6 [label setBackgroundColor:[UIColor redColor]];
7
8 //宽度不变,根据字的多少计算label的高度
9 NSString str = @"可以更改此内容进行测试,宽度不变,高度根据内容自动调节";
10 CGSize size = [str sizeWithFont:labelfont constrainedToSize:CGSizeMake(labelframesizewidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
11 //根据计算结果重新设置UILabel的尺寸
12 [label setFrame:CGRectMake(0, 10, 200, sizeheight)];
13 labeltext = str;
14
15 [selfview addSubview:label];
16 [label release];
2、UILabel高度不变,根据字体多少,自动调整UILabel的宽度,并折行显示
代码如下:
[cpp] view plaincopy
1 UILabel label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 20, 20)];
2 labelfont = [UIFont boldSystemFontOfSize:200f]; //UILabel的字体大小
3 labelnumberOfLines = 0; //必须定义这个属性,否则UILabel不会换行
4 labeltextColor = [UIColor whiteColor];
5 labeltextAlignment = NSTextAlignmentLeft; //文本对齐方式
6 [label setBackgroundColor:[UIColor redColor]];
7
8 //高度固定不折行,根据字的多少计算label的宽度
9 NSString str = @"高度不变获取宽度,获取字符串不折行单行显示时所需要的长度";
10 CGSize size = [str sizeWithFont:labelfont constrainedToSize:CGSizeMake(MAXFLOAT, labelframesizeheight)];
11 NSLog(@"sizewidth=%f, sizeheight=%f", sizewidth, sizeheight);
12 //根据计算结果重新设置UILabel的尺寸
13 [label setFrame:CGRectMake(0, 10, sizewidth, 20)];
14 labeltext = str;
15
16 [selfview addSubview:label];
17 [label relea

1、打开Myeclipse的相关界面,在Window那里点击Preferences。

2、d出设置的对话框,选择General下的Appearance进入。

3、点击Colors and Fonts按钮,需要在右侧选择Java。

4、选择Java Editor Text Font,并点击Edit。

5、通过设置对应的参数以后,直接确定返回。

6、这样一来会看到图示的结果,即可设置JLabel的字体样式,大小,颜色了。

我知道怎么把lable的背景颜色设为透明,不知道对你有没有用
选中lable控件->点击backcolor属性的下拉例表,->选择web选项卡->选择transparent,也就是web选项卡里的第一个
希望对你有用


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13400957.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-29
下一篇 2023-07-29

发表评论

登录后才能评论

评论列表(0条)

保存