【问题标题】:JLabel text being cut offJLabel 文本被截断
【发布时间】:2014-05-08 17:52:14
【问题描述】:

从上面的图片可以看出,一些文字被截断了:( 代码:

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

任何帮助将不胜感激

【问题讨论】:

  • @Reimeus 你能帮我找个教程或例子吗?我已经在使用 BorderLayout 和 BoxLayout
  • 调用pack() 调整组件大小
  • @nachokk 我会把这个放在哪里?
  • 建议不要调用你的类 Frame ,你已经导入了`java.awt.Frame`
  • 好吧,我没想到,我正在教一些小伙伴如何制作时钟 :) 一个小伙伴和我一起用 C# 制作了一个会说话的时钟。

标签: java swing fonts jframe jlabel


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

之后

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 time = new JLabel("test");
ResourceLoader resLoader = new ResourceLoader();
time.setFont(resLoader.getFont(ResourceLoader.FONT_DIGITAL, 72));
time.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));

【讨论】:

    【解决方案3】:

    将您的构造函数定义更改为(已编辑)

    public Frame() {
        setTitle("Clock");
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        relocalize();
        setSize(850, 650);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    

    【讨论】:

    • 恐怕这不起作用,当您使用 pack() 时,您会围绕框架内的组件调整框架大小,使框架成为标题栏,仅此而已
    • 试试这个 public Frame() { setTitle("Clock"); setSize(FRAME_WIDTH, FRAME_HEIGHT);可调整大小(假);设置默认关闭操作(EXIT_ON_CLOSE);重新定位();设置大小(850、650); setLocationRelativeTo(null);设置可见(真); }
    • @rahulpasricha 编辑您的原始答案,而不是发布新代码作为评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    相关资源
    最近更新 更多