salut à tous ,
je voudrai realiser une application qui permet de rechercher une sous chaine (ds un jtextfield) dans une chaine (ds un jtextarea) et chaque fois qu'il la trouve il la selectionne
le code source :
la 2éme fenetre
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 package rechercher_remplacer; import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Cadre1 extends JFrame { JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); JToolBar jToolBar1 = new JToolBar(); GridLayout gridLayout1 = new GridLayout(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JTextField jTextField1 = new JTextField(18); JScrollPane jScrollPane1 = new JScrollPane(); GridLayout gridLayout2 = new GridLayout(); static JTextArea jTextArea1 = new JTextArea(); public Cadre1() { try { setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } /** * Initialisation du composant. * * @throws java.lang.Exception */ private void jbInit() throws Exception { contentPane = (JPanel) getContentPane(); contentPane.setLayout(borderLayout1); setSize(new Dimension(400, 300)); setTitle("Rechercher/Remplacer"); jPanel1.setLayout(gridLayout1); jButton1.setText("Rechercher"); jButton1.addActionListener(new Cadre1_jButton1_actionAdapter(this)); jButton2.setText("Remplacer"); jButton2.addActionListener(new Cadre1_jButton2_actionAdapter(this)); jPanel2.setLayout(gridLayout2); contentPane.add(jPanel1, java.awt.BorderLayout.NORTH); jPanel1.add(jToolBar1); jToolBar1.add(jButton1); jToolBar1.add(jTextField1); jToolBar1.add(jButton2); contentPane.add(jPanel2, java.awt.BorderLayout.CENTER); jPanel2.add(jScrollPane1); jScrollPane1.getViewport().add(jTextArea1); } public void jButton2_actionPerformed(ActionEvent e) { //methode de remplaçer } public void jButton1_actionPerformed(ActionEvent e) { Cadre2 c =new Cadre2(); c.setSize(300,150); c.setLocation(300,150); c.setVisible(true); } } class Cadre1_jButton2_actionAdapter implements ActionListener { private Cadre1 adaptee; Cadre1_jButton2_actionAdapter(Cadre1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); } } class Cadre1_jButton1_actionAdapter implements ActionListener { private Cadre1 adaptee; Cadre1_jButton1_actionAdapter(Cadre1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); } }
les messages d'erreur qui s'affiche :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 package rechercher_remplacer; import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Cadre2 extends JFrame { GridLayout gridLayout1 = new GridLayout(); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel3 = new JPanel(); JButton jButton1 = new JButton(); JPanel jPanel4 = new JPanel(); FlowLayout flowLayout1 = new FlowLayout(); JLabel jLabel1 = new JLabel(); static JTextField jTextField1 = new JTextField(17); GridLayout gridLayout2 = new GridLayout(); JButton jButton2 = new JButton(); public Cadre2() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { getContentPane().setLayout(gridLayout1); gridLayout1.setColumns(1); gridLayout1.setRows(2); jPanel1.setLayout(borderLayout1); jPanel1.setMinimumSize(new Dimension(0, 0)); jButton1.setText("Rechercher"); jButton1.addActionListener(new Cadre2_jButton1_actionAdapter(this)); jPanel4.setLayout(flowLayout1); flowLayout1.setAlignment(FlowLayout.LEFT); jLabel1.setText("Texte à rechercher"); jPanel3.setLayout(gridLayout2); gridLayout2.setColumns(1); gridLayout2.setRows(2); jButton2.setText("Remplacer"); this.getContentPane().add(jPanel1); jPanel1.add(jPanel3, java.awt.BorderLayout.EAST); jPanel3.add(jButton1); jPanel3.add(jButton2); jPanel1.add(jPanel4, java.awt.BorderLayout.CENTER); jPanel4.add(jLabel1); jPanel4.add(jTextField1); this.getContentPane().add(jPanel2); } String textarea = Cadre1.jTextArea1.getText(); int longue = textarea.length(); String textfield = jTextField1.getText(); int longtf = textarea.length();int l=0;boolean trouver=false; public void jButton1_actionPerformed(ActionEvent e) { while(l<longue) {if(textarea.charAt(l)==textfield.charAt(0)) {if((textarea.substring(l,l+longtf)).equals(textfield)) { Cadre1.jTextArea1.select(l,l+longtf); trouver=true; l=longtf;break; } } l++; } if (trouver=false){System.out.println("Non trouvé"); } } } class Cadre2_jButton1_actionAdapter implements ActionListener { private Cadre2 adaptee; Cadre2_jButton1_actionAdapter(Cadre2 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); } }
at java.lang.String.charAt(String.java:444)
at rechercher_remplacer.Cadre2.jButton1_actionPerformed(Cadre2.java:73)
at rechercher_remplacer.Cadre2_jButton1_actionAdapter.actionPerformed(Cadre2.java:96)
SVP j'ai besoin de votre aide
Partager