⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buttondemo.m

📁 MATLAB-GUI初学相关资料 包含一些小程序与word文档ppt
💻 M
字号:
function buttondemo(varargin)
% A MATLAB version of the java tutorial example - How to use buttons

% Han Qun 
% College of Precision Instrument & Optoelectronics Engineering,
% Tianjin University, Tianjin 30072, P.R.China
% junziyang@126.com

% 01/10/05

% Creat a MATLAB figure and hide it,so that your GUI can appear normally
% after it is transformed into an EXE by the MATLAB Compiler
% h=figure('Visible','off');

% Import the necessary java packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

% Creat a window with the given name
frame = JFrame('ButtonDemo');

% Creat a panel to hold you buttons
panel = JPanel();

% Prepare the button icons
leftButtonIcon = ImageIcon('images/right.gif');
middleButtonIcon = ImageIcon('images/middle.gif');
rightButtonIcon = ImageIcon('images/left.gif');

% Creat the buttons
btn_left = JButton('Disable middle button',leftButtonIcon);
btn_left.setVerticalTextPosition(AbstractButton.CENTER);
btn_left.setHorizontalTextPosition(AbstractButton.LEFT);
btn_left.setMnemonic(KeyEvent.VK_D);
btn_left.setActionCommand('disable');

btn_middle = JButton('Middle button',middleButtonIcon);
btn_middle.setVerticalTextPosition(AbstractButton.BOTTOM);
btn_middle.setHorizontalTextPosition(AbstractButton.CENTER);
btn_middle.setMnemonic(KeyEvent.VK_M);

btn_right = JButton('Enable middle button',rightButtonIcon);
% Use the default text positionof CENTER, RIGHT.
btn_right.setMnemonic(KeyEvent.VK_E);
btn_right.setActionCommand('enable');
btn_right.setEnabled(false);

% Set the tips of the left and right button
btn_left.setToolTipText('Click this button to disable the middle button.');
btn_right.setToolTipText('This middle button does nothing when you click it.');

% Prepare a handle structure of the GUI components so that they can be
% interacted by the callback functions.
handles.frame = frame;
handles.btn_left = btn_left;
handles.btn_middle = btn_middle;
handles.btn_right = btn_right;
%Listen for actions on the left button and the right button.
set(btn_left,'ActionPerformedCallback',{@ButtonCallback1,handles});
set(btn_right,'ActionPerformedCallback',{@ButtonCallback1,handles});

% Add buttons to the panel.
panel.add(btn_left);
panel.add(btn_middle);
panel.add(btn_right);

% Add panel to the frame.
frame.getContentPane().add(panel);

% Set frame size and show it.
frame.pack;
frame.setLocation(300,300);
frame.show;

% set the closing behavior of the frame
% set(0,'CurrentFigure',h);

% set(frame,'WindowClosingCallback','delete(gcf);');

% Wait and listen for the user actions, untill the GUI being closed
% waitfor(h);
% frame.dispose;

% Button callback function
function ButtonCallback1(hObject,eventdata,handles)
eventdata
actionCommand = get(hObject,'ActionCommand');
try
    switch actionCommand
        case 'enable'
            handles.btn_left.setEnabled(true);
            handles.btn_middle.setEnabled(true);
            handles.btn_right.setEnabled(false);
        case 'disable'
            handles.btn_left.setEnabled(false);
            handles.btn_middle.setEnabled(false);
            handles.btn_right.setEnabled(true);
    end
    handles.frame.show;
catch
    err=lasterror;
    msgbox(err.message,'Unrecognized indicator','error','modal');
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -