Console.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package ui.view;
  2. import java.awt.Color;
  3. import javax.swing.JScrollPane;
  4. import java.awt.BorderLayout;
  5. import javax.swing.JTextPane;
  6. import javax.swing.text.BadLocationException;
  7. import javax.swing.text.Style;
  8. import javax.swing.text.StyleConstants;
  9. import javax.swing.text.StyledDocument;
  10. import javax.swing.JPanel;
  11. public class Console extends JScrollPane {
  12. /**
  13. *
  14. */
  15. private static final long serialVersionUID = 1L;
  16. private final JTextPane consoleText = new JTextPane();
  17. private final JPanel panel = new JPanel();
  18. private Style style;
  19. private StyledDocument doc;
  20. public Console() {
  21. super();
  22. this.setBackground(Color.WHITE);
  23. consoleText.setForeground(Color.BLACK);
  24. consoleText.setEditable(false);
  25. doc = consoleText.getStyledDocument();
  26. style = consoleText.addStyle("I'm a Style", null);
  27. this.setViewportView(panel);
  28. panel.setLayout(new BorderLayout(0, 0));
  29. panel.setBackground(Color.WHITE);
  30. panel.add(consoleText);
  31. setViewportView(panel);
  32. }
  33. public void addText(String t, Color c, int p) {
  34. StyleConstants.setForeground(style, c);
  35. StyleConstants.setFontSize(style, p);
  36. try {
  37. if (consoleText.getText().length() != 0) {
  38. doc.insertString(doc.getLength(), "\n", style);
  39. }
  40. doc.insertString(doc.getLength(), t, style);
  41. } catch (BadLocationException e) {
  42. }
  43. }
  44. }