Advertisement
Guest User

Untitled

a guest
May 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class TextWriter extends JPanel{
  5. public int width;
  6. public int height;
  7. public String r1 = "";
  8. public String r2 = "";
  9. public String r3 = "";
  10.  
  11. public TextWriter(int w,int h){
  12. width = w;
  13. height = h;
  14. JFrame myframe= new JFrame();
  15. myframe.getContentPane().add(this);
  16. myframe.setTitle("TextWriter");
  17. myframe.setSize(width,height);
  18. myframe.setVisible(true);
  19. myframe.setBackground(Color.WHITE);
  20. }
  21.  
  22. public void paintComponent(Graphics g){
  23. g.drawString(r1,20,30);
  24. g.drawString(r2,20,80);
  25. g.drawString(r3,20,140);
  26.  
  27. }
  28.  
  29.  
  30. public void print1(String s){
  31. r1 += s;
  32. this.repaint();
  33.  
  34. }
  35. public void reset1(String s){
  36. r1 = s;
  37. this.repaint();
  38. }
  39. public void print2(String s){
  40. r2 += s;
  41. this.repaint();
  42. }
  43. public void reset2(String s){
  44. r2 = s;
  45. this.repaint();
  46. }
  47. public void print3(String s){
  48. r3 += s;
  49. this.repaint();
  50. }
  51. public void reset3(String s){
  52. r3 = s;
  53. this.repaint();
  54. }
  55.  
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. import javax.swing.*;
  66. public class TestTextWriter
  67. { public static void main(String[] args)
  68. { TextWriter writer = new TextWriter(300, 200);
  69. String s = JOptionPane.showInputDialog("Please type some text:");
  70. writer.print1(s);
  71. s = JOptionPane.showInputDialog("Try it again:");
  72. writer.print1(s);
  73. s = JOptionPane.showInputDialog("Once more:");
  74. writer.print2(s);
  75. s = JOptionPane.showInputDialog("Last time:");
  76. writer.print3(s);
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement