java - JLabel 文本被截断

标签 java swing fonts jframe jlabel

enter image description here

正如您从上图中看到的,一些文本被截断了:( 代码:

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

}

字体:http://www.dafont.com/digital-7.font

任何帮助将不胜感激

最佳答案

Swing 成功的关键 layouts是为了避免 setLayout(null)pack() 封闭的 Window。这让包含的组件采用它们的首选大小,如下所示。为了避免这种情况 pitfall , 不要调用 setResizable(false)

image

import java.awt.*;
import javax.swing.*;

/** @see https://stackoverflow.com/a/23551260/230513 */
public class ClockFrame extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ClockFrame cf = new ClockFrame();
            }
        });
    }

    public ClockFrame() {
        setTitle("Clock");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        initComponents();
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    @SuppressWarnings("unused")
    private void initComponents() {
        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");
        JLabel time = new JLabel("00:00");
        time.setFont(time.getFont().deriveFont(72f));
        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);
    }
}

关于java - JLabel 文本被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23549089/

相关文章:

java - 在 Java 中从 ArrayList 中删除所有项目的最佳实践

java - unity firebase 数据库检索数据

java - jsoup clean 包含不需要的回车符

java - 当我使框架的背景透明时,ImageIcon 停止变化?

java - JPanel 不使用 setSize 和 setPrefferedSize

css - 斜体覆盖标准?

android - 获取小部件的宽度

java - 使用 if 与单选按钮和文本一起归档?

java - Java 图像即将出现在 NPE 中

java - 保存并加载字体/字体颜色