JLabel文本被切断 [英] JLabel text being cut off
本文介绍了JLabel文本被切断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从上面的图片可以看出一些文字正在被切断:(
代码:
package malgm.school .clockui.ui;
$ b $ import java.awt。*;
import java.awt.event.ActionEvent; $ b $ import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing。*;
import malgm.school.clockui.ClockUI;
import malgm.school.clockui。 ResourceLoader;
public class ClockFrame extends JFrame {
private static final long serialVersionUID = 1L;
public final static int FRAME_WIDTH = 600;
public final static int FRAME_HEIGHT = 200;
public ClockFrame(){
setTitle(Clock); $ b $ setSize(FRAME_WIDTH,FRAME_HEIGHT);
setResizable (false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
relocalize();
}
$ b public void relocalize(){
//擦除控件
this.getContentPane()。removeAll();
this.setLayout(null);
initComponents();
@SuppressWarnings(unused)
private void initComponents(){
setLayout(new BorderLayout());
JPanel header = new JPanel();
header.setLayout(new BoxLayout(header,BoxLayout.LINE_AXIS));
JPanel section = new JPanel();
section.setLayout(new BoxLayout(section,BoxLayout.LINE_AXIS));
JLabel label = new JLabel(The time is ...);
JButton speakButton = new JButton(Speak);
speakButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Runtime rt = Runtime.getRuntime();
try {
Process pr = rt.exec(ClockUI.dir +/SpeakingClock.exe);
} catch(IOException e1){
e1.printStackTrace();
}
}
});
JLabel time = new JLabel(test);
ResourceLoader resLoader = new ResourceLoader();
time.setFont(resLoader.getFont(ResourceLoader.FONT_DIGITAL,72));
section.add(Box.createHorizontalGlue());
section.add(time);
section.add(Box.createHorizontalGlue());
header.add(label);
header.add(Box.createHorizontalGlue());
header.add(speakButton);
add(header,BorderLayout.PAGE_START);
add(section,BorderLayout.CENTER);
FONT: http://www.dafont.com/digital-7.font
任何帮助将不胜感激
解决方案在
$ b $之后b
JLabel time = new JLabel(test);
ResourceLoader resLoader = new ResourceLoader();
time.setFont(resLoader.getFont(ResourceLoader.FONT_DIGITAL,72));
添加 time.setBorder(BorderFactory.createEmptyBorder(0,20, 20));
看起来像:
JLabel time = new JLabel(test);
ResourceLoader resLoader = new ResourceLoader();
time.setFont(resLoader.getFont(ResourceLoader.FONT_DIGITAL,72));
time.setBorder(BorderFactory.createEmptyBorder(0,20,20,20));
As you can see from the image above some of the text is being cut off :(
Code:
package malgm.school.clockui.ui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
import malgm.school.clockui.ClockUI;
import malgm.school.clockui.ResourceLoader;
public class ClockFrame extends JFrame {
private static final long serialVersionUID = 1L;
public final static int FRAME_WIDTH = 600;
public final static int FRAME_HEIGHT = 200;
public ClockFrame() {
setTitle("Clock");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
relocalize();
}
public void relocalize() {
//Wipe controls
this.getContentPane().removeAll();
this.setLayout(null);
initComponents();
}
@SuppressWarnings("unused")
private void initComponents() {
setLayout(new BorderLayout());
JPanel header = new JPanel();
header.setLayout(new BoxLayout(header, BoxLayout.LINE_AXIS));
JPanel section = new JPanel();
section.setLayout(new BoxLayout(section, BoxLayout.LINE_AXIS));
JLabel label = new JLabel("The time is...");
JButton speakButton = new JButton("Speak");
speakButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Runtime rt = Runtime.getRuntime();
try {
Process pr = rt.exec(ClockUI.dir + "/SpeakingClock.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
JLabel time = new JLabel("test");
ResourceLoader resLoader = new ResourceLoader();
time.setFont(resLoader.getFont(ResourceLoader.FONT_DIGITAL, 72));
section.add(Box.createHorizontalGlue());
section.add(time);
section.add(Box.createHorizontalGlue());
header.add(label);
header.add(Box.createHorizontalGlue());
header.add(speakButton);
add(header, BorderLayout.PAGE_START);
add(section, BorderLayout.CENTER);
}
}
FONT: http://www.dafont.com/digital-7.font
Any help will be greatly appreciated
解决方案 After
JLabel time = new JLabel("test");
ResourceLoader resLoader = new ResourceLoader();
time.setFont(resLoader.getFont(ResourceLoader.FONT_DIGITAL, 72));
Add time.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
After it will look like:
JLabel time = new JLabel("test");
ResourceLoader resLoader = new ResourceLoader();
time.setFont(resLoader.getFont(ResourceLoader.FONT_DIGITAL, 72));
time.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
这篇关于JLabel文本被切断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文