Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Window extends JFrame implements ActionListener {
- private JButton findnext;
- private JButton replace;
- private JButton delete;
- private JButton upper;
- private JTextField from,to;
- private JTextArea textArea;
- final static Color found = Color.PINK;
- final Highlighter hilit;
- final Highlighter.HighlightPainter painter;
- public Window() {
- setTitle("Project 8");
- Toolkit tk = Toolkit.getDefaultToolkit();
- Dimension d = tk.getScreenSize();
- setSize((d.width/4)*3,d.height);
- textArea = new JTextArea ("The apple ate the apple.",8,40);
- textArea.setLineWrap(true);
- Container contentPane = getContentPane();
- addWindowListener(new Close());
- contentPane.add(textArea);
- JPanel panel = new JPanel();
- JButton findnext = new JButton("FindNext");
- panel.add(findnext);
- from = new JTextField(8);
- panel.add(from);
- findnext.addActionListener(this);
- JButton replace = new JButton("Replace");
- panel.add(replace);
- to = new JTextField(8);
- panel.add(to);
- findnext.addActionListener(this);
- JButton delete = new JButton("Delete");
- panel.add(delete);
- findnext.addActionListener(this);
- JButton upper = new JButton("Upper");
- panel.add(upper);
- findnext.addActionListener(this);
- contentPane.add(panel, "South");
- hilit = new DefaultHighlighter();
- painter = new DefaultHighlighter.DefaultHighlightPainter(found);
- textArea.setHighlighter(hilit);
- }
- public void actionPerformed(ActionEvent evt) {
- String f = from.getText();
- String t = to.getText();
- int n = textArea.getText().indexOf(f);
- Object source = evt.getSource();
- if (source == findnext) {
- hilit.removeAllHighlights();
- String text = textArea.getText();
- int index = text.indexOf(f,0);
- if (index>0) {
- try {
- hilit.addHighlight(index, index+f.length(), DefaultHighlighter.DefaultPainter);
- }
- catch (BadLocationException e) {
- ;
- }
- }else if (source == replace) {
- if (n>=0 && f.length() > 0) {
- textArea.replaceRange(to.getText(),n,n+f.length());
- ;
- }else if (source == delete) {
- textArea.setText(" ");
- }else if (source == upper) {
- f.toUpperCase() ;
- }
- }
- }
- }
- }
- private JButton findnext;
- private JButton replace;
- private JButton delete;
- private JButton upper;
- JButton findnext = new JButton("FindNext");
- //...
- JButton replace = new JButton("Replace");
- //...
- JButton delete = new JButton("Delete");
- //...
- JButton upper = new JButton("Upper");
- if (source == findnext) {
- JButton findnext = new JButton("FindNext");
- //
- JButton replace = new JButton("Replace");
- //
- JButton delete = new JButton("Delete");
- //
- JButton upper = new JButton("Upper");
- findnext = new JButton("FindNext");
- //
- replace = new JButton("Replace");
- //
- delete = new JButton("Delete");
- //
- upper = new JButton("Upper");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement