【问题标题】:JLabel and JTextField don't appper in the swing formJLabel 和 JTextField 不适用于摇摆形式
【发布时间】:2018-04-07 19:06:02
【问题描述】:

我想使用 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) 没有出现在表单中以便插入或从数据库中选择。请知道我该如何纠正它。谢谢

【问题讨论】:

  • if (nom != "" && prenom != "") 将不起作用。见stackoverflow.com/questions/513832/…。此外,如果您正确格式化代码,您将获得更多帮助。就目前而言,代码很难理解。

标签: java eclipse forms swing layout


【解决方案1】:
p2.add(l1);

p2.add(tf1);

p2.add(l2);

p2.add(tf2);

p1(Tab1)p2(Tab2) 面板中都添加了上述这些字段。

这就是它不显示的原因。

您必须为p1p2 面板创建单独的控件。不要在两个面板中重复使用相同的控件。

例如:

l7 = new JLabel("Normal");

p2.add(l7);

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多