您好,欢迎访问三七文档
第七章Java图形用户界面1、阅读下面的程序,回答问题。importjava.awt.*;importjavax.swing.*;publicclassTextendsJFrame{publicT(){super(GridLayout);Containercon=this.getContentPane();con.setLayout(newGridLayout(2,3));con.add(newJButton(a));con.add(newJButton(b));con.add(newJButton(c));con.add(newJButton(d));con.add(newJButton(e));con.add(newJButton(f));setSize(200,80);setVisible(true);}publicstaticvoidmain(Stringargs[]){newT();}}画图表示程序运行后的图形界面。如果程序通过实现某个接口处理按钮的动作事件,则该接口名为何?接口中的方法头声明如何?实现的接口是ActionListener该接口的方法声明是publicvoidactionPerformed(ActionEvente)2、编写一个简单的计算器,要求图形用户界面如下图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassTestZextendsJFrameimplementsActionListener{privateJPaneljPanel1,jPanel2;privateJTextFieldresultField;privateJButtons1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,b5,b6,b7,b8,f1,f2;privatebooleanend,add,sub,mul,div;privateStringstr;privatedoublenum1,num2;publicTestZ(){super(计算器);setSize(300,240);Containercon=getContentPane();con.setLayout(newBorderLayout());jPanel1=newJPanel();jPanel1.setLayout(newGridLayout(1,1));jPanel2=newJPanel();jPanel2.setLayout(newGridLayout(4,4));resultField=newJTextField(0);jPanel1.add(resultField);con.add(jPanel1,BorderLayout.NORTH);s1=newJButton(1);s1.addActionListener(this);s2=newJButton(2);s2.addActionListener(this);s3=newJButton(3);s3.addActionListener(this);s4=newJButton(4);s4.addActionListener(this);s5=newJButton(5);s5.addActionListener(this);s6=newJButton(6);s6.addActionListener(this);s7=newJButton(7);s7.addActionListener(this);s8=newJButton(8);s8.addActionListener(this);s9=newJButton(9);s9.addActionListener(this);s0=newJButton(0);s0.addActionListener(this);b1=newJButton(+);b1.addActionListener(this);b2=newJButton(-);b2.addActionListener(this);b3=newJButton(*);b3.addActionListener(this);b4=newJButton(/);b4.addActionListener(this);f1=newJButton(.);f1.addActionListener(this);f2=newJButton(=);f2.addActionListener(this);jPanel2.add(s1);jPanel2.add(s2);jPanel2.add(s3);jPanel2.add(b1);jPanel2.add(s4);jPanel2.add(s5);jPanel2.add(s6);jPanel2.add(b2);jPanel2.add(s7);jPanel2.add(s8);jPanel2.add(s9);jPanel2.add(b3);jPanel2.add(s0);jPanel2.add(f1);jPanel2.add(f2);jPanel2.add(b4);con.add(jPanel2,BorderLayout.CENTER);}publicvoidnum(inti){Strings=null;s=String.valueOf(i);if(end){//如果数字输入结束,则将文本框置零,重新输入resultField.setText(0);end=false;}if((resultField.getText()).equals(0)){//如果文本框的内容为零,则覆盖文本框的内容resultField.setText(s);}else{//如果文本框的内容不为零,则在内容后面添加数字str=resultField.getText()+s;resultField.setText(str);}}publicvoidactionPerformed(ActionEvente){//数字事件if(e.getSource()==s1)num(1);elseif(e.getSource()==s2)num(2);elseif(e.getSource()==s3)num(3);elseif(e.getSource()==s4)num(4);elseif(e.getSource()==s5)num(5);elseif(e.getSource()==s6)num(6);elseif(e.getSource()==s7)num(7);elseif(e.getSource()==s8)num(8);elseif(e.getSource()==s9)num(9);elseif(e.getSource()==s0)num(0);//符号事件elseif(e.getSource()==b1)sign(1);elseif(e.getSource()==b2)sign(2);elseif(e.getSource()==b3)sign(3);elseif(e.getSource()==b4)sign(4);//等号elseif(e.getSource()==f1){str=resultField.getText();if(str.indexOf(.)=1){str+=.;resultField.setText(str);}}elseif(e.getSource()==f2){num2=Double.parseDouble(resultField.getText());if(add){num1=num1+num2;}elseif(sub){num1=num1-num2;}elseif(mul){num1=num1*num2;}elseif(div){num1=num1/num2;}resultField.setText(String.valueOf(num1));end=true;}}publicvoidsign(ints){if(s==1){add=true;sub=false;mul=false;div=false;}elseif(s==2){add=false;sub=true;mul=false;div=false;}elseif(s==3){add=false;sub=false;mul=true;div=false;}elseif(s==4){add=false;sub=false;mul=false;div=true;}num1=Double.parseDouble(resultField.getText());end=true;}publicstaticvoidmain(String[]args){TestZth1=newTestZ();th1.show();}}3、请编写一个简单的用户登录程序。要求使用图形用户界面,用户名和密码假定均为java,且密码输入时全部显示为星号(*)。importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.*;publicclassJavaApplication49{publicstaticvoidmain(String[]args){MyLandmy=newMyLand();}}classMyLand{inta=0;publicMyLand(){JFrameframe=newJFrame();frame.setBounds(450,300,370,220);Fontfont=newFont(宋体,Font.PLAIN,30);frame.setFont(font);frame.setLayout(newBorderLayout());JPanelpaneltop=newJPanel();paneltop.add(newJLabel(用户登陆,JLabel.CENTER));frame.add(paneltop,BorderLayout.NORTH);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanelpanel_1=newJPanel(newGridLayout(3,1));frame.add(panel_1,BorderLayout.CENTER);JPanelpanel_11=newJPanel(newFlowLayout());JPanelpanel_12=newJPanel(newFlowLayout());JPanelpanel_13=newJPanel(newGridLayout(1,5));panel_1.add(panel_11);panel_1.add(panel_12);panel_1.add(panel_13);panel_11.add(newJLabel(用户名:));finalJTextFieldtext1=newJTextField(,10);panel_11.add(text1);panel_12.add(newJLabel(用户密码:));finalJPasswordFieldtext2=newJPasswordField(,10);text2.setEchoChar('*');panel_12.add(text2);JButtonb=newJButton(确定);classMyExam7_3implementsActionListener{publicvoidactionPerformed(ActionEvente){JDialogdialog=newJDialog();dialog.setBounds(450,200,560,400);dialog.setVisible(true);Fontfont1=newFont(宋体,Font.BOL
本文标题:java-作业七
链接地址:https://www.777doc.com/doc-4882469 .html