2012-06-30 2 views
1

JEditorPane (창)에서 선택한 텍스트의 글꼴 크기를 설정하는 방법 JComboBox 사용?JEditorPane에서 선택된 텍스트의 글꼴 크기 설정 JComboBox 사용

toolbar.add(new StyledEditorKit.FontSizeAction("12", 12)); 

그러나 당신은 단순히 버튼의 수백 어차피 :

이전에 내가 사용.

+0

음 ... JComboBox에서 int를 가져 오는 방법을 알아 냈습니다 ... INT를 사용하여 선택한 텍스트의 글꼴을 설정하는 방법은 무엇입니까? – Primm

+0

Fount a good demo : http://stackoverflow.com/questions/939109/increasing-the-font-size-of-a-jtextpane-that-displays-html-text – Primm

답변

3

나는이 작업을 수행 할 수있는 표준 방법을 모르지만, 실험에,이 작품 :

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.Action; 
import javax.swing.JComboBox; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.DefaultStyledDocument; 
import javax.swing.text.Document; 
import javax.swing.text.StyledDocument; 
import javax.swing.text.StyledEditorKit; 

public class EditorPaneFun extends JPanel { 
    private static final Integer[] ITEMS = { 9, 10, 11, 12, 14, 16, 18, 20, 24, 
     32 }; 
    private JEditorPane editorPane = new JEditorPane(); 
    private JComboBox<Integer> fontBox = new JComboBox<Integer>(ITEMS); 
    private StyledDocument doc = new DefaultStyledDocument(); 
    private StyledEditorKit styledEditorKit = new StyledEditorKit(); 

    public EditorPaneFun() { 
     editorPane.setDocument(doc); 
     editorPane.setEditorKit(styledEditorKit); 
     JScrollPane scrollpane = new JScrollPane(editorPane); 
     scrollpane.setPreferredSize(new Dimension(500, 400)); 
     JPanel comboPanel = new JPanel(); 
     comboPanel.add(fontBox); 

     setLayout(new BorderLayout()); 
     add(scrollpane, BorderLayout.CENTER); 
     add(comboPanel, BorderLayout.SOUTH); 

     Document doc = editorPane.getDocument(); 
     for (int i = 0; i < 20; i++) { 
     int offset = doc.getLength(); 
     String str = "This is line number: " + i + "\n"; 
     try { 
      doc.insertString(offset, str, null); 
     } catch (BadLocationException e) { 
      e.printStackTrace(); 
     } 
     } 

     fontBox.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      int size = (Integer) fontBox.getSelectedItem(); 
      Action fontAction = new StyledEditorKit.FontSizeAction(String 
        .valueOf(size), size); 
      fontAction.actionPerformed(e); 
     } 
     }); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("EditorPaneFun"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new EditorPaneFun()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

참고이 문서는 DefaultStyledDocument이며,이 단지 작품을 경우의 EditorKit가 된 StyledEditorKit 인 경우.

+0

아니요, 좋지 않습니다. 'setCharacterAttributes (...)'...를 사용하는 것이 더 좋다. –

+0

이 문제는 선택한 텍스트를 가져올 수 없으므로 글꼴 크기가 변경되지 않는다는 것입니다. – Primm

+1

대답을 찾으셨습니까? http://stackoverflow.com/questions/939109/increasing-the-font -size-of-a-jtextpane-that-displays-html-text – Primm

관련 문제