اذهب إلى المحتوى
  • 0

طريقة الربط ما بين الويندوز في جافا

RAA

السؤال

public class frame extends javax.swing.JFrame {
      private test ts;
    /**
     * Creates new form frame
     */
    public frame() {
        initComponents();
    }
    public boolean checkInput(String inputUser, String inputPass) throws FileNotFoundException, IOException{
        FileReader in = new FileReader("Info.txt");
        BufferedReader br = new BufferedReader(in);
        String line;
        while ((line = br.readLine()) != null){
            if (line.contains(inputUser) && line.contains(inputPass)){
                return true;
            }
        }
        in.close();
        return false;
    }

    /**
     * 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();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Users\\MB\\Downloads\\wo.jpg")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 650, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 380, Short.MAX_VALUE)
        );

        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Arial Black", 3, 18)); // NOI18N
        jLabel1.setText("WELCOME");
        jPanel3.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(175, 45, -1, -1));

        jLabel4.setFont(new java.awt.Font("Arial Black", 3, 18)); // NOI18N
        jLabel4.setText("User Name:");
        jPanel3.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(46, 121, 138, -1));

        jLabel3.setFont(new java.awt.Font("Arial Black", 3, 18)); // NOI18N
        jLabel3.setText("Password:");
        jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(46, 185, 138, -1));
        jPanel3.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(202, 125, 142, -1));
        jPanel3.add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(202, 189, 142, -1));

        jButton1.setBackground(new java.awt.Color(255, 255, 255));
        jButton1.setFont(new java.awt.Font("Arial Black", 3, 14)); // NOI18N
        jButton1.setText("Sign in");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(158, 273, -1, -1));

        jButton2.setBackground(new java.awt.Color(255, 255, 255));
        jButton2.setFont(new java.awt.Font("Arial Black", 3, 14)); // NOI18N
        jButton2.setText("Sign up");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(269, 273, -1, -1));

        jLabel5.setIcon(new javax.swing.ImageIcon("C:\\Users\\MB\\Downloads\\owo.jpg")); // NOI18N
        jPanel3.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 380));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 468, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(651, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(37, 37, 37)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 375, Short.MAX_VALUE))
        );

        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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String userName = jTextField1.getText();
        String passWord = jTextField2.getText();
          try {
              if(checkInput(userName, passWord)){
                  ts = new test();
              } } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Error");
          }
        dispose();
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        frame2 f2 = new frame2();
        f2.show();
        
        dispose();
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new frame().setVisible(true);
            }
        });
    }
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        frame f1 = new frame();
        f1.show();

        dispose();
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        /* String name = jTextField1.getText();
        String mobileNo = jTextField2.getText();
        String email = jTextField3.getText();
        String age = jTextField4.getText(); */
        try {
            String name = jTextField1.getText();
            String mobileNo = jTextField2.getText();
            String email = jTextField3.getText();
            String age = jTextField4.getText();
            FileWriter Writer = new FileWriter("Info.txt", true);
            Writer.write("" + name + " " + mobileNo + " " + email + " " + age + " ");
            Writer.write(System.getProperty("line.separator"));
            Writer.close();
            JOptionPane.showMessageDialog(null, "Success");
            setVisible(false);
            new frame2().setVisible(true);
            ts = new test();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error");
        }

    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frame2().setVisible(true);
            }
        });
    }

احتاج مساعدة بدي اربط ما بين 2 ويندوز انو اذا اليوزر ادخل بيانتو في الويندوز تبعت المعلومات بينتقل الى ويندوز اللوق ان وفي حالة ما ادخل بيانات وضغط على البوتون تبع الحفظ يخرجلو مسج انو ايرور ويجب عليه ادخال معلوماتو لينتقل الى ويندوز اللوق ان كيف ممكن تتعمل ؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

يبدو أنك تريد إنشاء نموذج تسجيل دخول يسمح للمستخدمين بإدخال اسم المستخدم وكلمة المرور الخاصة بهم ، ثم عرض معلوماتهم في نافذة السجل إذا تم تسجيل الدخول بنجاح. إذا فشل تسجيل الدخول، يجب أن يتلقى المستخدم رسالة خطأ ويطلب منه إدخال معلوماته مرة أخرى. بالإضافة إلى ذلك ، هناك زر "حفظ" يسمح للمستخدمين بحفظ بياناتهم في ملف.

تتمثل إحدى طرق تنفيذ ذلك في استخدام كائنين مختلفين من JFrame ، أحدهما لنموذج تسجيل الدخول والآخر لنافذة السجل. عندما يُدخل المستخدم معلوماته وينقر على زر "تسجيل الدخول" ، يمكنك التحقق مما إذا كان الإدخال صحيحًا عن طريق استدعاء طريقة checkInput (). إذا كان الإدخال صحيحًا ، يمكنك إنشاء مثيل لنافذة السجل JFrame وتمرير البيانات الضرورية كمعلمات إلى مُنشئها. إذا كان الإدخال غير صحيح ، يمكنك إظهار رسالة خطأ وتطلب من المستخدم إدخال معلوماته مرة أخرى.

في المثال هذا ، ينشيء كلاس LoginForm JFrame حقلين نصيين لاسم المستخدم وكلمة المرور وزر "تسجيل الدخول" وزر "إلغاء" (الذي لم يتم تنفيذه هنا). عندما ينقر المستخدم على زر "تسجيل الدخول" ، يتم استدعاء طريقة checkInput () للتحقق من صحة اسم المستخدم وكلمة المرور. إذا كانت صحيحة ، يتم إنشاء مثيل لكلاس LogWindow ، ويمرر اسم المستخدم كمعامل إلى المُنشئ الخاص به. إذا كانت غير صحيحة ، تظهر رسالة خطأ

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class LoginForm extends JFrame implements ActionListener {

    private JTextField usernameField;
    private JPasswordField passwordField;

    public LoginForm() {
        super("Login Form");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel usernameLabel = new JLabel("Username:");
        JLabel passwordLabel = new JLabel("Password:");

        usernameField = new JTextField(20);
        passwordField = new JPasswordField(20);

        JButton signInButton = new JButton("Sign in");
        signInButton.addActionListener(this);

        JPanel mainPanel = new JPanel(new GridLayout(3, 2));
        mainPanel.add(usernameLabel);
        mainPanel.add(usernameField);
        mainPanel.add(passwordLabel);
        mainPanel.add(passwordField);
        mainPanel.add(new JLabel(""));
        mainPanel.add(signInButton);

        getContentPane().add(mainPanel);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("Sign in")) {
            if (checkInput()) {
                LogWindow logWindow = new LogWindow(usernameField.getText());
                setVisible(false);
            } else {
                JOptionPane.showMessageDialog(this, "Incorrect username or password. Please try again.");
                usernameField.setText("");
                passwordField.setText("");
            }
        }
    }

    private boolean checkInput() {
        // Here you can implement the logic to check if the username and password are correct.
        // For example, you can compare them with a database or a file.
        // For simplicity, let's assume that the correct username is "user" and the correct password is "password".
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());
        return username.equals("user") && password.equals("password");
    }

    public static void main(String[] args) {
        new LoginForm();
    }
}

class LogWindow extends JFrame implements ActionListener {

    private JTextArea logTextArea;
    private JButton saveButton;

    public LogWindow(String username) {
        super("Log Window for " + username);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        logTextArea = new JTextArea(20, 40);
        logTextArea.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(logTextArea);

        saveButton = new JButton("Save");
        saveButton.addActionListener(this);

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(scrollPane, BorderLayout.CENTER);
        mainPanel.add(saveButton, BorderLayout.SOUTH);

        getContentPane().add(mainPanel);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("Save")) {
            JFileChooser fileChooser = new JFileChooser();
            int result = fileChooser.showSaveDialog(this);
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                try {
                    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                    writer.write(logTextArea.getText());
                    writer.close();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(this, "Error writing to file: " + ex.getMessage());
                }
            }
        }
    }
}

 

تم التعديل في بواسطة Mustafa Suleiman
رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...