基于Dialog的程序为什么不能使用类似VK_0的KeyEvent

foniex 2003-08-18 05:12:25
在基于Dialog的程序中对键盘消息处理,为什么不能使用类似VK_0的KeyEvent?

BOOL CDrawScreenDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base Class
CDrawScreenDlg *pDlg=(CDrawScreenDlg*)AfxGetApp()->m_pMainWnd;
if(pMsg->message==WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_RIGHT:// 正确
myfunc1();
break;
case VK_0://error C2065: 'VK_0' : undeclared Identifier myfunc2();
default:
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
...全文
96 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangweintk 2003-08-19
  • 打赏
  • 举报
回复
没有定义,要不按顶楼的建议自己定义,

但是还是按我上面这位老兄的方法比较方便。
zhucde 2003-08-18
  • 打赏
  • 举报
回复
数字键和字母键直接像如下使用:
'1','2',...
'A','B',...
feeboby 2003-08-18
  • 打赏
  • 举报
回复
找到0的键值
#define vk_0 xx
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; public class NotePad { public static void main(String[] args){ myfr fr=new myfr("猪猪java记事本"); fr.setLocation(100,100); fr.setSize(750,650); } } ///////////////////////////myfr主窗体类////////////////////////////////////// class myfr extends JFrame implements ActionListener{ String str,strnext,path,fname; //部分变量的声明 JPanel mainpane; JFileChooser choose = new JFileChooser(); //文件对话框 Dialog find,replace; //find为查找对话框,replace对话框 JTextField findtxt,repltxt; //find为查找对话框的输入文本区,replace对话框的输入文本区 Font newfont; JButton findenter,replb; //find为查找对话框,replace对话框的确定按钮 JLabel state=new JLabel(" 猪猪java记事本------"); //状态栏 JTextArea txt1; //主输入文本区 File newfiles; JPopupMenu popm; //弹出菜单声明 JMenu m1,m2,m3,m4,m5,m6; //各菜单项 JMenuItem m61,m62,m26,m271,m34,m51,m52,m53,m54,p_copy,p_cut,p_paste,p_del; int startp,endp,nexttemp,newstartp,newendp; //查找替换时所用的临时变量 JToolBar toolbar = new JToolBar(); //工具条 JButton newf=new JButton(new ImageIcon("9.jpg")); //图标在PIC下 JButton open=new JButton(new ImageIcon("2.jpg")); JButton save=new JButton(new ImageIcon("3.jpg")); JButton copy=new JButton(new ImageIcon("6.jpg")); JButton cut=new JButton(new ImageIcon("7.jpg")); JButton pp=new JButton(new ImageIcon("8.jpg")); JButton del=new JButton(new ImageIcon("pic/del.gif")); JButton findc=new JButton(new ImageIcon("9.jpg")); JButton color=new JButton(new ImageIcon("10.jpg")); JButton help=new JButton(new ImageIcon("pic/help.gif")); JButton exit=new JButton(new ImageIcon("pic/exit.gif")); myfr(String sss){ /////构造函数开始 super(sss); JMenuBar mb=new JMenuBar(); fname=null; //初始文件名为空 findenter=new JButton("确定"); findenter.addActionListener(this); //声明对话框中上确定按钮,并注册事件 replb=new JButton("确定"); replb.addActionListener(this); mainpane=(JPanel)this.getContentPane(); mainpane.setLayout(new BorderLayout()); txt1=new JTextArea("",13,61); txt1.addMouseListener(new handlemouse(this));//注册鼠标右击事件 txt1.setFont(new Font("宋体",Font.PLAIN,18)); mainpane.add(txt1, BorderLayout.CENTER); mainpane.add("North",toolbar); mainpane.add("South",state); JScrollPane sll = new JScrollPane(); //创建滚动条 mainpane.add("Center", sll); ; sll.getViewport().add(txt1); //将滚动条装入文本区 //文本栏中的浮动条 popm=new JPopupMenu(); ////POPMeun 开始 p_copy=new JMenuItem("复制 "); p_copy.addActionListener(this); KeyStroke keycopyp=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK); p_copy.setAccelerator(keycopyp); p_cut=new JMenuItem("剪切 "); p_cut.addActionListener(this); KeyStroke keycutp=KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK); p_cut.setAccelerator(keycutp); p_paste=new JMenuItem("粘贴 "); p_paste.addActionListener(this); KeyStroke keypp=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK); p_paste.setAccelerator(keypp); p_del=new JMenuItem("删除 "); p_del.addActionListener(this); KeyStroke keydelp=KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0); p_del.setAccelerator(keydelp); popm.add(p_copy); popm.add(p_cut); popm.add(p_paste); popm.add(p_del); txt1.add(popm); ////POPMenu 结束 m1=new JMenu("文件(F)"); m1.setMnemonic('F'); //定义快捷方式 JMenuItem m10=new JMenuItem("新建 "); m10.addActionListener(this); //注册事件监听器 KeyStroke keynew=KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK); //定义快捷键 m10.setAccelerator(keynew); JMenuItem m11=new JMenuItem("打开 "); m11.addActionListener(this); KeyStroke keyopen=KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK); m11.setAccelerator(keyopen); JMenuItem m12=new JMenuItem("保存 "); m12.addActionListener(this); KeyStroke keysave=KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK); m12.setAccelerator(keysave); JMenuItem m13=new JMenuItem("另保存为 "); m13.addActionListener(this); JMenuItem m14=new JMenuItem("退出 "); m14.addActionListener(this); KeyStroke keyexit=KeyStroke.getKeyStroke(KeyEvent.VK_F4,Event.ALT_MASK); m14.setAccelerator(keyexit); //////////////////////////////////////////// m2=new JMenu("编辑(E)"); m2.setMnemonic('E'); JMenuItem m21=new JMenuItem("复制 "); m21.addActionListener(this); KeyStroke keycopy=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK); m21.setAccelerator(keycopy); JMenuItem m22=new JMenuItem("剪切 "); m22.addActionListener(this); KeyStroke keycut=KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK); m22.setAccelerator(keycut); JMenuItem m23=new JMenuItem("粘贴 "); m23.addActionListener(this); KeyStroke keyp=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK); m23.setAccelerator(keyp); JMenuItem m24=new JMenuItem("删除 "); m24.addActionListener(this); KeyStroke keydel=KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0); m24.setAccelerator(keydel); JMenuItem m25=new JMenuItem("查找 "); m25.addActionListener(this); KeyStroke keyfind=KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK); m25.setAccelerator(keyfind); m26=new JMenuItem("查找下一个 "); m26.addActionListener(this); KeyStroke keyfn=KeyStroke.getKeyStroke(KeyEvent.VK_F3,0); m26.setAccelerator(keyfn); m26.setEnabled(false); JMenuItem m27=new JMenuItem("替换 "); m27.addActionListener(this); KeyStroke keyrepl=KeyStroke.getKeyStroke(KeyEvent.VK_H,Event.CTRL_MASK); m27.setAccelerator(keyrepl); m271=new JMenuItem("替换下一个"); m271.setEnabled(false); m271.addActionListener(this); KeyStroke keyrepn=KeyStroke.getKeyStroke(KeyEvent.VK_F6,0); m271.setAccelerator(keyrepn); JMenuItem m28=new JMenuItem("全选 "); m28.addActionListener(this); KeyStroke keyall=KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK); m28.setAccelerator(keyall); JMenuItem m29=new JMenuItem("日期/时间 "); m29.addActionListener(this); m3=new JMenu("格式(O)"); m3.setMnemonic('O'); JMenu m31=new JMenu("字体样式 "); JMenuItem m311=new JMenuItem("正常 "); m311.addActionListener(this); JMenuItem m312=new JMenuItem("粗体 "); m312.addActionListener(this); JMenuItem m313=new JMenuItem("斜体 "); m313.addActionListener(this); JMenu m32=new JMenu("字体大小 "); JMenuItem m321=new JMenuItem("最大 "); m321.addActionListener(this); JMenuItem m322=new JMenuItem("较大 "); m322.addActionListener(this); JMenuItem m323=new JMenuItem("适中 "); m323.addActionListener(this); JMenuItem m324=new JMenuItem("较小 "); m324.addActionListener(this); JMenuItem m325=new JMenuItem("最小 "); m325.addActionListener(this); JMenuItem m33=new JMenuItem("字体颜色 "); m33.addActionListener(this); m34=new JMenuItem("自动换行 "); m34.addActionListener(this); m5=new JMenu("视图风格(V)"); m5.setMnemonic('V'); //m51=new JMenuItem("系统风格 "); //m51.addActionListener(this); m52=new JMenuItem("MOTIF风格 "); m52.addActionListener(this); m53=new JMenuItem("默认风格 "); m53.addActionListener(this); m54=new JMenuItem("状态栏 "); m54.addActionListener(this); m6=new JMenu("帮助(H)"); m6.setMnemonic('H'); m61=new JMenuItem("帮助主题 "); m61.addActionListener(this); m62=new JMenuItem("关于 "); m62.addActionListener(this); //添加各项 m1.add(m10); m1.add(m11); m1.add(m12); m1.add(m13); m1.addSeparator(); m1.add(m14); m2.add(m21); m2.add(m22); m2.add(m23); m2.add(m24); m2.addSeparator(); m2.add(m25); m2.add(m26); m2.add(m27); m2.add(m271); m2.addSeparator(); m2.add(m28); m2.add(m29); m3.add(m31); m31.add(m311); m31.add(m312); m31.add(m313); m3.add(m32); m32.add(m321); m32.add(m322); m32.add(m323); m32.add(m324); m32.add(m325); m3.add(m33); m3.addSeparator(); m3.add(m34); //m5.add(m51); m5.add(m52); m5.add(m53); m5.addSeparator(); m5.add(m54); m6.add(m61); m6.addSeparator(); m6.add(m62); mb.add(m1); mb.add(m2); mb.add(m3); mb.add(m5); mb.add(m6); this.setJMenuBar(mb); //设置菜单栏 toolbar.add(newf); //工具栏各按钮 newf.setToolTipText("新建"); newf.addActionListener(this); //toolbar.add(open); //open.setToolTipText("打开"); //open.addActionListener(this); //toolbar.add(save); //save.setToolTipText("保存"); //save.addActionListener(this); //toolbar.addSeparator(); toolbar.add(copy); copy.setToolTipText("复制"); copy.addActionListener(this); toolbar.add(cut); cut.setToolTipText("剪切"); cut.addActionListener(this); toolbar.add(pp); pp.setToolTipText("粘贴"); pp.addActionListener(this); //toolbar.add(del); //del.setToolTipText("删除"); //del.addActionListener(this); toolbar.addSeparator(); //toolbar.add(findc); //findc.setToolTipText("查找"); //findc.addActionListener(this); toolbar.add(color); color.setToolTipText("字体颜色"); color.addActionListener(this); toolbar.addSeparator(); //toolbar.add(help); //help.setToolTipText("帮助主题"); //help.addActionListener(this); //toolbar.add(exit); //exit.setToolTipText("退出"); //exit.addActionListener(this); setVisible(true); pack(); show(); this.addWindowListener(new xxx(this)); }///构造函数结束/// ////////////////事件处理///////////////////////////////////// public void actionPerformed(ActionEvent p){ if(p.getActionCommand()=="新建 "||p.getSource()==newf){ //响应菜单及工具栏事件 fname=null; //置文件名为空,便于判断文件是否保存过 txt1.setText(""); state.setText(" 猪猪java记事本------"); } if(p.getActionCommand()=="打开 "||p.getSource()==open){ try { if(this.choose.APPROVE_OPTION==this.choose.showOpenDialog(this)){ path=this.choose.getSelectedFile().getPath(); fname=this.choose.getSelectedFile().getName(); File file=new File(path); int flength=(int)file.length(); FileReader fReader=new FileReader(file); char[] data=new char[flength]; fReader.read(data,0,flength); txt1.setText(new String(data)); state.setText(" 猪猪java记事本------"+path+" 共"+flength+"字节");//状态栏统计文件字节数 txt1.setCaretPosition(0); } }catch(IOException e){} } if(p.getActionCommand()=="保存 "||p.getSource()==save){ if(fname==null){ othersave(); } //如果文件名为空,说明文件未被创建,弹出另存为对话框 try { File savefile=new File(path); savefile.createNewFile(); FileWriter fw=new FileWriter(savefile); fw.write(txt1.getText()); fw.close(); }catch(IOException e){} } if(p.getActionCommand()=="另保存为 "){ othersave(); } if(p.getActionCommand()=="退出 "||p.getSource()==exit){ exit(); } //////////////////编辑//////////////////// if(p.getActionCommand()=="复制 "||p.getSource()==copy) { txt1.copy(); } if(p.getActionCommand()=="剪切 "||p.getSource()==cut) { txt1.cut(); } if(p.getActionCommand()=="粘贴 "||p.getSource()==pp) { txt1.paste(); } if(p.getActionCommand()=="删除 "||p.getSource()==del){ txt1.replaceSelection(""); } /////////////////////////////////////////////////////// if(p.getActionCommand()=="全选 "){ txt1.selectAll(); } if(p.getActionCommand()=="日期/时间 "){ //用DATE类插入当前日期/时间,编译时会出现说明 int inpoint=txt1.getCaretPosition(); Date dt=new Date(); String strdate=dt.toLocaleString(); txt1.insert(strdate,inpoint);} //格式// //////////字体样式//////////////// if(p.getActionCommand()=="正常 "){ newfont=txt1.getFont(); //得到当前应用字体,获得字体大小,创建相应样式 int size=newfont.getSize(); txt1.setFont(new Font("宋体",Font.PLAIN,size)); } if(p.getActionCommand()=="粗体 "){ newfont=txt1.getFont(); int size=newfont.getSize(); txt1.setFont(new Font("宋体",Font.BOLD,size)); } if(p.getActionCommand()=="斜体 "){ newfont=txt1.getFont(); int size=newfont.getSize(); txt1.setFont(new Font("宋体",Font.ITALIC,size)); } ////////////字体大小///////////////// if(p.getActionCommand()=="最大 "){ newfont=txt1.getFont(); ////得到当前应用字体,获得字体样式,创建相应字体的大小 int sty=newfont.getStyle() ; txt1.setFont(new Font("宋体",sty,35)); } if(p.getActionCommand()=="较大 "){ newfont=txt1.getFont(); int sty=newfont.getStyle() ; txt1.setFont(new Font("宋体",sty,30)); } if(p.getActionCommand()=="适中 "){ newfont=txt1.getFont(); int sty=newfont.getStyle() ; txt1.setFont(new Font("宋体",sty,18)); } if(p.getActionCommand()=="较小 "){ newfont=txt1.getFont(); int sty=newfont.getStyle() ; txt1.setFont(new Font("宋体",sty,16)); } if(p.getActionCommand()=="最小 "){ newfont=txt1.getFont(); int sty=newfont.getStyle() ; txt1.setFont(new Font("宋体",sty,14)); } if(p.getActionCommand()=="字体颜色 "||p.getSource()==color){ //字体颜色 JColorChooser jColor=new JColorChooser(); //调用颜色面板,设置前景就可更改字体颜色 Color fcolor=txt1.getForeground(); txt1.setForeground( jColor.showDialog(txt1,"选择字体颜色",fcolor)); } if(p.getActionCommand()=="自动换行 "){ txt1.setLineWrap(true); m34.setEnabled(false);} //if(p.getActionCommand()=="系统风格 ") //不稳定,在不同编译平台下表现不一致 // { } if(p.getActionCommand()=="MOTIF风格 "){ this.dispose(); myfr ww=new myfr("我的javaX记事本"); try { //MOTIF界面 UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); SwingUtilities.updateComponentTreeUI(ww); }catch ( Exception e ) {} } if(p.getActionCommand()=="默认风格 "){ this.dispose(); myfr ww=new myfr("猪猪java记事本"); try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(ww); }catch ( Exception e ) {} } if(p.getActionCommand()=="状态栏 ") //状态栏的隐藏和显视 {state.setVisible( !(state.isVisible()) );} /////////////////////帮助/////////////////////////////////////////// if(p.getActionCommand()=="帮助主题 "||p.getSource()==help){ myhelp help=new myhelp(); help.setSize(200,240);} if(p.getActionCommand()=="关于 "){ JLabel prompt=new JLabel("欢迎使用我的JAVAX记事本",JLabel.CENTER); JOptionPane.showMessageDialog(null,prompt,"关于我的JAVAX记事本",JOptionPane.INFORMATION_MESSAGE); } } public void othersave() {//另存为方法 if(choose.APPROVE_OPTION==choose.showSaveDialog(this)){ path=choose.getSelectedFile().getPath(); newfiles=new File(path); state.setText(" 猪猪java记事本------"+path); fname=choose.getSelectedFile().getName();; try { newfiles=new File(path); newfiles.createNewFile(); FileWriter fw=new FileWriter(newfiles); fw.write(txt1.getText()); fw.close(); }catch(IOException e){} } } public void notfindmethod(){ //提示查找不到的方法notfindmethod if(!txt1.getSelectedText().equals(findtxt.getText())) { txt1.setCaretPosition(0); //光标返回文件头部 JOptionPane.showMessageDialog(null,"查找不到对应的字符!","查找错误",JOptionPane.ERROR_MESSAGE); m26.setEnabled(false); } } public void exit(){ int value; String[] qq={"返回", "退出"}; value=JOptionPane.showOptionDialog(null, "你确定退出吗?请注意保存文件!", "退出程序?", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,null, qq, qq[0]); if(value==JOptionPane.YES_OPTION) return; else if(value==JOptionPane.NO_OPTION) { System.exit(0); } } }//主窗口类结束// class xxx extends WindowAdapter{ //关闭窗口XXX类//////////// myfr fx; xxx(myfr fxx) {fx=fxx;} public void windowClosing(WindowEvent e){ JOptionPane.showMessageDialog(null,"欢迎使用本软件!","退出",JOptionPane.INFORMATION_MESSAGE); fx.dispose(); System.exit(0); } } class xxxhelp extends WindowAdapter{ //关闭帮助主题窗口类XXXhelp myhelp fx; xxxhelp(myhelp fxx){ fx=fxx;} public void windowClosing(WindowEvent e){ fx.dispose(); } } class myhelp extends JFrame{ //帮助主题类///////////////// TextArea helparea; myhelp() { super("我的帮助主题"); helparea=new TextArea("",45,25,TextArea.SCROLLBARS_NONE);//创建没有滚动条的帮助窗口 helparea.setText(" "); helparea.append("我的JAVAX记事本是一个用来创建简单的文档的基本的文本编辑器.可用于打开查看TXT文档。"); helparea.append("要编辑文字,请先选定它,然后单击相应菜单上的相关命令。"); helparea.append("鼠标悬停在工具栏上会出现相关提示。本软件在SDK 1.40下编写,"); helparea.append("最好在1.4环境下运行,否则会出现一些小问题,但不影响正常使用……"); setResizable(false); getContentPane().add(helparea); show();pack(); helparea.setFont(new Font("宋体",Font.PLAIN,14)); helparea.setEnabled(false); addWindowListener(new xxxhelp(this)); } } class handlemouse extends MouseAdapter //处理右键弹出菜单类 { myfr fx; handlemouse(myfr fxx){ fx=fxx; } public void mouseReleased(MouseEvent n){ if(n.isPopupTrigger()) fx.popm.show((Component)n.getSource(),n.getX(),n.getY()); } }
Socket聊天室源程序 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.net.*; /* * 聊天客户端的主框架类 */ public class ChatClient extends JFrame implements ActionListener{ String ip = "127.0.0.1";//连接到服务端的ip地址 int port = 8888;//连接到服务端的端口号 String userName = "匆匆过客";//用户名 int type = 0;//0表示未连接,1表示已连接 Image icon;//程序图标 JComboBox combobox;//选择发送消息的接受者 JTextArea messageShow;//客户端的信息显示 JScrollPane messageScrollPane;//信息显示的滚动条 JLabel express,sendToLabel,messageLabel ; JTextField clientMessage;//客户端消息的发送 JCheckBox checkbox;//悄悄话 JComboBox actionlist;//表情选择 JButton clientMessageButton;//发送消息 JTextField showStatus;//显示用户连接状态 Socket socket; ObjectOutputStream output;//网络套接字输出流 ObjectInputStream input;//网络套接字输入流 ClientReceive recvThread; //建立菜单栏 JMenuBar jMenuBar = new JMenuBar(); //建立菜单组 JMenu operateMenu = new JMenu ("操作(O)"); //建立菜单项 JMenuItem loginItem = new JMenuItem ("用户登录(I)"); JMenuItem logoffItem = new JMenuItem ("用户注销(L)"); JMenuItem exitItem=new JMenuItem ("退出(X)"); JMenu conMenu=new JMenu ("设置(C)"); JMenuItem userItem=new JMenuItem ("用户设置(U)"); JMenuItem connectItem=new JMenuItem ("连接设置(C)"); JMenu helpMenu=new JMenu ("帮助(H)"); JMenuItem helpItem=new JMenuItem ("帮助(H)"); //建立工具栏 JToolBar toolBar = new JToolBar(); //建立工具栏中的按钮组件 JButton loginButton;//用户登录 JButton logoffButton;//用户注销 JButton userButton;//用户信息的设置 JButton connectButton;//连接设置 JButton exitButton;//退出按钮 //框架的大小 Dimension faceSize = new Dimension(400, 600); JPanel downPanel ; GridBagLayout girdBag; GridBagConstraints girdBagCon; public ChatClient(){ init();//初始化程序 //添加框架的关闭事件处理 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); //设置框架的大小 this.setSize(faceSize); //设置运行时窗口的位置 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setLocation( (int) (screenSize.width - faceSize.getWidth()) / 2, (int) (screenSize.height - faceSi
简单的计算器 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyCalculator extends Frame implements ActionListener { JTextField txt = new JTextField(""); StringBuffer copyBoard=new StringBuffer(20); private String cmd = "="; private double result = 0;// 运算结果 private boolean start = true; StringBuffer strs=new StringBuffer(); class Window { Window() { Frame help=new Frame("关于作者"); help.setBounds(400,200,200,200); help.setVisible(true); help.setResizable(false); help.addWindowListener(new WindowAdapter() //关闭窗口 { public void windowClosing(WindowEvent e) { ((Frame)e.getSource()).dispose(); } } ); TextArea title = new TextArea(" 软件125实训项目 一 制作:第二组 常志铭 朱靖 2013.5.10 ",10,8,TextArea.SCROLLBARS_NONE); title.setBounds(50,50,200,30); title.setEditable(false); help.add(title); } } MyCalculator() { this.setTitle("我的计算器"); this.setBounds(400,150,225,320); this.createMenu(); this.createMainPanel(); this.addWindowListener(new WindowAdapter() //关闭窗口 { public void windowClosing(WindowEvent e) { System.exit(0); } } ); this.setResizable(false); this.setVisible(true); } private void createMenu() { MenuBar bar = new MenuBar(); this.setMenuBar(bar); Menu EditMenu = new Menu("编辑(E)"); Menu helpMenu = new Menu("帮助(H)"); bar.add(EditMenu); bar.add(helpMenu); MenuItem copyItem = new MenuItem("复制"); copyItem.setShortcut(new MenuShortcut(KeyEvent.VK_C,false)); EditMenu.add(copyItem); copyItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String temp = txt.getText().trim(); copyBoard.replace(0, copyBoard.length(), temp); } } ); MenuItem pasteItem = new MenuItem("粘帖"); pasteItem.setShortcut(new MenuShortcut(KeyEvent.VK_V,false)); EditMenu.add(pasteItem); pasteItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txt.setText(copyBoard.toString()); result=(Double.parseDouble(copyBoard.toString())); } } ); MenuItem helpItem = new MenuItem("关于计算器"); helpItem.setShortcut(new MenuShortcut(KeyEvent.VK_H,false)); helpMenu.add(helpItem); helpItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { new Window(); } } ); } private void createMainPanel() { //整体面板 Panel Win = new Panel(null); Win.setSize(230, 330); Win.setVisible(true); //Win.setBackground(Color.black); this.add(Win); //显示 txt.setHorizontalAlignment(JTextField.RIGHT); txt.setBounds(5,5,210,50); txt.setFont(new java.awt.Font("Dialog", 1, 30)); txt.setEditable(false); Win.add(txt); //按键面板 Panel Butt = new Panel(null); Butt.setBounds(0, 50, 230, 280); //Butt.setBackground(Color.white); Butt.setVisible(true); Win.add(Butt); Button butx = new Button("C"); butx.setBounds(15,15,40,28); butx.addActionListener(this); butx.setFont(new java.awt.Font("Dialog", 1, 15)); Butt.add(butx); Button butz = new Button("←"); butz.setBounds(65,15,40,28); butz.setFont(new java.awt.Font("Dialog", 1, 20)); butz.addActionListener(this); Butt.add(butz); Button butcf = new Button("/"); butcf.setBounds(115,15,40,28); butcf.setFont(new java.awt.Font("Dialog", 0, 20)); butcf.addActionListener(this); Butt.add(butcf); Button butc = new Button("*"); butc.setBounds(165,15,40,28); butc.setFont(new java.awt.Font("Dialog", 1, 25)); butc.addActionListener(this); Butt.add(butc); Button but7 = new Button("7"); but7.setBounds(15,55,40,28); but7.setFont(new java.awt.Font("Dialog", 0, 15)); but7.addActionListener(this); Butt.add(but7); Button but8 = new Button("8"); but8.setBounds(65,55,40,28); but8.setFont(new java.awt.Font("Dialog", 0, 15)); but8.addActionListener(this); Butt.add(but8); Button but9 = new Button("9"); but9.setBounds(115,55,40,28); but9.setFont(new java.awt.Font("Dialog", 0, 15)); but9.addActionListener(this); Butt.add(but9); Button butjf = new Button("-"); butjf.setBounds(165,55,40,28); butjf.setFont(new java.awt.Font("Dialog", 0, 28)); butjf.addActionListener(this); Butt.add(butjf); Button but4 = new Button("4"); but4.setBounds(15,95,40,28); but4.setFont(new java.awt.Font("Dialog", 0, 15)); but4.addActionListener(this); Butt.add(but4); Button but5 = new Button("5"); but5.setBounds(65,95,40,28); but5.setFont(new java.awt.Font("Dialog", 0, 15)); but5.addActionListener(this); Butt.add(but5); Button but6 = new Button("6"); but6.setBounds(115,95,40,28); but6.setFont(new java.awt.Font("Dialog", 0, 15)); but6.addActionListener(this); Butt.add(but6); Button butj = new Button("+"); butj.setBounds(165,95,40,28); butj.setFont(new java.awt.Font("Dialog", 0, 20)); butj.addActionListener(this); Butt.add(butj); Button but1 = new Button("1"); but1.setBounds(15,135,40,28); but1.setFont(new java.awt.Font("Dialog", 0, 15)); but1.addActionListener(this); Butt.add(but1); Button but2 = new Button("2"); but2.setBounds(65,135,40,28); but2.setFont(new java.awt.Font("Dialog", 0, 15)); but2.addActionListener(this); Butt.add(but2); Button but3 = new Button("3"); but3.setBounds(115,135,40,28); but3.setFont(new java.awt.Font("Dialog", 0, 15)); but3.addActionListener(this); Butt.add(but3); Button bute = new Button("="); bute.setBounds(165,135,40,68); bute.setFont(new java.awt.Font("Dialog", 0, 25)); bute.addActionListener(this); Butt.add(bute); Button but0 = new Button("0"); but0.setBounds(15,175,90,28); but0.setFont(new java.awt.Font("Dialog", 0, 15)); but0.addActionListener(this); Butt.add(but0); Button butd = new Button("."); butd.setBounds(115,175,40,28); butd.setFont(new java.awt.Font("Dialog", 1, 25)); butd.addActionListener(this); Butt.add(butd); } public void actionPerformed(ActionEvent event) { String sf = event.getActionCommand(); if(sf.equals("9")||sf.equals("8")||sf.equals("7")||sf.equals("6")||sf.equals("5")||sf.equals("4")||sf.equals("3")||sf.equals("2")||sf.equals("1")||sf.equals("0")||sf.equals("C")||sf.equals("←")||sf.equals(".")) { String input = sf; if (start) { txt.setText(""); start = false; } if (input.equals("←")) { String str = txt.getText(); if (str.length() > 0) txt.setText(str.substring(0, str.length() - 1)); } else if (input.equals("C")) { txt.setText("0"); start = true; } else txt.setText(txt.getText() + input); } else { String command = sf; if (start) { cmd = command; } else { calculate(Double.parseDouble(txt.getText())); cmd = command; start = true; } } } public void calculate(double x) { if (cmd.equals("+")) result += x; else if (cmd.equals("-")) result -= x; else if (cmd.equals("*")) result *= x; else if (cmd.equals("/")) result /= x; else if (cmd.equals("=")) result = x; txt.setText("" + result); } public static void main (String[] args) { new MyCalculator(); } }

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧