2017-10-26 2 views
-1

Java eclipse를 사용하여 양식을 만들고 싶습니다.JLabel 및 JTextField가 스윙 양식에 포함되지 않습니다.

class gestiontache extends JFrame{ 


JFrame f; 
JPanel p1, p2, p3; 
JTabbedPane tp; 

JLabel l1, l2, l3,l4,l5; 
JComboBox tf3categor; 
JComboBox tf4Affiliation; 
JComboBox tf5montant; 

JTextField tf1, tf2; 
JScrollPane sp1; 
JButton savebtn, resetbtn, editbtn; 
private static String FILE = "c:/temp/DocumentPdf.pdf"; 
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, 
     Font.BOLD); 
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, 
     Font.NORMAL, BaseColor.RED); 
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, 
     Font.BOLD); 
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, 
     Font.BOLD); 
gestiontache() { 
    f = new JFrame("Form"); 
    GridLayout lay1= new GridLayout(12, 2); 
    GridLayout lay2= new GridLayout(5, 2); 
    p1 = new JPanel(lay1); 
    p2 = new JPanel(lay2); 
    lay1.setHgap(5); //Cinq pixels d'espace entre les colonnes (H comme Horizontal) 
    lay1.setVgap(5); //Cinq pixels d'espace entre les lignes (V comme Vertical) 
    lay2.setHgap(5); 
    lay2.setVgap(5); 
    tp = new JTabbedPane(); 
    l1 = new JLabel("Nom"); 
    l2 = new JLabel("Prénom"); 
    l3 = new JLabel("Catégorie"); 
    l4 = new JLabel("Affiliation"); 
    l5 = new JLabel("Montant à payer"); 
    tf1 = new JTextField(12); 
    tf2 = new JTextField(12); 
    tf3categor = new JComboBox(new String[] { "Medecin", "Technicien", "Pharmacien","Autre" }); 
    tf4Affiliation =new JComboBox(new String[] { "K", "T", "Sf","Gab","Toze","Med","Tat","Na","B","G","Si","Ga","Ke","Kr" }); 
    tf5montant = new JComboBox(new String[] { "15 Dinars", "30 Dinars"}); 
    savebtn = new JButton(" Ajouter "); 
    resetbtn = new JButton(" Annuler"); 
    editbtn = new JButton(" Imprimer"); 

    p1.add(l1); 
    p1.add(tf1); 
    p1.add(l2); 
    p1.add(tf2); 
    p1.add(l3); 
    p1.add(tf3categor); 
    p1.add(l4); 
    p1.add(tf4Affiliation); 
    p1.add(l5); 
    p1.add(tf5montant); 
    p1.add(savebtn); 
    p1.add(resetbtn); 
    p2.add(l1); 
    p2.add(tf1); 
    p2.add(l2); 
    p2.add(tf2); 
    p2.add(editbtn); 
    resetbtn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent ae) { 
      clear(); 
     } 
    }); 
    savebtn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent ae) { 
      String nom, prenom,categorie, affiliation, montant; 
      nom = tf1.getText(); 
      prenom = tf2.getText(); 
      categorie=(String) tf3categor.getSelectedItem(); 
      affiliation=(String) tf4Affiliation.getSelectedItem(); 
      montant=(String) tf5montant.getSelectedItem(); 
      String url = "jdbc:mysql://localhost:3306/seminaire"; 
      String userid = "root"; 
      String password = ""; 
      try { 
      Connection connection = DriverManager.getConnection(url, 
         userid, password); 
      Statement st = connection.createStatement(); 

       if (nom != "" && prenom != ""&& categorie!= ""&& affiliation!= ""&& montant!= "") { 
    st.executeUpdate("insert into participant values('" + nom 
          + "','" + prenom + "','" + categorie + "','"+affiliation+"','"+montant+"')"); 
JOptionPane.showMessageDialog(null,"Données insérées avec succès"); 
        clear(); 
       } else { 
    JOptionPane.showMessageDialog(null, "merci de saisir vos données"); 
       } 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
     } 
    }); 
      editbtn.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
      String nom, prenom,categorie, affiliation, montant; 
      nom = tf1.getText(); 
      prenom = tf2.getText(); 
      String url = "jdbc:mysql://localhost:3306/seminaire"; 
      String userid = "root"; 
      String password = ""; 
      try { 
       Connection connection = DriverManager.getConnection(url, 
         userid, password); 
       Statement st = connection.createStatement(); 

       if (nom != "" && prenom != "") { 
       ResultSet rs= st.executeQuery("SELECT * FROM participant 
       WHERE nom=nom && prenom=prenom"); 
        while (rs.next()) 
    { 
    String nm = rs.getString("nom"); 
    String prnm = rs.getString("prenom"); 
    String cat = rs.getString("categorie"); 
    String afl=rs.getString("affiliation"); 
    String mnt=rs.getString("montant"); 
    // print the results 
    Document document = new Document(); 
    try { 
     PdfWriter.getInstance(document, new FileOutputStream(FILE)); 
     //open 
     document.open(); 
     Paragraph p = new Paragraph(); 
     p.add("Reçu"); 
     p.setAlignment(Element.ALIGN_CENTER); 
     document.add(p); 
     Paragraph p2 = new Paragraph(); 
     p2.add(nm); //no alignment 
     document.add(p2); 
     Font f = new Font(); 
     f.setStyle(Font.BOLD); 
     f.setSize(8); 
     document.add(new Paragraph("This is my paragraph 3", f)); 
     //close 
     document.close(); 
     System.out.println("Done"); 
     } catch (FileNotFoundException | DocumentException e) { 
     e.printStackTrace(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
     } 
     }  
     else { 
     JOptionPane.showMessageDialog(null, "merci de saisir vos données"); 

       } 

     } 
      catch (Exception e) 
      { 
       System.err.println(e.getMessage()); 
      } 
      }}); 
     } 
      void dis() { 
     f.getContentPane().add(tp); 
     tp.addTab("Ajouter participant", p1); 
     tp.addTab("Imprimer attestation", p2); 

     f.setSize(500, 400); 
    f.setVisible(true); 
    f.setResizable(true); 
     } 

    void clear() 
    { 
     tf1.setText(""); 
     tf2.setText(""); 
     tf3categor.setSelectedItem(""); 
     tf4Affiliation.setSelectedItem(""); 
     tf5montant.setSelectedItem(""); 
    } 

    public static void main(String z[]) { 
    gestiontache data = new gestiontache(); 
    data.dis(); 
    } 
     }   

` 여기서 문제는 JLabel의 및 JTextField를 (NOM은, prenom)를하기 위해 형태로 표시되지 않는 경우 : 문제는 내가 총이 JLabel의와 JTextField.this 내 코드 추가 얻을 수있다 삽입하거나 데이터베이스에서 선택하십시오. 어떻게 내가 그것을 바로 잡을 수 있는지 생각 해봐. 당신에게

+0

'경우 (! NOM = ""! && prenom = "")'작동하지 않습니다. https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java를 참조하십시오. 또한 코드를 올바르게 포맷하면 더 많은 도움을 얻을 수 있습니다. 현재와 ​​같이 코드는 이해하기가 어렵습니다. – VGR

답변

0
p2.add(l1); 

p2.add(tf1); 

p2.add(l2); 

p2.add(tf2); 

이 위의 필드는 모두 p1(Tab1)p2(Tab2) 패널에 추가됩니다 감사합니다.

왜 표시되지 않는가?

p1p2 패널에 대해 별도의 컨트롤을 만들어야합니다. 두 패널에서 동일한 컨트롤을 재사용하지 마십시오. 예를 들어

:

l7 = new JLabel("Normal"); 

p2.add(l7); 
+0

괜찮습니다. 고맙습니다. –

관련 문제