图形用户界面.docx
- 文档编号:29595789
- 上传时间:2023-07-25
- 格式:DOCX
- 页数:36
- 大小:35.14KB
图形用户界面.docx
《图形用户界面.docx》由会员分享,可在线阅读,更多相关《图形用户界面.docx(36页珍藏版)》请在冰豆网上搜索。
图形用户界面
实验三图形用户界面
1、实验目的
1、掌握常用组件的使用
2、掌握布局管理器的使用
3、理解Java事件处理机制
4、掌握常用事件处理方法
2、实验内容
1、设计一个计算器的面板,要求布置9个数字按钮和基本算术运算的按钮。
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassExam3{
publicExam3(){
JFramef=newJFrame();
ContainercontentPane=f.getContentPane();
contentPane.setLayout(newGridLayout(5,3));
for(inti=1;i<=9;i++)
contentPane.add(newJButton(""+i));
contentPane.add(newJButton("+"));
contentPane.add(newJButton("-"));
contentPane.add(newJButton("*"));
contentPane.add(newJButton("/"));
contentPane.add(newJButton("."));
contentPane.add(newJButton("="));
f.setTitle("网格布局管理器");
f.pack();
f.setVisible(true);
}
publicstaticvoidmain(Stringargs[]){
Exam3b=newExam3();
}
}
2、设计一个图形界面,要求输入产品的说明书,内容包括产品名称、产品规格、产品编号、产品说明。
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassExam4{
publicstaticvoidmain(Stringargs[]){
JFramef=newJFrame("JTextArea例题");
ContainercontentPane=f.getContentPane();
contentPane.setLayout(newBorderLayout());
JPanelp1=newJPanel();
p1.setLayout(newGridBagLayout());
GridBagConstraintsgbc=newGridBagConstraints();
gbc.anchor=GridBagConstraints.WEST;
gbc.insets=newInsets(2,2,2,2);
p1.setBorder(BorderFactory.createTitledBorder("建构一般的JTextArea"));
JLabell1=newJLabel("产品名称:
");
JLabell2=newJLabel("产号编号:
");
JLabell3=newJLabel("产品规格:
");
JLabell4=newJLabel("产品说明:
");
JTextAreat1=newJTextArea(2,8);
JTextAreat2=newJTextArea(10,2);
JTextAreat3=newJTextArea("TravelMate372系列",3,10);
JTextAreat4=newJTextArea("采用因特尔迅驰移动技术,因特尔@奔腾M处理器1.5GHz",5,10);
t1.setText("宏基笔记本电脑");
t2.append("A19345-by3874534-34345");
t4.setLineWrap(true);
gbc.gridy=1;gbc.gridx=0;p1.add(l1,gbc);
gbc.gridx=1;p1.add(t1,gbc);
gbc.gridy=2;gbc.gridx=0;p1.add(l2,gbc);
gbc.gridx=1;p1.add(t2,gbc);
gbc.gridy=3;gbc.gridx=0;p1.add(l3,gbc);
gbc.gridx=1;p1.add(t3,gbc);
gbc.gridy=4;gbc.gridx=0;p1.add(l4,gbc);
gbc.gridx=1;p1.add(t4,gbc);
contentPane.add(p1);
f.pack();
f.show();
f.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
}
}
3、利用面板设计容器结构,在窗口中放置5个标签。
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassExam5{
publicExam5(){
JFramef=newJFrame("JPanelDemo");
ContainercontentPane=f.getContentPane();
contentPane.setLayout(newGridLayout(2,1));
JLabel[]label=newJLabel[5];
for(inti=0;i label[i]=newJLabel("标签"+(i+1),JLabel.CENTER); label[i].setBackground(Color.lightGray); label[i].setBorder(BorderFactory.createEtchedBorder()); label[i].setOpaque(true); } JPanelpanel1=newJPanel(newGridLayout(1,1)); panel1.add(label[0]); JPanelpanel2=newJPanel(newGridLayout(1,2)); JPanelpanel3=newJPanel(newGridLayout(1,2)); panel3.add(label[1]); panel3.add(label[2]); JPanelpanel4=newJPanel(newGridLayout(2,1)); panel4.add(label[3]); panel4.add(label[4]); panel2.add(panel3); panel2.add(panel4); contentPane.add(panel1); contentPane.add(panel2); f.pack(); f.show(); f.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); } }); } publicstaticvoidmain(String[]arg){ newExam5(); } } 4、设计基于AWT的计算器。 importjava.awt.*; importjava.lang.*; importjava.awt.event.*; importjava.applet.Applet; importjava.awt.datatransfer.*; classCalculatorextendsFrameimplements WindowListener,ActionListener,KeyListener{ Stringcommand,copy,arg,chg,txt; doubleresult; Stringnumber="123456789.0"; Stringoperator="/*-+="; CopyPastecp; MenuMenu1; MenuBarMenubar1; MenuItemmenuitem1,menuitem2,menuitem3; TextFieldentrytext; Buttonnumbut[];//Numberbuttons Buttoncombut[];//Commandbuttons Panelcompanel,numpanel; publicstaticvoidmain(String[]arguments){ CalculatorcalWindow=newCalculator("JavaCalculator"); calWindow.setSize(200,250); calWindow.setVisible(true); } publicCalculator(Stringtitle){ super(title); addWindowListener(this); addKeyListener(this); cp=newCopyPaste(); //cp.clip=getToolkit().getSystemClipboard(); setBackground(Color.blue); setLayout(newGridLayout(1,1)); Menubar1=newMenuBar(); Menu1=newMenu("Edit"); menuitem1=newMenuItem("&Copy"); Menu1.add(menuitem1); menuitem1.addActionListener(this); menuitem2=newMenuItem("&Paste"); Menu1.add(menuitem2); menuitem2.addActionListener(this); menuitem3=newMenuItem("&Exit"); Menu1.add(menuitem3); menuitem3.addActionListener(this); Menubar1.add(Menu1); setMenuBar(Menubar1); GridBagLayoutgridbag=newGridBagLayout(); GridBagConstraintsconstraints=newGridBagConstraints(); setLayout(gridbag); constraints.weighty=1; constraints.weightx=1; //constraints.fill=GridBagConstraints.BOTH; FontbigFont=newFont("Courier",Font.BOLD,14); entrytext=newTextField(20); constraints.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(entrytext,constraints); add(entrytext); entrytext.setFont(bigFont); entrytext.setEditable(false); entrytext.setForeground(Color.black); entrytext.setBackground(Color.white); entrytext.addKeyListener(this); entrytext.requestFocus(); constraints.weighty=1; constraints.weightx=1; companel=newPanel(); constraints.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(companel,constraints); companel.setLayout(newGridLayout(1,3,5,5)); //Createthebuttons FontcomsFont=newFont("Arial",Font.BOLD,12); String[]coms={"Back","CE","C"}; combut=newButton[3]; for(inti=0;i<=2;i++){ combut[i]=newButton(coms[i]); companel.add(combut[i]); combut[i].addActionListener(this); combut[i].setFont(comsFont); combut[i].addKeyListener(this); } add(companel); companel.addKeyListener(this); constraints.weighty=4; constraints.weightx=1; numpanel=newPanel(); constraints.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(numpanel,constraints); numpanel.setLayout(newGridLayout(4,5,3,3)); //Createthebuttons String[]nums={"7","8","9","/","sqrt", "4","5","6","*","%", "1","2","3","-","1/x", "0","+/-",".","+","=" }; numbut=newButton[20]; for(inti=0;i<=19;i++){ numbut[i]=newButton(nums[i]); numpanel.add(numbut[i]); numbut[i].addActionListener(this); numbut[i].addKeyListener(this); if(operator.indexOf(nums[i])>-1){ numbut[i].setForeground(Color.red); }else{ numbut[i].setForeground(Color.blue); } } add(numpanel); numpanel.addKeyListener(this); //initializeglobalvariables. command="+"; copy=""; chg="N"; txt=""; arg=""; result=0; } publicvoidactionPerformed(ActionEvente){ if(e.getActionCommand()=="&Exit"){ setVisible(false); System.exit(0); }elseif(e.getActionCommand()=="&Copy"){ Stringtxt=entrytext.getText(); if(txt! =null){ cp.doCopy(txt); } }elseif(e.getActionCommand()=="&Paste"){ cp.doPaste(); if(cp.ctxt! =null){ entrytext.setText(cp.ctxt); } }elseif(e.getActionCommand()=="Back")back_space(); elseif(e.getActionCommand()=="CE")entrytext.setText(""); elseif(e.getActionCommand()=="C"){ result=0; command="+"; entrytext.setText(""); }else{ arg=e.getActionCommand(); txt=entrytext.getText(); if(number.indexOf(arg)>-1){ if(chg=="Y")txt=""; txt=txt+arg; entrytext.setText(txt); chg="N"; }elsecheck_entry(); } } publicvoidkeyPressed(KeyEventk){} publicvoidkeyReleased(KeyEventk){ intikey=k.getKeyCode(); if(ikey==127)entrytext.setText(""); } publicvoidkeyTyped(KeyEventk){ intikey=k.getKeyChar(); if(ikey==8)back_space(); elseif(ikey==10){ arg="="; txt=entrytext.getText(); display_ans(); }else{ txt=entrytext.getText(); charckey=(char)ikey; arg=String.valueOf(ckey); if(number.indexOf(arg)>-1){ if(chg=="Y")txt=""; txt=txt+arg; entrytext.setText(txt); chg="N"; }elsecheck_entry(); } } publicvoidback_space(){ txt=entrytext.getText(); intl=txt.length(); if(l>0){ txt=txt.substring(0,l-1); entrytext.setText(txt); } } publicvoiddisplay_ans(){ entrytext.setText(compute_tot(txt,command)); chg="Y"; command="+"; result=0; } publicvoidcheck_entry(){ if(arg.equals("="))display_ans(); elseif(arg.equals("%")){ entrytext.setText(compute_tot(txt,arg)); chg="Y"; }elseif(operator.indexOf(arg)>-1){ entrytext.setText(compute_tot(txt,command)); command=arg; chg="Y"; }elseif(arg=="sqrt"){ entrytext.setText(compute_tot(txt,arg)); chg="Y"; command=""; }elseif(arg=="+/-"){ Doubletnum=Double.valueOf(txt); doublenum=tnum.doubleValue(); num=num*-1; entrytext.setText(String.valueOf(num)); chg="Y"; }elseif(arg=="1/x"){ entrytext.setText(compute_tot(txt,arg)); chg="Y"; } } Stringcompute_tot(Stringt,Stringc){ Doubletnum=Double.valueOf(t); doublenum=tnum.doubleValue(); if(c.equals("+"))result=result+num; elseif(c.equals("-"))result=result-num; elseif(c.equals("*"))result=result*num; elseif(c.equals("/"))result=result/num; elseif(c.equals("%"))result=num/100; elseif(c.equals("+/-"))result=num*-1; elseif(c.equals("1/x"))result=1.000/num
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 图形 用户界面
![提示](https://static.bdocx.com/images/bang_tan.gif)