2012-08-27 5 views
5

** हल **जावा कांसेप्ट आईडिया

मैं काफी जावा के लिए नया हूँ और अब तक मैं इसे प्यार करता!

तो मैं सिर्फ यह पूछ रहा हूं कि किसी के पास कोई विचार है जो मेरी मदद कर सकता है। तो यहां मैं क्या करना चाहता हूं।

मैं अभी जो काम कर रहा हूं वह एक ऐसा एप्लिकेशन है जो मेरी स्थानीय वेबसाइट (शीर्षक, सामग्री, आदि) के साथ बातचीत कर सकता है। तो मुझे क्या करना है JOptionPane.showConfirmDialog दिखाएं, और उपयोगकर्ता नाम और पासवर्ड दर्ज करें।

तो मूल रूप से करता है, तो उपयोगकर्ता नाम या पासवर्ड गलत है मैं जब वे क्लिक Ok उन्हें पता जानकारी गलत showConfirmDialog गायब हो जाता है है कि वहाँ जाने के लिए एक JOptionPane.showMessageDialog लेकिन प्रदर्शित करना चाहते हैं!

कोई विचार लोग ?! मेरा कोड यहाँ है।

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

public class javaTesting extends JFrame { 
    public JFrame mrFrame; 
    public int enter; 
    public JPanel mrPanel; 

    public javaTesting() throws Exception 
    { 
      Class.forName("com.mysql.jdbc.Driver"); 
      try { 
       Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms","root",""); 
      } catch (SQLException e){ 
       System.out.println(e.getMessage()); 
      } 
      mrFrame = new JFrame(); 
      mrPanel = new JPanel(); 

      mrPanel.setLayout(new GridLayout(4,1)); 

      JLabel user = new JLabel("Username"); 
      mrPanel.add(user); 

      JTextField user_input = new JTextField(30); 
      mrPanel.add(user_input); 

      JLabel pass = new JLabel("Password"); 
      mrPanel.add(pass); 

      JTextField pw_input = new JPasswordField(30); 
      mrPanel.add(pw_input); 

      mrFrame.setSize(700,700); 
      mrFrame.setLocationRelativeTo(null); 
      mrFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      //mrFrame.setVisible(true); 
      mrFrame.setResizable(false); 

      input(); 

      if(enter == JOptionPane.OK_OPTION) 
      { 
       JOptionPane.showMessageDialog(null, "You clicked ok!"); 
       input(); 
      } else { 
       System.exit(1); 
      } 
    } 
    public void input() 
    { 
     enter = (int) JOptionPane.showConfirmDialog(mrFrame,mrPanel,"Login Cridantiels",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE); 
    } 
    public static void main(String agrs[]) throws Exception 
    { 
     new javaTesting(); 
    } 
} 

तो मैंने यही किया और ऐसा लगता है कि यह गलत है या नहीं, यह गलत नहीं है। हालांकि यह काम करता है:]

do{ 
     input(); 

     if(enter == JOptionPane.OK_OPTION) 
     { 
      JOptionPane.showMessageDialog(null, "You clicked ok!"); 
     } else { 
      System.exit(1); 
     } 
    } while(enter != JOptionPane.CANCEL_OPTION); 
+5

है मूल रूप से, आप एक पाश की जरूरत धन्यवाद @shan इसे संभाल करने के लिए ... – shan

+0

, मैं एक करते हैं {} जबकि() पाश किया है, और यह ठीक काम करता है। धन्यवाद! –

+1

एक और संकेत। जावा में सभी कक्षाएं पूंजी पत्र से शुरू होनी चाहिए :) यदि आप नए हैं, तो जानना महत्वपूर्ण है;) –

उत्तर

3

शान के रूप में सुझाव दिया गया है, आप पाश आप साख आप कोड के भाग सभा की जरूरत है। यहाँ एक उदाहरण

JPanel pnlDetails = new JPanel(new GridBagLayout()); 
JTextField userNameField = new JTextField(10); 
JPasswordField passwordField = new JPasswordField(10); 

GridBagConstraints gbc = new GridBagConstraints(); 
gbc.gridx = 0; 
gbc.gridy = 0; 
gbc.anchor = GridBagConstraints.WEST; 
gbc.insets = new Insets(2, 2, 2, 2); 

pnlDetails.add(new JLabel("User name:"), gbc); 
gbc.gridy++; 
pnlDetails.add(new JLabel("Password:"), gbc); 

gbc.gridx = 1; 
gbc.gridy = 0; 
gbc.anchor = GridBagConstraints.EAST; 
pnlDetails.add(userNameField, gbc); 
gbc.gridy++; 
pnlDetails.add(passwordField, gbc); 

// The result from the dialog, will be OK or CANCEL 
int operation; 
// Flag used to determine if the credentials are okay or not 
boolean badCredentials = true; 
do { 

    operation = JOptionPane.showConfirmDialog(null, pnlDetails, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); 

    String userName = userNameField.getText(); 
    char[] password = passwordField.getPassword(); 

    // You would valid you credintals here :P 
    if (userName.equals("super") && new String(password).equals("happy")) { 

     badCredentials = false; 

    } else if (operation != JOptionPane.CANCEL_OPTION) { 

     JOptionPane.showMessageDialog(null, "Invalid Credentials", "Error", JOptionPane.ERROR_MESSAGE); 

    } 

} while (operation != JOptionPane.CANCEL_OPTION && badCredentials); 

if (!badCredentials && operation != JOptionPane.CANCEL_OPTION) { 

    System.out.println("Continue program"); 

} else { 

    System.out.println("Exit program"); 

} 

System.exit(0);