0

I'm a little new to java, having just started a high-school level class in August. I am still learning the basic information, and have a general understanding of the basics, such as JOptionPane, Scanner, Array and system lines. My question is if there is a way to create a standalone Java program, where you can just double click an icon on the desktop to launch it. If so, is there a way to create a file dump where you can only access it by a password? I already have a basic login program that allows access based on a predetermined user and password. The goal of this program is to create a secure (even though it's basic) file share between people who are meant to have it and keep out those who aren't, that contains specific files (.docx, .jpeg, .pptx, etc.) that are utilized for school work. If so, is there also a way to limit access to these files to being only accessed through this program?

This is the code I have so far:

import javax.swing.JOptionPane.*;
import java.lang.Math.*;
import java.lang.System.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JFrame;

public class UserLog extends JFrame  

{

public static void main(String[]Args) throws InterruptedException 
    {
    boolean isValid=false;
    while(!isValid)
        {
    // Components related to "login" field    
    JLabel label_loginname = new JLabel("Enter your login name:");    
    JTextField loginname = new JTextField(15);    
    // loginname.setText("EnterLoginNameHere"); 
    // Pre-set some text    
    // Components related to "password" field    
    JLabel label_password = new JLabel("Enter your password:");    
    JPasswordField password = new JPasswordField();    
    // password.setEchoChar('@'); 
    // Sets @ as masking character    
    // password.setEchoChar('\000'); 
    // Turns off masking    
    JCheckBox rememberCB = new JCheckBox("Remember me");

    Object[] array = {label_loginname,
    loginname,                       
    label_password,                       
    password,                       
    rememberCB};
    Object[] options = {"Login", "Cancel"};
    int res = JOptionPane.showOptionDialog(null,
                                            array,
                                            "Login",
                                            JOptionPane.YES_NO_OPTION,
                                            JOptionPane.QUESTION_MESSAGE,
                                            null,     //do not use a custom Icon
                                            options,  //the titles of buttons
                                            options[0]); //default button title

    // User hit Login    
    if (res == 0) 
        { 
            System.out.println( "Login" ); 
        }    
    // User hit CANCEL    
    if (res == 1) 
        { 
            System.out.println( "Canceled" ); 
        }    
    // User closed the window without hitting any button    
    if (res == JOptionPane.CLOSED_OPTION) 
        { 
            System.out.println( "CLOSED_OPTION" ); 
        }


    // Output data in "login" field, if any    
    String newloginname = loginname.getText();    
    String newpassword = new String(password.getPassword());    
    if (newloginname.equalsIgnoreCase("Cody_Coulter") && newpassword.equals("cheche1"))
        {
            System.out.println("Login Successful!");
            boolean selectedCB = rememberCB.isSelected();    
            System.out.println( "selectedCB: " + selectedCB );
            Thread.sleep(3000);
            Object[] array1= {"It's about time to choose"};
            Object[] options1= {"Leave", "Keep Going"};
            int res1 = JOptionPane.showOptionDialog(null,
                                            array1,
                                            "There",
                                            JOptionPane.YES_NO_OPTION,
                                            JOptionPane.QUESTION_MESSAGE,
                                            null,     //do not use a custom Icon
                                            options1,  //the titles of buttons
                                            options1[0]); //default button title
            if(res1==1)
                {
                    String name1 = JOptionPane.showInputDialog(null,
                                                        "What is your name?");
                    int length = 0;
                    length = newpassword.length();
                    String Pass = "*";
                    newpassword =newpassword.replaceAll(".","*");
                    System.out.println("Username: "+newloginname+"\nPassword: "+
                                        newpassword+"\nName: "+name1);
                }

        }
    else {
            JOptionPane.showMessageDialog(null,"Wrong Username or Password!");
            isValid=false;
         }
        }
    // Output data in "password" field, if any    
    // Output state of "remember me" check box    

    }

}

This is just the login screen for the program, but I just want to be able to:

  • Edit the user or password by choice, such as enter old pass: now new: repeat: and saving the new password.
  • Create a file dump only accessed through this program containing basic files such as docx, pptx, etc
  • How to edit the Username, password, or add new users to the program, only by having an admin password.
  • If I have the only account to this program and someone else wants one, the lines reading would be:

    Username: _____
    Password: _____
    Retype Password:_____
    Authentication: _____
    

    and it then creates a permament account.

Sorry for the unorthodox questions, but I am extremely curious, as well as new to these boards. I just want to know if it's possible with java, and if so, where I could reference the material to learn/teach myself.

4

1 回答 1

0

是的,这是可能的,但我认为在一篇文章中要回答的问题太多了。

您应该研究的是 Java 核心库中的 Scanner 类。使用 Scanner 类,您可以读取和读取文件。在这里搜索一些提示。因此,使用 Scanner,您可以拥有一个写入密码的文件(保持文件受保护),并且您可以通过重写文件上的密码(并从同一文件中读取)来更改密码。

于 2013-11-14T18:16:42.390 回答