1

I have being trying to display a phrase with a custom font onto a JFrame but I only end up with the text without the font. When I compile the following code:

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

public class JavaPractice{
  public static void main(String[]args){
    JFrame frame = new JFrame("Java Practice!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(label);
    frame.pack();
    frame.getContentPane().setBackground(Color.decode("#101010"));
    frame.setSize(720,480);
    frame.setResizable(false);
    frame.setVisible(true);
    JLabel label = new JLabel("Press Enter To Continue");
    Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("PixelFont.ttf"));
    label.setFont(font.deriveFont(Font.BOLD, 12f));
  }
}

I get feedback from the compiler saying that getClass() is not a non-static method and cannot be referenced from a static context.

It also says that it cannot find the symbol frame.add(label);

Please be specific in your answer since I am not that advanced in Java.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
nico.user
  • 483
  • 2
  • 5
  • 15
  • 4
    `getClass() is not a non-static method and cannot be referenced from a static context.` Did you search the forum/web to see what that message means? `It also says that it cannot find the symbol frame.add(label);` - because you are trying to add the label to the frame before you define the label. – camickr Dec 18 '15 at 02:40
  • 3
    You are calling `frame.add(label);` before you create the JLabel (`JLabel label = new JLabel("Press Enter To Continue");`) so it does not exist yet. Regarding the getClass(), see this: http://stackoverflow.com/questions/8275499/how-to-call-getclass-from-a-static-method-in-java – MC10 Dec 18 '15 at 02:41
  • 1
    `JavaPractice,class.getResourceAsStream`, but you may need to supply a absolute path to the resource (I can never remember) – MadProgrammer Dec 18 '15 at 02:45
  • (1) Calling `pack` and then `setSize` renders the first useless. (2) Call `setVisible` after you set everything up, including the font. – user1803551 Dec 18 '15 at 14:18
  • Thanks for your comments which helped me find the right solution, now its works just how I wanted it. – nico.user Dec 18 '15 at 18:39

1 Answers1

4

I've changed your code based on the things we pointed out in the comments. Where is your font file located?

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

public class JavaPractice{
    public static void main(String[]args){
        JFrame frame = new JFrame("Java Practice!");
        JLabel label = new JLabel("Press Enter To Continue");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(label);
        frame.pack();
        frame.getContentPane().setBackground(Color.decode("#101010"));
        frame.setSize(720,480);
        frame.setResizable(false);
        frame.setVisible(true);
        try{
            Font font = Font.createFont(Font.TRUETYPE_FONT, JavaPractice.class.getResourceAsStream("PixelFont.ttf"));
            label.setFont(font.deriveFont(Font.BOLD, 12f));
        }
        catch(Exception e){}
    }
}

The result currently is: Output

MC10
  • 552
  • 1
  • 12
  • 26
  • my font is just in my documents in the same place where my java file is, there are just there and not in a folder, so do I have to put it in a folder? – nico.user Dec 18 '15 at 18:35
  • Thank you for fixing my code, now it works just I wanted to. – nico.user Dec 18 '15 at 18:38
  • 1
    @nico.user No problem, glad it's working. You can select this as the accepted answer if it solved your problem. – MC10 Dec 18 '15 at 19:09