西安拾光婚礼工作室
2024-11-30 10:56:26
最佳回答
1目标:初始化窗体代码:如下----------------public class view extends jframe{public view(){th**.settitle("计算器");//设置标题th**.setresizable(false);//不可改变大小th**.setlocationrelativeto(**);//居中显示th**.setsize(228, 324);//窗体大小,可以使用faststone capture的屏幕尺量一下win计算器的大小th**.setdefaultcloseoperation(jframe.exit_on_close);//默认关闭操作}public static void main(string[] args) {new view().setv**ible(true);}}2目标:设置菜单代码:如下----------------/*** 创建初始化菜单方法(别忘记在构造函数里调用)*/private void initmenu(){jmb = new jmenubar();//创建菜单条jm_view = new jmenu("查看(v)");//查看菜单jm_view.setmnemonic('v');//设置热键jm_edit = new jmenu("编辑(e)");//编辑菜单jm_edit.setmnemonic('e');jm_help = new jmenu("帮助(h)");//帮助菜单jm_help.setmnemonic('h');jmb.add(jm_view);jmb.add(jm_edit);jmb.add(jm_help);th**.setjmenubar(jmb);//将菜单条加入窗体中}3目标:仿结果显示框代码:如下----------------/*** 创建内容区域初始化方法*/private void initcontent(){//整个窗口区域jpanel jp = new jpanel();jp.setlayout(**);//设置空布局以便绝对定位//显示框区域jpanel jptmp = new jpanel(){//获取背景图片,使用截图工具从win计算器显示框中截取2像素宽度作为背景image bg = new imageicon("imgs/bg.png").getimage();/** * 填充结果显示区域的背景 */@overrideprotected void paintcomponent(graphics g) {super.paintcomponent(g);//x轴平铺满面板for(int i = 0;i<th**.getwidth();i+=bg.getwidth(th**)){g.drawimage(bg, i, 0, th**);//绘制背景图片}}};//设置显示框上方小字体font font_lab = new font("arial", font.bold, 22);jla_input = new jlabel();jla_input.settext("0");//设置字体右对齐jla_input.sethorizontalalignment(jlabel.right);jla_input.setfont(font_lab);//设置显示框中计算结果的字体font_lab = new font("arial", font.bold, 10);jla_show = new jlabel();jla_show.settext("sqrt");//设置字体右对齐jla_show.sethorizontalalignment(jlabel.right);jla_show.setfont(font_lab);//设置显示框区域的位置的大小jptmp.setbounds(14, 15, 190, 50);jptmp.setlayout(new borderlayout());jptmp.add(jla_show,borderlayout.north);jptmp.add(jla_input,borderlayout.center);//设置边框jptmp.setborder(borderfactory.createlineborder(new color(142,156,173)));jp.add(jptmp);th**.setlayout(new borderlayout());th**.add(jp,borderlayout.center);}4目标:绘制其他按钮代码:如下----------------①设置字符枚举类public enum operator{//用于swith判断,待用mc,mr,ms,madd,msubtract,//mc,mr,ms,m+,m-clback,clerror,clclear,opernegation,opersqrt,//<-,ce,c,-/+,开平方n7,n8,n9,operdivide,operper,// /,%,n4,n5,n6,opermultiply,operreciproc,//*,倒数n1,n2,n3,opersubtract,operequ,//-,=n0,operdot,operadd;//+//用于显示public static final string [] labs = new string[]{"mc","mr","ms","m+","m-","←","ce","c","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+"};}②添加按钮jptmp = new jpanel();jptmp.setbounds(14, 72, 196,194);jptmp.setlayout(**);font font_but = new font("arial", font.bold, 10);//创建按钮字体for (int i = 0; i < **_oper.length; i++) {**_oper[i] = new **utton(operator.labs[i]);//设置按钮文字**_oper[i].setforeground(new color(30,57,91));//设置字体颜色**_oper[i].setactioncommand(operator.values()[i].tostring());//设置按钮标示**_oper[i].setfont(font_but);//设置按钮字体**_oper[i].setbounds(i%5*(33+6), i/5*(24+6), 33, 24);//统一33宽,24高,间距6,每5个换一行**_oper[i].setborder(new lineborder(new color(135,151,170), 1));//设置按钮边框**_oper[i].addactionl**tener(th**);//添加**jptmp.add(**_oper[i]);}**_oper[24].setbounds(24%5*(33+6), 24/5*(24+6), 33, 24*2+6);//调整等号按钮位置**_oper[25].setbounds(25%5*(33+6), 25/5*(24+6), 33*2+6, 24);//调整0按钮的位置**_oper[26].setbounds(27%5*(33+6), 26/5*(24+6), 33, 24);//调整.按钮的位置**_oper[27].setbounds(28%5*(33+6), 27/5*(24+6), 33, 24);//调整较好按钮的位置jp.add(jptmp);5完整代码-----package com.lfd.view;import j**a.awt.borderlayout;import j**a.awt.color;import j**a.awt.font;import j**a.awt.graphics;import j**a.awt.image;import j**a.awt.event.actionevent;import j**a.awt.event.actionl**tener;import j**ax.swing.borderfactory;import j**ax.swing.imageicon;import j**ax.swing.**utton;import j**ax.swing.jframe;import j**ax.swing.jlabel;import j**ax.swing.jmenu;import j**ax.swing.jmenubar;import j**ax.swing.jpanel;import j**ax.swing.border.lineborder;import com.lfd.common.operator;public class view extends jframe implements actionl**tener{private jmenubar jmb;private jmenu jm_view,jm_edit,jm_help;private jlabel jla_input,jla_show;private **utton[] **_oper = new **utton[28];public view(){initmenu();initcontent();th**.settitle("计算器");//设置标题th**.setresizable(false);//不可改变大小th**.setlocationrelativeto(**);//居中显示th**.setsize(228, 324);//窗体大小,可以使用faststone capture的屏幕尺量一下win计算器的大小th**.setdefaultcloseoperation(jframe.exit_on_close);//默认关闭操作}/*** 初始化菜单方法*/private void initmenu(){jmb = new jmenubar();//创建菜单条jm_view = new jmenu("查看(v)");//查看菜单jm_view.setmnemonic('v');//设置热键jm_edit = new jmenu("编辑(e)");//编辑菜单jm_edit.setmnemonic('e');jm_help = new jmenu("帮助(h)");//帮助菜单jm_help.setmnemonic('h');jmb.add(jm_view);jmb.add(jm_edit);jmb.add(jm_help);th**.setjmenubar(jmb);//将菜单条加入窗体中}/*** 创建内容区域初始化方法*/private void initcontent(){//整个窗口区域jpanel jp = new jpanel();jp.setlayout(**);//显示框区域jpanel jptmp = new jpanel(){//获取背景图片,使用截图工具从win计算器显示框中截取2像素宽度作为背景image bg = new imageicon("imgs/bg.png").getimage();/*** 填充结果显示区域的背景*/@overrideprotected void paintcomponent(graphics g) {super.paintcomponent(g);//x轴平铺满面板for(int i = 0;i<th**.getwidth();i+=bg.getwidth(th**)){g.drawimage(bg, i, 0, th**);//绘制背景图片}}};//设置显示框上方小字体font font_lab = new font("arial", font.bold, 22);jla_input = new jlabel();jla_input.settext("0");//设置字体右对齐jla_input.sethorizontalalignment(jlabel.right);jla_input.setfont(font_lab);//设置显示框中计算结果的字体font_lab = new font("arial", font.bold, 10);jla_show = new jlabel();jla_show.settext("sqrt");//设置字体右对齐jla_show.sethorizontalalignment(jlabel.right);jla_show.setfont(font_lab);//设置显示框区域的位置的大小jptmp.setbounds(14, 15, 190, 50);jptmp.setlayout(new borderlayout());jptmp.add(jla_show,borderlayout.north);jptmp.add(jla_input,borderlayout.center);//设置边框jptmp.setborder(borderfactory.createlineborder(new color(142,156,173)));jp.add(jptmp);jptmp = new jpanel();jptmp.setbounds(14, 72, 196,194);jptmp.setlayout(**);font font_but = new font("arial", font.bold, 10);//创建按钮字体for (int i = 0; i < **_oper.length; i++) {**_oper[i] = new **utton(operator.labs[i]);//设置按钮文字**_oper[i].setforeground(new color(30,57,91));//设置字体颜色**_oper[i].setactioncommand(operator.values()[i].tostring());//设置按钮标示**_oper[i].setfont(font_but);//设置按钮字体**_oper[i].setbounds(i%5*(33+6), i/5*(24+6), 33, 24);//统一33宽,24高,间距6,每5个换一行**_oper[i].setborder(new lineborder(new color(135,151,170), 1));//设置按钮边框**_oper[i].addactionl**tener(th**);//添加**jptmp.add(**_oper[i]);}**_oper[24].setbounds(24%5*(33+6), 24/5*(24+6), 33, 24*2+6);//调整等号按钮位置**_oper[25].setbounds(25%5*(33+6), 25/5*(24+6), 33*2+6, 24);//调整0按钮的位置**_oper[26].setbounds(27%5*(33+6), 26/5*(24+6), 33, 24);//调整.按钮的位置**_oper[27].setbounds(28%5*(33+6), 27/5*(24+6), 33, 24);//调整较好按钮的位置jp.add(jptmp);th**.setlayout(new borderlayout());th**.add(jp,borderlayout.center);}public static void main(string[] args) {new view().setv**ible(true);}@overridepublic void actionperformed(actionevent e) {//todo 实现逻辑操作**utton ** = (**utton) e.getsource(); string commond = **.getactioncommand();}}-----package com.lfd.common;public enum operator{mc,mr,ms,madd,msubtract,//mc,mr,ms,m+,m-clback,clerror,clclear,opernegation,opersqrt,//<-,ce,c,-/+,开平方n7,n8,n9,operdivide,operper,// /,%,n4,n5,n6,opermultiply,operreciproc,//*,倒数n1,n2,n3,opersubtract,operequ,//-,=n0,operdot,operadd;//+public static final string [] labs = new string[]{"mc","mr","ms","m+","m-","←","ce","c","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+"};}end 20210311