1

enter image description here

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

jamierocks
  • 685
  • 5
  • 16

3 Answers3

7

A key to success with Swing layouts is to avoid setLayout(null) and to pack() the enclosing Window. This lets the contained components adopt their preferred sizes, as shown below. To avoid this pitfall, don't invoke 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);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod May 08 '14 at 19:56
2

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

enter image description here

jamierocks
  • 685
  • 5
  • 16
0

change your constructor definition to (EDITED)

public Frame() {
    setTitle("Clock");
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    relocalize();
    setSize(850, 650);
    setLocationRelativeTo(null);
    setVisible(true);
}
rahul pasricha
  • 931
  • 1
  • 14
  • 35
  • I'm afraid this doesn't work, when you use pack() you size the frame around the components within the frame, making the frame a title bar and nothing else – jamierocks May 08 '14 at 18:10
  • try this public Frame() { setTitle("Clock"); setSize(FRAME_WIDTH, FRAME_HEIGHT); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); relocalize(); setSize(850, 650); setLocationRelativeTo(null); setVisible(true); } – rahul pasricha May 08 '14 at 18:15
  • 3
    @rahulpasricha edit your original answer instead of posting new code as comment. – kryger May 08 '14 at 18:17