第二次作业
[实验目的]
1.掌握软件开发的基本流程
2.掌握常用的软件开发方式和工具。
登录流程图:
登录代码:
package login;
import java.awt.*;
import javax.swing.*;
public class UserUI {
public void initUI(){
System.out.println ("UserUI");
//界面
JFrame jf = new JFrame ("用户界面");
jf.setSize (400, 400);//窗体大小
jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);//窗体关闭方式
jf.setLocationRelativeTo (null);// 居中
jf.setLayout (new FlowLayout ());
// 创建组件对象
//在界面中显示一张图片:
ImageIcon imgIcon = new ImageIcon ("src/login/111.png");//图标对象
JLabel imgjla = new JLabel (imgIcon);//把图片显示在一个标签里面
JLabel accountjla = new JLabel ("账号: ");
JTextField accountjtf = new JTextField (33);// 33个字符宽度
JLabel passwordjla = new JLabel ("密码: ");
JPasswordField passwordjpf = new JPasswordField (33);// 33个字符宽度
JButton loginjbt = new JButton ("登录");
JButton registjbt = new JButton ("注册");
// 添加组件 按顺序添加
jf.add (imgjla);
jf.add (accountjla);
jf.add (accountjtf);
jf.add (passwordjla);
jf.add (passwordjpf);
jf.add (loginjbt);
jf.add (registjbt);
jf.setVisible (true);
// 创建监听器对象 并添加给登录注册按钮
UIListener uiListener = new UIListener ();
loginjbt.addActionListener (uiListener);
registjbt.addActionListener (uiListener);
// 将界面上的输入框对象变量名 中存储的输入框对象地址 复制一份给监听器对象中输入框对象变量名
uiListener.accountjtf = accountjtf;
uiListener.passwordjpf = passwordjpf;
} public static void main(String[] args){
new UserUI ().initUI ();
}
}
package login;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UIListener implements ActionListener{
JTextField accountjtf;
JPasswordField passwordjpf;
// 创建一个用户数组
User[] userList = new User[10];
int index = 0;
// 重写点击按钮会调用的方法
// ActionEvent 动作事件 用来获取当前被点击的按钮的信息
@Override
public void actionPerformed(ActionEvent e){
// 获取 按钮上的文本 用来判断是哪个按钮被点击了
String ac = e.getActionCommand ();
// 获取输入框中的账号密码 进行验证 登录 注册
String account = accountjtf.getText ();
String password = passwordjpf.getText ();
System.out.println ("账号: " + account + " 密码: " + password);
if(account.equals ("") || password.equals ("")){
JOptionPane.showMessageDialog (null, "账号或密码不能为空");
return;
}
if(ac.equals ("登录")){
System.out.println ("登录按钮被点击了");
// 验证这个账号是否已经被注册过
for(int i = 0; i < index; i++){
User user = userList[i];
if(user.account.equals (account)){
boolean isLogin = user.login (password);
if(isLogin){
// 弹窗
JOptionPane.showMessageDialog (null, "登录成功");
// 小窗 信息显示
// 创建一个小窗对象
JFrame jf = new JFrame ("欢迎登录");
jf.setSize (400, 400);
JLabel info = new JLabel ();
info.setText ("欢迎您: " + user.showInfo ());
jf.add (info);
jf.setVisible (true);
} else{
// 弹窗
JOptionPane.showMessageDialog (null, "登录错误");
}
return;
}
}
JOptionPane.showMessageDialog (null, "账号不存在,请先注册~");
} else if(ac.equals ("注册")){
System.out.println ("注册按钮被点击了");
// 验证这个账号是否已经被注册过
for(int i = 0; i < index; i++){
User user = userList[i];
if(user.account.equals (account)){
// 弹窗
JOptionPane.showMessageDialog (null, "账号已经被注册过了");
return;
}
}
// 循环结束后 都没有弹窗 就表示这个账号没有被注册过
User user = new User ();
user.account = account;
user.password = password;
// 将存储了数据的用户对象 存储到数组中
userList[index] = user;
index++;
// 弹窗
JOptionPane.showMessageDialog (null, "注册成功,请登录~");
}
}
}
账号密码为空
账号已注册
账号未注册
登录成功
计算器代码:
package sy;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Counter extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
int i;
private final String[] str = { "+", "-", "*", "1", "2", "3", "/",
"4", "5", "6", "=", "7", "8", "9", "0" };
JButton[] buttons = new JButton[str.length];
JButton button = new JButton("CE");
JTextField text = new JTextField();
public Counter() {
super("我的计算器");
JPanel panel = new JPanel(new GridLayout(4, 4));
panel.add("North", button);
for (i = 0; i < str.length; i++) {
buttons[i] = new JButton(str[i]);/
if(i==14)
buttons[i].setBackground(Color.orange);
else if (i<3)
buttons[i].setBackground(Color.gray);
else if((i+2)%4!=0)
buttons[i].setBackground(Color.orange);
else buttons[i].setBackground(Color.gray);
panel.add(buttons[i]);
}
JPanel panel1 = new JPanel(new BorderLayout());
panel1.add("Center", text);
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North", panel1);
getContentPane().add("Center", panel);
for (i = 0; i < str.length; i++)
buttons[i].addActionListener(this);
button.addActionListener(this);
text.addActionListener(this);
setSize(450, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object f = e.getSource();
String label = e.getActionCommand();
if (f == button)
handleReset();
else if ("0123456789.".indexOf(label) >= 0)
handleNumber(label);
else
handleOperator(label);
}
boolean data = true;
public void handleNumber(String key) {
if (data)
text.setText(key);
else if ((key.equals(".")) && (text.getText().indexOf(".") <= 0))
text.setText(text.getText() + ".");
else if (!key.equals("."))
text.setText(text.getText() + key);
data = false;// 否则错误
}
double result = 0.0;
String operator = "=";
public void handleReset() {
text.setText("0");
data = true;
}
public void handleOperator(String key) {
if (operator.equals("+"))
result += Double.valueOf(text.getText());
else if (operator.equals("-"))
result -= Double.valueOf(text.getText());
else if (operator.equals("*"))
result *= Double.valueOf(text.getText());
else if (operator.equals("/"))
result /= Double.valueOf(text.getText());
else if (operator.equals("="))
result = Double.valueOf(text.getText());
text.setText(String.valueOf(result));
operator = key;
data = true;
}
public static void main(String[] args) {
new Counter();
}
}
加法
减法
乘法
除法