Java 使用或覆盖已弃用的 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22115318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:41:27  来源:igfitidea点击:

Java uses or overrides a deprecated API

javadeprecated

提问by user3368306

I have written a code for my project and I am getting a warning message when I compile. I could not run it. Having tried many possible ways like ignoring warning and etc, I am still unable to run it. Below you will find the code.

我已经为我的项目编写了代码,并且在编译时收到一条警告消息。我无法运行它。尝试了许多可能的方法,例如忽略警告等,我仍然无法运行它。您将在下面找到代码。

import javax.swing.*;
 import java.awt.*;
    import java.awt.event.*;
 import java.io.*;
 import java.net.*;
 import java.util.*;
 import javax.swing.event.*;
 import java.sql.*;

  class NymbleServer extends JFrame 
 {
 JLabel jl1;
 JLabel jl2;


 JButton jb1;
 JButton jb2;

 JTextField jt1;
 JPasswordField jt2;
 String msg="";


 Container c;
 ImageIcon ii;
 ImageIcon i2;
 JLabel jl4;
 JLabel jl6;


 String str="";
 String pass="";
 String path="";
 String coreaddr="";
 String core="";
 String nextcore="";
 String dest="";
 String dest1="";
 File f;
 File fgs;
 Vector v = new Vector();

 NymbleServer()

 {

     jl1=new JLabel("Nymble Server Name ");
     jl2=new JLabel("Server Key ");


     jb1=new JButton("Send");
     jb2=new JButton("Reset");

     jt1=new JTextField(10);
     jt2=new JPasswordField(10);
     ii=new ImageIcon("nymbleserver.png");
     i2=new ImageIcon("nymbleserver1.PNG");
     jl4=new JLabel(ii);
     jl6=new JLabel(i2);

     c = getContentPane();
     c.setLayout(null);
     c.setBackground(new Color(0,0,120));
     c.show();
     c.add(jl1);
     c.add(jt1);
     c.add(jl2);
     c.add(jb1);
     c.add(jb2);
     c.add(jl4);
     c.add(jl6);
     c.add(jt2);

        addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm(evt);
        }
    });





     jl4.setBounds(30,20,728,68);
     jl1.setBounds(20,110,200,25);
     jt1.setBounds(150,110,100,25);
     jl2.setBounds(50,145,200,25);
     jt2.setBounds(150,145,100,25);
     jb1.setBounds(70,235,100,25);
     jb2.setBounds(180,235,100,25);
     jl6.setBounds(300,105,485,309);




     setSize(800,500);
     setVisible(true);
     setTitle("Server");





    jb1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae)
        {


            try {   

  if(!jt1.getText().equals("") && !jt2.getText().equals("")){

      JOptionPane.showMessageDialog (null, "Authorised User", "Login 

Seccess", JOptionPane.INFORMATION_MESSAGE);

         VerifAdminLogin();

         }
         else
         JOptionPane.showMessageDialog((Component) null, 

       "Invalid password. Please try again. ", "Login Error", 

       JOptionPane.INFORMATION_MESSAGE);

        }
        catch(Exception e) { System.out.println(e);
        }

        }
    });



    jb2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae)
        {
        jt1.setText("");
        jt2.setText("");
        } 
        });



     }

     private void exitForm(java.awt.event.WindowEvent evt) 
    {
    dispose();
     }  

 public static void main(String a[])

 {
     new FileOpen();
 }

 void VerifAdminLogin() 
    {
            Connection con=null;
            String url="jdbc:odbc:nymble";
            Statement st=null;

          try
          {

                     String val1=jt1.getText();
                     val1 = val1.trim();
                     String val2 =  

             (String)jt2.getText();
                     val2 = val2.trim();            



                Class.forName

              ("sun.jdbc.odbc.JdbcOdbcDriver");

                con=DriverManager.getConnection(url);

                st = con.createStatement();


            ResultSet rs=st.executeQuery("Select Key from 

               Server where SerName='"+val1+"'");

            while(rs.next()){
                String user = rs.getString(1);

                boolean b=user.equals(val2);            



                if(b)
                {

                  new Server().setVisible(true);
            }
                 else
                {
                    JOptionPane.showMessageDialog

          ((Component) null, "Invalid password. Please try again. ", "Login Error", 

           JOptionPane.INFORMATION_MESSAGE);
                    jt1.setText("");
                    jt2.requestFocus();
                }
                }
          }
          catch(SQLException ex)
           {
            System.out.println("Unable to access the database");
           }
          catch(ClassNotFoundException ex)
           {
            System.out.println("Class not found");
           }
          catch(Exception ex)
          {
           System.out.println("Exception raised is:"+ex);
          }
          finally {
          con=null;
          }
    }

      }

回答by Bitman

Just replace

只需更换

new PseudonymManager().show(); 

with

new PseudonymManager().setVisible(true);

Method show() was deprecated since java 1.5 so just read java docs next time;)

方法 show() 从 java 1.5 开始被弃用,所以下次只需阅读 java 文档;)