Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. public class Window extends JFrame implements ActionListener {
  2. private JButton findnext;
  3. private JButton replace;
  4. private JButton delete;
  5. private JButton upper;
  6. private JTextField from,to;
  7. private JTextArea textArea;
  8. final static Color found = Color.PINK;
  9. final Highlighter hilit;
  10. final Highlighter.HighlightPainter painter;
  11.  
  12. public Window() {
  13. setTitle("Project 8");
  14. Toolkit tk = Toolkit.getDefaultToolkit();
  15. Dimension d = tk.getScreenSize();
  16. setSize((d.width/4)*3,d.height);
  17. textArea = new JTextArea ("The apple ate the apple.",8,40);
  18. textArea.setLineWrap(true);
  19. Container contentPane = getContentPane();
  20. addWindowListener(new Close());
  21. contentPane.add(textArea);
  22. JPanel panel = new JPanel();
  23. JButton findnext = new JButton("FindNext");
  24. panel.add(findnext);
  25. from = new JTextField(8);
  26. panel.add(from);
  27. findnext.addActionListener(this);
  28. JButton replace = new JButton("Replace");
  29. panel.add(replace);
  30. to = new JTextField(8);
  31. panel.add(to);
  32. findnext.addActionListener(this);
  33. JButton delete = new JButton("Delete");
  34. panel.add(delete);
  35. findnext.addActionListener(this);
  36. JButton upper = new JButton("Upper");
  37. panel.add(upper);
  38. findnext.addActionListener(this);
  39. contentPane.add(panel, "South");
  40. hilit = new DefaultHighlighter();
  41. painter = new DefaultHighlighter.DefaultHighlightPainter(found);
  42. textArea.setHighlighter(hilit);
  43. }
  44.  
  45. public void actionPerformed(ActionEvent evt) {
  46. String f = from.getText();
  47. String t = to.getText();
  48. int n = textArea.getText().indexOf(f);
  49. Object source = evt.getSource();
  50.  
  51. if (source == findnext) {
  52. hilit.removeAllHighlights();
  53. String text = textArea.getText();
  54. int index = text.indexOf(f,0);
  55. if (index>0) {
  56. try {
  57. hilit.addHighlight(index, index+f.length(), DefaultHighlighter.DefaultPainter);
  58. }
  59. catch (BadLocationException e) {
  60. ;
  61. }
  62. }else if (source == replace) {
  63. if (n>=0 && f.length() > 0) {
  64. textArea.replaceRange(to.getText(),n,n+f.length());
  65. ;
  66. }else if (source == delete) {
  67. textArea.setText(" ");
  68. }else if (source == upper) {
  69. f.toUpperCase() ;
  70. }
  71. }
  72. }
  73. }
  74. }
  75.  
  76. private JButton findnext;
  77. private JButton replace;
  78. private JButton delete;
  79. private JButton upper;
  80.  
  81. JButton findnext = new JButton("FindNext");
  82. //...
  83. JButton replace = new JButton("Replace");
  84. //...
  85. JButton delete = new JButton("Delete");
  86. //...
  87. JButton upper = new JButton("Upper");
  88.  
  89. if (source == findnext) {
  90.  
  91. JButton findnext = new JButton("FindNext");
  92. //
  93. JButton replace = new JButton("Replace");
  94. //
  95. JButton delete = new JButton("Delete");
  96. //
  97. JButton upper = new JButton("Upper");
  98.  
  99. findnext = new JButton("FindNext");
  100. //
  101. replace = new JButton("Replace");
  102. //
  103. delete = new JButton("Delete");
  104. //
  105. upper = new JButton("Upper");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement