2017-12-20 28 views
0

swing에서 JTable에 사용자 정의 글꼴을로드하려고합니다.자바 글꼴 createFont가 작동하지 않습니다 (IOException)

private void carregar_font(){ 
    try { 
     URL fontName = getClass().getResource("fonts/open.ttf"); 
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontName.toString()))); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       } catch(FontFormatException e) { 
     e.printStackTrace(); 
     } 
    } 

이 나에게 IOException가 제공된다 이 내가 할 방법이다. 어떠한 제안? 감사합니다

+1

getResourceAsStream 및 other Font.createFont method을 사용하십시오. 정확한 [mcve]와 정확한 스택 추적을 게시 하시겠습니까? – Frakcool

+0

당신은 메이븐을 사용하고 있습니까? –

+0

@Mdkhirulashik, noope –

답변

0

getClass().getResource는 파일 이름이 아닌 URL을 반환합니다. new File으로 직접 전달하면 유효하지 않거나 존재하지 않는 파일이됩니다.

리소스 URL은 파일을 가리 키지 않습니다.

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
try (InputStream fontStream = getClass().getResourceAsStream("fonts/open.ttf")) { 
    ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, fontStream)); 
}