用java模拟设计一个简单的“用户注册”程序。当用户输入用户名和密码时,单击“注

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java模拟设计一个简单的“用户注册”程序。当用户输入用户名和密码时,单击“注相关的知识,希望对你有一定的参考价值。

用户注册
我们经常会在一些网站进行用户的注册,现在请你模拟设计一个简单的“用户注册”程序。当用户输入用户名和密码时,单击“注册”按钮,弹出提示信息“恭喜你,注册成功!”;当用户输入用户名已存在时,弹出提示信息“该用户已存在,请重新注册!”;如果用户没有输入用户名或密码,弹出提示信息“用户名或密码不能为空”。
求大神帮忙,本人菜鸟刚刚接触java

参考技术A 所有功能均已实现,如有不满意的地方我再修改

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Login extends JPanel

//声明各个控件
private JLabel user_name_label = null;
private JLabel password_label = null;
private JTextField user_name_text = null;
private JTextField password_text = null;
private JButton login = null;
private JButton regist = null;

//声明文件用以保存注册信息
private final String file_name = "注册.txt";

public Login()

//获得各个控件并且为之设置显示文本
user_name_label = new JLabel();
user_name_label.setText("姓名:");
password_label = new JLabel();
password_label.setText("密码:");
user_name_text = new JTextField();
password_text = new JTextField();
login = new JButton();
login.setText("登录");
regist = new JButton();
regist.setText("注册");

//设置面板的布局为网格布局
setLayout(new GridLayout(3,2));

//将控件添加到面板里
add(user_name_label);
add(user_name_text);
add(password_label);
add(password_text);
add(login);
add(regist);

//为两个按钮添加监听
regist.addActionListener(new ActionListener()

public void actionPerformed(ActionEvent e)

String name = user_name_text.getText().toString();
String password = password_text.getText().toString();
String str = null;
String[] result = null;
try

if((name.length() == 0)&&(password.length() == 0))

int a = JOptionPane.showConfirmDialog(null,"请输入用户名和密码","确认对话框",JOptionPane.YES_NO_OPTION);
throw new Exception("");

else if(name.length() == 0)

int a = JOptionPane.showConfirmDialog(null,"请输入用户名","确认对话框",JOptionPane.YES_NO_OPTION);

else if(password.length() == 0)

int a = JOptionPane.showConfirmDialog(null,"请输入密码","确认对话框",JOptionPane.YES_NO_OPTION);


InputStream in = new FileInputStream(file_name);
InputStreamReader reader = new InputStreamReader(in);
BufferedReader buffered_reader = new BufferedReader(reader);
while((str = buffered_reader.readLine()) != null)

result = str.split(" ");
if(result[0].equals(name))

int a = JOptionPane.showConfirmDialog(null,"该用户已存在,请重新注册","确认对话框",JOptionPane.YES_NO_OPTION);
throw new Exception("");



OutputStream out = new FileOutputStream(file_name,true);
OutputStreamWriter writer = new OutputStreamWriter(out);
BufferedWriter buffered_writer = new BufferedWriter(writer);
buffered_writer.write(name+" "+password);
buffered_writer.newLine();
buffered_writer.close();
int a = JOptionPane.showConfirmDialog(null,"恭喜你,注册成功!","确认对话框",JOptionPane.YES_NO_OPTION);

catch(Exception e1)




);

login.addActionListener(new ActionListener()

public void actionPerformed(ActionEvent e)

String name = user_name_text.getText().toString();
String password = password_text.getText().toString();
String result = null;

try

if((name.length() == 0)&&(password.length() == 0))

int a = JOptionPane.showConfirmDialog(null,"请输入用户名和密码","确认对话框",JOptionPane.YES_NO_OPTION);
throw new Exception("");

else if(name.length() == 0)

int a = JOptionPane.showConfirmDialog(null,"请输入用户名","确认对话框",JOptionPane.YES_NO_OPTION);

else if(password.length() == 0)

int a = JOptionPane.showConfirmDialog(null,"请输入密码","确认对话框",JOptionPane.YES_NO_OPTION);


InputStream in = new FileInputStream(file_name);
InputStreamReader reader = new InputStreamReader(in);
BufferedReader buffered_reader = new BufferedReader(reader);

while((result = buffered_reader.readLine()) != null)

if(result.equals(name+" "+password))

int a = JOptionPane.showConfirmDialog(null,"登陆成功","确认对话框",JOptionPane.YES_NO_OPTION);
break;


if(!(result.equals(name+" "+password)))

int a = JOptionPane.showConfirmDialog(null,"用户名或密码错误","确认对话框",JOptionPane.YES_NO_OPTION);


catch(Exception e1)

//e1.printStackTrace();


);


public static void main(String[] args)

JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Login(),BorderLayout.NORTH);

本回答被提问者和网友采纳
参考技术B 单机?后面是什么啊追问

我的问题后面又补充了

用java编写一个简单计算器

编写一个GUI程序,利用第一和第二个文本框分别输入两个整数,在单击运算符按钮时,则在第一和第二个文本框之间显示运算符;单击‘=’按钮,则在第三个文本框中显示第一和第二个文本框的运算结果。
就按照我给的题目,写出图片一样的程序

参考技术A

/*

 * @(#)JCalculator.java 1.00 06/17/2015

 */

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

/**

 * A simple calculator program.

 * <p>I saw this program in a QQ group, and help a friend correct it.</p>

 *

 * @author Singyuen Yip

 * @version 1.00 12/29/2009

 * @see JFrame

 * @see ActionListener

 */

public class JCalculator extends JFrame implements ActionListener

    /**

     * Serial Version UID

     */

    private static final long serialVersionUID = -169068472193786457L;

    /**

     * This class help close the Window.

     * @author Singyuen Yip

     *

     */

    private class WindowCloser extends WindowAdapter

       public void windowClosing(WindowEvent we)

           System.exit(0);

       

    

 

    int i;

    // Strings for Digit & Operator buttons.

    private final String[] str =  "7", "8", "9", "/", "4", "5", "6", "*","1",

           "2", "3", "-", ".", "0", "=", "+" ;

    // Build buttons.

    JButton[] buttons = new JButton[str.length];

    // For cancel or reset.

    JButton reset = new JButton("CE");

    // Build the text field to show the result.

    JTextField display = new JTextField("0");

   

    /**

     * Constructor without parameters.

     */

    public JCalculator()

       super("Calculator");

       // Add a panel.

       JPanel panel1 = new JPanel(new GridLayout(4, 4));

       // panel1.setLayout(new GridLayout(4,4));

       for (i = 0; i < str.length; i++)

           buttons[i] = new JButton(str[i]);

           panel1.add(buttons[i]);

       

       JPanel panel2 = new JPanel(new BorderLayout());

       // panel2.setLayout(new BorderLayout());

       panel2.add("Center", display);

       panel2.add("East", reset);

       // JPanel panel3 = new Panel();

       getContentPane().setLayout(new BorderLayout());

       getContentPane().add("North", panel2);

       getContentPane().add("Center", panel1);

       // Add action listener for each digit & operator button.

       for (i = 0; i < str.length; i++)

           buttons[i].addActionListener(this);

       // Add listener for "reset" button.

       reset.addActionListener(this);

       // Add listener for "display" button.

       display.addActionListener(this);

       // The "close" button "X".

       addWindowListener(new WindowCloser());

       // Initialize the window size.

       setSize(800, 800);

       // Show the window.

       // show(); Using show() while JDK version is below 1.5.

       setVisible(true);

       // Fit the certain size.

       pack();

      

   

    public void actionPerformed(ActionEvent e)

       Object target = e.getSource();

       String label = e.getActionCommand();

       if (target == reset)

           handleReset();

       else if ("0123456789.".indexOf(label) > 0)

           handleNumber(label);

       else

           handleOperator(label);

    

    // Is the first digit pressed?

    boolean isFirstDigit = true;

    /**

     * Number handling.

     * @param key the key of the button.

     */

    public void handleNumber(String key)

       if (isFirstDigit)

           display.setText(key);

       else if ((key.equals(".")) && (display.getText().indexOf(".") < 0))

           display.setText(display.getText() + ".");

       else if (!key.equals("."))

           display.setText(display.getText() + key);

       isFirstDigit = false;

    

   

    /**

     * Reset the calculator.

     */

    public void handleReset()

       display.setText("0");

       isFirstDigit = true;

       operator = "=";

    

 

    double number = 0.0;

    String operator = "=";

   

    /**

     * Handling the operation.

     * @param key pressed operator's key.

     */

    public void handleOperator(String key)

       if (operator.equals("+"))

           number += Double.valueOf(display.getText());

       else if (operator.equals("-"))

           number -= Double.valueOf(display.getText());

       else if (operator.equals("*"))

           number *= Double.valueOf(display.getText());

       else if (operator.equals("/"))

           number /= Double.valueOf(display.getText());

       else if (operator.equals("="))

           number = Double.valueOf(display.getText());

       display.setText(String.valueOf(number));

       operator = key;

       isFirstDigit = true;

    

   

    public static void main(String[] args)

       new JCalculator();

    


运行界面:

参考技术B package swing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Calculator extends JFrame
JTextField jt1 ,jt2 ,jt3;
JLabel jLabel;
JButton equButton,addButton,reduceButton,mulButton,divButton;
public Calculator() 
super("简易计算器");
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new GridLayout(2, 0));
JPanel uJPanel = new JPanel();
Dimension preferredSize = new Dimension(50, 29);
GridBagConstraints gbc = new GridBagConstraints();
uJPanel.setLayout(new GridBagLayout());
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 2, 1, 2);
jt1 = new JTextField(4);
jLabel = new JLabel();
jLabel.setPreferredSize(new Dimension(10, 29));
jt2 = new JTextField(4);
equButton = new JButton("=");
ActionListener myActionListener = new MyActionListener();
equButton.addActionListener(myActionListener);
jt3 = new JTextField(8);
jt1.setPreferredSize(preferredSize);
jt2.setPreferredSize(preferredSize);
jt3.setPreferredSize(preferredSize);
uJPanel.add(jt1,gbc);
uJPanel.add(jLabel,gbc);
uJPanel.add(jt2,gbc);
uJPanel.add(equButton,gbc);
gbc.weightx = 1;
uJPanel.add(jt3,gbc);
contentPanel.add(uJPanel);
JPanel dJPanel = new JPanel();
dJPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 2));
addButton = new JButton("+");
reduceButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
addButton.addActionListener(myActionListener);
reduceButton.addActionListener(myActionListener);
mulButton.addActionListener(myActionListener);
divButton.addActionListener(myActionListener);
dJPanel.add(addButton);
dJPanel.add(reduceButton);
dJPanel.add(mulButton);
dJPanel.add(divButton);
contentPanel.add(dJPanel);
this.setContentPane(contentPanel);
this.pack();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);

class MyActionListener implements ActionListener
Double d1,d2,d3;
String operator = "";
public void actionPerformed(ActionEvent e) 
String fun = e.getActionCommand();
if (!fun.equals("=")) 
jLabel.setText(fun);
operator = fun;
else 
d1 = jt1.getText().equals("")?null:Double.valueOf(jt1.getText());
d2 = jt2.getText().equals("")?null:Double.valueOf(jt2.getText());
d3 = calculate(d1,d2,operator);
jt3.setText(d3==null ? "":d3.toString());
jt3.setCaretPosition(0);



Double calculate(Double d1,Double d2,String operator)
if (d1 == null || d2 == null) 
return null;

Double d3 = null;
switch (operator) 
case "+":
d3 = d1 + d2;
break;
case "-":
d3 = d1 - d2;
break;
case "*":
d3 = d1 * d2;
break;
case "/":
d3 = d1 / d2;
break;

return d3;

public static void main(String[] args) 
new Calculator();

本回答被提问者和网友采纳
参考技术C 我已经写好了。需要的话,撩我追问

咋给我

追答

看我的名字。

追问

直接在这里回答不可以吗?

参考技术D 不错,都有现成的了
~~
第5个回答  2018-01-17 你这是样本吗?不是已经写好了?追问

哪有?那些都是很复杂的,我并不需要那么复杂的

追答

你什么要求

追问

就按照我给的那个要求做就可以了

以上是关于用java模拟设计一个简单的“用户注册”程序。当用户输入用户名和密码时,单击“注的主要内容,如果未能解决你的问题,请参考以下文章

java课程设计:设计一个计算器模拟程序。

模拟ATM系统 —— 用户注册登录和操用户作页设计 查询账号退出账号功能

Java多线程实现简单的售票程序

如何制作网页,完成一个简单的用户注册功能?

WEB程序设计(后台登陆页面和用户注册页面设计)

c#控制台编写用户注册程序,实现用户注册