Unterschiede
Hier werden die Unterschiede zwischen der gewählten und der aktuellen Version gezeigt.
swing:jeditorpane-unvisible-text 2014/06/25 14:47 | swing:jeditorpane-unvisible-text 2020/01/22 20:59 aktuell | ||
---|---|---|---|
Zeile 38: | Zeile 38: | ||
public class UnvisibleText extends JFrame { | public class UnvisibleText extends JFrame { | ||
- | public UnvisibleText() { | + | public UnvisibleText() { |
- | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
- | setSize(400, 300); | + | setSize(400, 300); |
- | setLocationRelativeTo(null); | + | setLocationRelativeTo(null); |
- | init(); | + | init(); |
- | } | + | } |
- | private void init() { | + | private void init() { |
- | Container cp = getContentPane(); | + | Container cp = getContentPane(); |
- | cp.setLayout(new BorderLayout()); | + | cp.setLayout(new BorderLayout()); |
- | JEditorPane editor = new JEditorPane(); | + | JEditorPane editor = new JEditorPane(); |
- | cp.add(new JScrollPane(editor), BorderLayout.CENTER); | + | cp.add(new JScrollPane(editor), BorderLayout.CENTER); |
- | initEditorWithText(editor); | + | initEditorWithText(editor); |
- | } | + | } |
- | private void initEditorWithText(JEditorPane editor) { | + | private void initEditorWithText(JEditorPane editor) { |
- | editor.setContentType("text/rtf"); | + | editor.setContentType("text/rtf"); |
- | StyledDocument doc = (StyledDocument) editor.getDocument(); | + | StyledDocument doc = (StyledDocument) editor.getDocument(); |
- | Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); | + | Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); |
- | Style invisibleStyle = doc.addStyle("invisible", defaultStyle); | + | Style invisibleStyle = doc.addStyle("invisible", defaultStyle); |
- | StyleConstants.setFontSize(invisibleStyle, 0); | + | StyleConstants.setFontSize(invisibleStyle, 0); |
- | StyleConstants.setForeground(invisibleStyle, editor.getBackground()); | + | StyleConstants.setForeground(invisibleStyle, editor.getBackground()); |
- | try { | + | try { |
- | doc.insertString(doc.getLength(), "Hello ", null); | + | doc.insertString(doc.getLength(), "Hello ", null); |
- | doc.insertString(doc.getLength(), "crazy ", doc.getStyle("invisible")); | + | doc.insertString(doc.getLength(), "crazy ", doc.getStyle("invisible")); |
- | doc.insertString(doc.getLength(), "world!", null); | + | doc.insertString(doc.getLength(), "world!", null); |
- | System.out.println("Gesamter Text: " + doc.getText(0, doc.getLength())); | + | System.out.println("Gesamter Text: " + doc.getText(0, doc.getLength())); |
- | } catch (BadLocationException e) { | + | } catch (BadLocationException e) { |
- | e.printStackTrace(); | + | e.printStackTrace(); |
- | } | + | } |
- | } | + | } |
- | public static void main(String[] args) { | + | public static void main(String[] args) { |
- | new UnvisibleText().setVisible(true); | + | new UnvisibleText().setVisible(true); |
- | } | + | } |
} | } | ||
</code> | </code> | ||