fork download
  1. import java.awt.Color;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Date;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JTextPane;
  12. import javax.swing.text.BadLocationException;
  13. import javax.swing.text.DefaultStyledDocument;
  14. import javax.swing.text.StyleConstants;
  15. import javax.swing.text.StyleContext;
  16. import javax.swing.text.StyledDocument;
  17.  
  18. public class FrameTextArea extends JFrame {
  19.  
  20. private StyleContext context = new StyleContext();
  21. private StyledDocument document = new DefaultStyledDocument(context);
  22. private javax.swing.text.Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
  23. private final JTextPane textArea = new JTextPane(document);
  24. private final JScrollPane TextAreaScroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  25. private JButton jButton = new JButton("Clear");
  26.  
  27. public FrameTextArea() {
  28.  
  29. setSize(400, 200);
  30. setTitle("Text Pane");
  31. StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
  32. StyleConstants.setFontSize(style, 10);
  33. StyleConstants.setSpaceAbove(style, 1);
  34. StyleConstants.setSpaceBelow(style, 1);
  35. setLayout(new FlowLayout());
  36. jButton.addActionListener(new ActionListener() {
  37.  
  38. public void actionPerformed(ActionEvent e) {
  39. try {
  40. document.remove(0, document.getLength());
  41. } catch (BadLocationException ex) {
  42. Logger.getLogger(FrameTextArea.class.getName()).log(Level.SEVERE, null, ex);
  43. }
  44. }
  45. });
  46.  
  47.  
  48. add(jButton);
  49. add(TextAreaScroll);
  50. StyleConstants.setForeground(style, Color.red);
  51. try {
  52. StyleConstants.setForeground(style, Color.red);
  53. document.insertString(document.getLength(), "\n" + new Date() + ": " + "Red Text", style);
  54. StyleConstants.setForeground(style, Color.PINK);
  55. document.insertString(document.getLength(), "\n" + new Date() + ": " + "PINK Text", style);
  56.  
  57. StyleConstants.setForeground(style, Color.black);
  58. document.insertString(document.getLength(), "\n" + "<a href=\"http://w...content-available-to-author-only...s.com\" >Link</a>", style);
  59. } catch (BadLocationException ex) {
  60. Logger.getLogger(FrameTextArea.class.getName()).log(Level.SEVERE, null, ex);
  61. }
  62.  
  63.  
  64. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65. setVisible(true);
  66. }
  67.  
  68. public static void main(String[] args) {
  69. FrameTextArea frameTextArea= new FrameTextArea();
  70. }
  71. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:18: class FrameTextArea is public, should be declared in a file named FrameTextArea.java
public class FrameTextArea extends JFrame {
       ^
1 error
stdout
Standard output is empty