JOptionPane:parentComponent没有有效的父级

时间:2017-08-06 13:17:56

标签: java database eclipse swing wampserver

我正在使用Java eclipse作为我的数据库,但最终我遇到这样的错误:JOptionPane: parentComponent does not have a valid parent我不知道问题是什么,请有人解释,因为我只是第二年的IT学生而且我想要了解有关Java的更多信息我想完成我的目标。

import java.awt.EventQueue;
import java.lang.*;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;

import javax.swing.*;

public class Login {

  Connection connection = null;

  private JFrame frame;
  private JTextField username;
  private JPasswordField password;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          Login window = new Login();
          window.frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */

  public Login() {
    initialize();
    connection = Database.dbconector();

  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    username = new JTextField();
    username.setBounds(127, 55, 200, 50);
    frame.getContentPane().add(username);
    username.setColumns(10);

    password = new JPasswordField();
    password.setBounds(127, 125, 200, 50);
    frame.getContentPane().add(password);

    JButton btnLogin = new JButton("Log in");
    btnLogin.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {


        try {
          String query = "select * from bsit-db where Username=? and Password=?";
          PreparedStatement pst = connection.prepareStatement(query);
          pst.setString(1, username.getText());
          pst.setString(2, password.getText());


          ResultSet rs = pst.executeQuery();
          int count = 0;
          while (rs.next()) {
            count = count + 1;
          }
          if (count == 1) {
            JOptionPane.showInternalInputDialog(null, "Successfully Login to your account!");



          } else {
            JOptionPane.showInternalInputDialog(null, "Incorrect ID/Password. Please Try Again!");
          }
          rs.close();
          pst.close();

        } catch (Exception e1) {
          JOptionPane.showInternalInputDialog(null, e1);

        } finally {
          try {

          } catch (Exception e1) {
            JOptionPane.showInternalInputDialog(null, e1);
          }
        }


      }
    });
    btnLogin.setBounds(103, 202, 112, 37);
    frame.getContentPane().add(btnLogin);

    JButton btnClear = new JButton("clear");
    btnClear.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {

        username.setText(null);
        password.setText(null);
      }
    });
    btnClear.setBounds(237, 202, 112, 37);
    frame.getContentPane().add(btnClear);

    JLabel lblUsername = new JLabel("username");
    lblUsername.setBounds(66, 55, 200, 50);
    frame.getContentPane().add(lblUsername);

    JLabel lblPassword = new JLabel("password");
    lblPassword.setBounds(66, 125, 200, 50);
    frame.getContentPane().add(lblPassword);
  }
}

这是控制台错误

  

Sun Aug 06 20:54:43 CST 2017警告:建立SSL连接   不建议不使用服务器的身份验证。根据   到MySQL 5.5.45 +,5.6.26 +和5.7.6+要求SSL连接必须   如果显式选项不是,则默认建立       为了符合不使用SSL的现有应用程序,verifyServerCertificate属性设置为'false'。您需要   通过设置useSSL = false显式禁用SSL,或设置useSSL = true   并提供信任库       用于服务器证书验证。       java.lang.RuntimeException:JOptionPane:parentComponent没有有效的父级       在javax.swing.JOptionPane.createInternalFrame(未知来源)       在javax.swing.JOptionPane.showInternalOptionDialog(未知来源)       在javax.swing.JOptionPane.showInternalMessageDialog(未知来源)       在javax.swing.JOptionPane.showInternalMessageDialog(未知来源)       在javax.swing.JOptionPane.showInternalMessageDialog(未知来源)       在Database.dbconector(Database.java:19)       在登录。 < init> (Login.java:44)       登录$ 1.run(Login.java:29)       at java.awt.event.InvocationEvent.dispatch(Unknown Source)       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)       在java.awt.EventQueue.access $ 500(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown   资源)       at java.awt.EventQueue.dispatchEvent(Unknown Source)       at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       在java.awt.EventDispatchThread.run(未知来源)

1 个答案:

答案 0 :(得分:0)

  

showInternalInputDialog用于显示在a中的选项窗格   JDesktopPane。请改为showInputDialog

请参阅此问题:JOptionPane: parentComponent does not have a valid parent