Matlab----GUI编程-登录框实现(纯m文件)

1. 功能

  • 用户名输入
  • 密码输入(隐藏密码,带星号)
  • 输入正确错误提示
  • 带背景图片
  • 带logo修改

2. 界面
Matlab----GUI编程-登录框实现(纯m文件)_第1张图片

Matlab----GUI编程-登录框实现(纯m文件)_第2张图片
Matlab----GUI编程-登录框实现(纯m文件)_第3张图片
3. 源代码

【loginSys.m
function loginSys
%% 清除变量,清命令窗口屏,清除图形
clear;
clc;
%% 新建图形并设置相关属性
%scrsz = get(0,'ScreenSize');
Hd_login = figure('NumberTitle', 'off', 'Resize','off','Color',[0.753,0.753,0.753],'Name', '登录','tag','Hd_login');
%取得Figure的JavaFrame。
%修改图标
newIcon = javax.swing.ImageIcon('.\images\icon.png');
figFrame = get(Hd_login,'JavaFrame');            
figFrame.setFigureIcon(newIcon);
%转换图形窗口菜单条的“开”与“关”5
set(Hd_login,'menubar','none','Unit','normalized','Position',[0.28,0.32,0.38,0.45]);
%% 设置登录窗口背景图片
%% 添加背景图片
Hd_axes = axes('units','normalized','position',[0 0 1 1],'tag','Hd_axes');
uistack(Hd_axes,'down')
II=imread('.\images\login.jpg');
image(II)
% colormap gray
set(Hd_axes,'handlevisibility','off','visible','off');
%% 添加登录所需控件:用户名框、密码框等
%用户名
Hd_user = uicontrol('Parent',Hd_login,'Style','text',...
    'String','用户名',...
    'Units','normalized',...
    'FontWeight','bold',...    
    'Background',[0.8 0.8 0.8],...
    'ForegroundColor','black',...
    'Units','normalized',...
    'FontSize',25,...
    'Enable','inactive',...
    'tag','Hd_user',...
    'Position',[0.1,0.68,0.25,0.15],...
    'CallBack','');
Hd_unserin = uicontrol('Parent',Hd_login,'Style','Edit',...
    'String','',...
    'FontWeight','bold',...
    'ForegroundColor',[0.871,0.49,0],...
    'Units','normalized',...
    'FontSize',10,...
    'HorizontalAlignment','left',...
    'tag','Hd_unserin',...
    'Position',[0.38,0.68,0.55,0.15],...
    'CallBack','');
%密码
Hd_passwd = uicontrol('Parent',Hd_login,'Style','text',...
    'String','密 码',...
    'Units','normalized',...
    'FontWeight','bold',...    
    'Background',[0.8 0.8 0.8],...
    'ForegroundColor','black',...
    'Units','normalized',...
    'FontSize',25,...
    'Enable','inactive',...
    'tag','Hd_passwd',...
    'Position',[0.1,0.48,0.25,0.15],...
    'CallBack','');
%java 实现密码文本框输入
jPass = javax.swing.JPasswordField;
J_passwdin = javacomponent(jPass,[198 167 287 52]);
%提示 文本框
hTip = uicontrol('style','text','visible','off','String','',...
    'FontSize',18,...
    'FontWeight','bold',...
    'ForegroundColor','red',...
    'position',[150 300 250 30]);
%登录,修改密码
uicontrol('pos',[100 80 140 40],'string','确认','FontWeight','bold',...
    'ForegroundColor',[0 0 1],...
    'FontSize',18,...
    'callback',{@loginCheck,Hd_unserin,J_passwdin,hTip});
uicontrol('pos',[290 80 140 40],'string','修改密码','FontWeight','bold',...
    'ForegroundColor',[0 0 1],...
    'FontSize',18,...
    'callback',{@modifyPasswd,hTip});

function modifyPasswd(~,~,hTip)
    set(hTip,'Visible','on','string','请联系开发人员!');
    pause(3);
    set(hTip,'Visible','off');
【loginCheck.m
function loginCheck(~, ~,hUser,hPass,hTip)
    user = get(hUser,'String')
    pswd = hPass.text
    if (strcmp(pswd, '111111') && strcmp(user,'FreeApe'))
        set(hTip,'Visible','off','string','密码正确');
        out = '密码正确'
        %do something...(比如进入系统主界面等)
    else
        set(hTip,'Visible','on','string','用户名或密码错误!');
        pause(2);
        set(hTip,'Visible','off');
        out = '密码错误'
    end

你可能感兴趣的:(matlab,GUI编程,matlab登录窗口,纯m文件实现)