2012-12-17 6 views
2

JLabel 내에 사용자 정의 글꼴을 표시하려고하는데 작성시 매우 작은 텍스트로 표시됩니다. 나는 텍스트가 너무 작기 때문에 내가 지정한 글꼴을 사용하고 있는지 알지 못한다. 여기에 the font that I used입니다. 그래서 제가 글꼴을 너무 작게 만드는 것입니까?사용자 정의 JLabel 글꼴이 너무 작습니다.

package sscce; 

import java.awt.Font; 
import java.awt.FontFormatException; 
import java.io.File; 
import java.io.IOException; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class Main extends JFrame{ 

    public Main(){ 
     this.setSize(300, 300); 
     this.setResizable(false); 
     this.setLocationRelativeTo(null); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     GameFont fnt = new GameFont("/home/ryan/Documents/Java/Space Shooters/src/media/fonts/future.ttf", 20); 
     Label lbl = fnt.createText("Level 1 asdf sadf saf saf sf "); 

     this.add(lbl); 
    } 

    public static void main(String[] args){ 
     Main run = new Main(); 
    } 

    public class GameFont{ 

     protected Font font; 

     public GameFont(String filename, int fontSize){ 
      try{ 
       File fontFile = new File(filename); 
       font = Font.createFont(Font.TRUETYPE_FONT, fontFile); 
       font.deriveFont(fontSize); 
      }catch(FontFormatException | IOException e){ 
      } 
     } 

     public Label createText(String text){ 
      Label lbl = new Label(font); 
      lbl.setText(text); 
      return lbl; 
     } 
    } 

    public class Label extends JLabel{ 

     public Label(Font font){ 
      this.setFont(font); 
     } 
    } 
} 

답변

3

Font API, deriveFont (...) 메소드를 다시 살펴보십시오. 당신은 int로 매개 변수가 전달되는 경우, 방법이를 기대하기 때문에 글꼴의 스타일 (굵게, 기울임 꼴을 설정하는 의미하는 플로트 아닌 크기에 대한 INT에 전달하려면 , 밑줄), 크기가 아닙니다. 글꼴 deriveFont(...) 메서드로을 반환해야합니다.

그래서이 변경이에

public GameFont(String filename, int fontSize){ 
     try{ 
      File fontFile = new File(filename); 
      font = Font.createFont(Font.TRUETYPE_FONT, fontFile); 
      font.deriveFont(fontSize); 
     }catch(FontFormatException | IOException e){ 
     } 
    } 

: 또한

public GameFont(String filename, float fontSize){ 
     try{ 
      File fontFile = new File(filename); 
      font = Font.createFont(Font.TRUETYPE_FONT, fontFile); 
      font = font.deriveFont(fontSize); 
     }catch(FontFormatException | IOException e){ 
      e.printStackTrace(); // **** 
     } 
    } 

, 결코 당신이하고있는 같은 예외를 무시!

+0

그래서 저는 이것을 float로 변경했고, 여전히 작습니다. –

+0

@RyanNaddy : 편집을 참조하십시오. deriveFont 메소드로부터 돌려 주어진 Font를 사용할 필요가 있습니다. –

+0

좋습니다. 감사! 변수를 재 할당하지 않았습니다! –

관련 문제