2016-12-03 135 views
0
//RESEARCH jframe code 




/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package InterfaceDico; 

import GestionDictionnaire.GestionDico; 
import javax.swing.JOptionPane; 

/** 
* 
* @author Franck 
*/ 
public class RechercherMot extends javax.swing.JFrame { 

    /** 
    * Creates new form RechercherMot 
    */ 
    public RechercherMot() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jPanel1 = new javax.swing.JPanel(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     valider = new javax.swing.JButton(); 
     entrer_mot = new javax.swing.JTextField(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     entrer_definition = new javax.swing.JTextArea(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

     jLabel1.setText("              RECHERCHER MOT "); 

     jLabel2.setText("MOT A RECHERCHER :"); 

     valider.setText("RECHERCHER"); 
     valider.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       validerActionPerformed(evt); 
      } 
     }); 

     entrer_mot.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       entrer_motActionPerformed(evt); 
      } 
     }); 

     entrer_definition.setColumns(20); 
     entrer_definition.setRows(5); 
     jScrollPane1.setViewportView(entrer_definition); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jScrollPane1) 
        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
         .addGap(0, 0, Short.MAX_VALUE) 
         .addComponent(valider)) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
         .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE) 
         .addGap(18, 18, 18) 
         .addComponent(entrer_mot, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap()) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(entrer_mot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE) 
       .addGap(18, 18, 18) 
       .addComponent(valider) 
       .addContainerGap()) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void validerActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     String mot= entrer_mot.getText(); 
     entrer_mot.setText(""); 
     String definition = entrer_definition.getText(); 
     entrer_definition.setText(""); 


      if(mot.equalsIgnoreCase("")){ 
       JOptionPane.showMessageDialog(null, "données incorrectes", "Erreur", JOptionPane.ERROR_MESSAGE); 
      } 
     else{ 
       GestionDico dico =new GestionDico(); 
       dico.RechercherMot(mot); 

在這裏,當我做enter_definition=setText(dico.RechercherMot(mot))出現了錯誤。想顯示在jtextarea的搜索結果

+1

發生了什麼錯誤?發佈異常堆棧跟蹤或錯誤詳細信息。 – Blip

+0

錯誤:無法將void轉換爲String;如果我想打印我的功能的結果,我該怎麼辦? –

+0

在jtextarea中打印 –

回答

0

方法RechercherMot(String mot)的返回類型是無效的。這意味着它不會返回任何東西。現在您已使用enter_definition.setText(dico.RechercherMot(mot))其中enter_definitionJTextArea的一個實例,功能setText需要String作爲參數。但你的dico.RechercherMot(mot)返回無效或沒有。所以你得到編譯錯誤void不能轉換爲String;

現在,爲了去除錯誤,你必須的RechercherMot(String mot)返回類型更改爲String和方法,使用return語句返回一個有效String

這裏我明白你所說的應該返回你正在執行的查詢的文本格式化結果集。所以我的建議是,你可以聲明String變量說result和你創建了準備好的語句之前初始化爲空字符串:

String result = ""; 

然後在循環中while(resultat.next())更換

System.out.println("mot = " +resultat.getString("mot")+" "+"definition = "+resultat.getString("definition")+" "); 

隨着

result += "mot = " +resultat.getString("mot")+" "+"definition = "+resultat.getString("definition")+" \n"; 

最後在方法的最後一行,在catch塊之後插入:

return result; 

當然從void改變方法的返回類型以String

+0

thankssssssssssssssssssss –