2014-11-19 4 views
1

자바로 텍스트 편집기를 만들고 싶습니다. 여기에 내가 지금까지 쓴 것입니다 :JTextArea의 글꼴 설정

package com.thundercrust.applications; 

import java.awt.BorderLayout; 

public class TextEditor implements ActionListener{ 

private static String[] fontOptions = {"Serif", "Agency FB", "Arial", "Calibri", "Cambrian", "Century Gothic", "Comic Sans MS", "Courier New", "Forte", "Garamond", "Monospaced", "Segoe UI", "Times New Roman", "Trebuchet MS", "Serif"}; 
private static String[] sizeOptions = {"8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28"}; 

ImageIcon newIcon = new ImageIcon("res/NewIcon.png"); 
ImageIcon saveIcon = new ImageIcon("res/SaveIcon.png"); 
ImageIcon openIcon = new ImageIcon("res/OpenIcon.png"); 
ImageIcon fontIcon = new ImageIcon("res/FontIcon.png"); 
ImageIcon changeFontIcon = new ImageIcon("res/ChangeFontIcon.png"); 

JButton New = new JButton(newIcon); 
JButton Save = new JButton(saveIcon); 
JButton Open = new JButton(openIcon); 
JButton changeFont = new JButton(changeFontIcon); 

JLabel fontLabel = new JLabel(fontIcon); 
JLabel fontLabelText = new JLabel("Font: "); 
JLabel fontSizeLabel = new JLabel("Size: "); 

JComboBox <String> fontName = new JComboBox<>(fontOptions); 
JComboBox <String> fontSize = new JComboBox<>(sizeOptions); 

JToolBar tool = new JToolBar(); 

JTextArea texty = new JTextArea(); 
JScrollPane scroll = new JScrollPane(texty); 

private static final int WIDTH = 1366; 
private static final int HEIGHT = WIDTH/16 * 9; 

private static String name = "Text Editor"; 

private static JFrame frame = new JFrame(); 

public void Display() { 
    frame.setTitle(name); 
    frame.setSize(WIDTH, HEIGHT); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 
    frame.setResizable(false); 
    frame.setVisible(true); 

    New.addActionListener(this); 
    New.setToolTipText("Creates a new File"); 
    Save.addActionListener(this); 
    Save.setToolTipText("Saves the current File"); 
    Open.addActionListener(this); 
    Open.setToolTipText("Opens a file"); 
    changeFont.addActionListener(this); 
    changeFont.setToolTipText("Change the Font"); 

    fontLabel.setToolTipText("Font"); 

    fontLabelText.setToolTipText("Set the kind of Font"); 
    fontSizeLabel.setToolTipText("Set the size of the Font"); 

    tool.add(New); 
    tool.addSeparator(); 
    tool.add(Save); 
    tool.addSeparator(); 
    tool.add(Open); 
    tool.addSeparator(); 
    tool.addSeparator(); 
    tool.addSeparator(); 
    tool.add(fontLabel); 
    tool.addSeparator(); 
    tool.add(fontLabelText); 
    tool.add(fontName); 
    tool.addSeparator(); 
    tool.add(fontSizeLabel); 
    tool.add(fontSize); 
    tool.addSeparator(); 
    tool.add(changeFont); 

    JPanel pane = new JPanel(); 
    pane.setLayout(new BorderLayout()); 
    pane.add(tool, "North"); 
    pane.add(scroll, "Center"); 
    frame.setContentPane(pane); 
} 

public static void main(String args[]) { 
    TextEditor editor = new TextEditor(); 
    editor.Display(); 
} 

public void actionPerformed(ActionEvent evt) { 
    String fontNameSet; 
    String fontSizeSetTemp; 
    int fontSizeSet; 
    Object source = evt.getSource(); 
    if(source == New) { 
     texty.setText(""); 
    } 
    else if(source == changeFont) { 
     fontNameSet = (String) fontName.getSelectedItem(); 
     fontSizeSetTemp = (String) fontSize.getSelectedItem(); 
     fontSizeSet = Integer.parseInt(fontSizeSetTemp); 
     System.out.println(fontNameSet + fontSizeSet); 
     scroll.setFont(new Font(fontNameSet, fontSizeSet, Font.PLAIN)); 
    } 

} 
} 

내 문제는 JTextArea에 대한 글꼴 설정으로입니다. 내 실제 프로그램에서 changeFont 버튼을 클릭해도 아무런 변화가 없습니다. 나는 무엇을해야합니까?

+6

당신은'가 JScrollPane입니다 scroll''의 글꼴을 변경하는 '. 변경 사항은 하위 요소에 반영되지 않으므로 텍스트 영역의 글꼴도 설정해야합니다. – BackSlash

답변

2
new Font(fontNameSet, fontSizeSet, Font.PLAIN) 

이것은 잘못된 순서로 arguments입니다. 그것은해야한다 :

new Font(fontNameSet, Font.PLAIN, fontSizeSet) 

또한, 코멘트에서 언급 한 바와 같이 :

scroll.setFont(new Font(fontNameSet, fontSizeSet, Font.PLAIN)); 

가되어야한다

texty.setFont(new Font(fontNameSet, Font.PLAIN, fontSizeSet)); 
+1

잘 잡으세요! @ThunderCrust : 또한 deriveFont()를 고려하십시오. [here] (http://stackoverflow.com/a/14599176/230513). – trashgod