Java程序填空_第1页
Java程序填空_第2页
Java程序填空_第3页
Java程序填空_第4页
Java程序填空_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、本题的功能是计算二维数组arr=34,21,45,67,20),23,10,3,45,76,22,3,79,56,50中的最小值,并输出。参考答案:第1处:j<5或j<4第2处:j0第3处:i或i1或ii1本题的功能是通过鼠标确定两个点,然后画两点间的直线。窗口中有个按钮“Draw line”,单击该按钮后,它就处于按下状态,然后用鼠标在窗口中单击下,在单击的地方就会出现个坐标圆点,用鼠标在另外个地方单击下又会出现另外个圆点,并且此时在两个坐标圆点间画出条直线,且“Draw line”处于可用状态,再单击这个按钮就可以画另外条直线。 · 参考答案:第1处:Poi

2、nt第2处:mevtgetPoint下面的程序中,有两个文本框,Input和Output,用户在Input文本框中输入字符串后回车,字符串会在Output文本框中出现。· 参考答案:第1处;new Actionlistener()第2处:tflgetText()本题中定义了长度为20的维整型数组a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。public class javalpublic static void main(Stringargs)int a=Int i:forai=ifori+)=1:i=0;i<20;i+)Systemoutprint

3、(”a”+i+”=”+ai+”,“);· 参考答案:第1处:new int020第2处:i0;i<20第3处:i%2 !0本题的功能是计算二维数组arr=34,21,45,67,20,23,10,3,45,76,22,3,79,56,50中的最小值,并输出。 public class javalpublic static void main(Stringargs)int arr=34,21,45,67,20),23,10,3,45, 76,22,3,79,56,50; int i=0: intj=0; int min=arr00

4、; while(i<3)while(      )if(arrij<min)min=arrij; j+;       ;       ; Systemoutprintln("The min:"+min); · 参考答案:第1处:j<5或j<=4第2处:j=0第3处:i+或i+=1或i=i+1.本题将数组中arr=5,6,3,7,9,1

5、的各个元素按下标的逆序输出。 public class javalpublic static void main(Stringargs)int arr=5,6,3,7,9,1;          ; n=         ; while(n>=O)SystemOUtprint(arrn+"");     

6、;     ; · 参考答案:第1处:int n第2处:arr1ength-1第3处:n-或n=n-1或n-=l.本题定义了一个方法add(),用于求两个整形数的和。方法中有两个整形参数a和b,方法体中计算a和b的和sum,并将结果返回。程序中调用add()方法求整数24和34的和,并将结果打印输出。 public class javalpublic static void main(Stringargs)int a=24,b=34; Systemoutprintln(add(a,b); pub

7、lic static int add(         )         ; suma+b;          ;  · 参考答案:第1处:int a,int b第2处:int sum第3处:return sum本题将一个整形变量opl的二进制的低3位全部变成1。 public

8、 class javalpublic static void main(Stringargs)          opl=21; int op2=7:           res;           ; Systemoutprintln(res); 

9、; · 参考答案:第1处:int 第2处:int 第3处:res=oplop2本题的功能是计算l10之间除了5以外的各个自然数的和。 public class javalpublic static void main(Stringargs)int i=1 ; int sum=0; while(i<=10)if(i= =5)       ;        ; &#

10、160;      ; i+; Systemoutprintln("sum="+sum); · 参考答案:第1处:i=i+1或i+或i+=1第2处:continue第3处:sum+=i或sum=sum+i本题的功能是获取鼠标在窗口中的位置。当鼠标移进窗口中,就会实时显示鼠标在窗口中的相对位置,比如显示为"鼠标的当前位置:X:Y"(其中,X为横坐标,Y为纵坐标)。 import javaawt*; import javaawtevent*

11、; import javautil*; import javaxswing*; public class java2public static void main(Stringargs)MouseFrame frame=new MouseFrame(); framesetDefaultCloseoperation(JFrameEXIT_0N CLOSE); frameshow(); class MouseFrame extends JFramepublic MouseFrame()setTitle("java2")

12、; setSize(WIDTH,HEIGHT); MousePanel panel=new MousePanel(); Container contentPane=getContentPane(); contentPaneadd(panel); public static final int WIDTH = 300; public static final int HEIGHT=200;  class MousePanel extends JPanelpublic MousePanel()addMouseListe

13、ner(new MouseHandler(); addMouseMotionListener(new MouseMotionHan-dler(); public void paintComponent(Graphics g)(superpaintComponent(g); String text="鼠标指针位置:"+mousex+":"+mousey; gdrawString(text,10,10); private int mousex,mousey; private class MouseM

14、otionHandler      public void mouseMoved(MouseEvent event)mousex=eventgetX(); mousey=eventgetY(); repaint(); public void mouseDragged(MouseEvent event)mousex=eventgetX(); mousey=eventgetY(); repaint(); private class MouseHandler  

15、   public void mousePressed(MouseEvent eveat)mousex=eventgetX(); mousey=eventgetY(); · 参考答案:第1处:implements MouseMotionListener第2处:extends MouseAdapter本题主窗口中包括一个文本框和一个文本域,在上面的文本框中输入一个整数并按回车键,就会在下面的文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。 tmport javaawtevent*: 

16、import javaawt*; import javaxswingJOptionPane; class Dwindow extends Frame implements ActionLis-tenerTextField inputNumber; TextArea show; Dwindow(String s)super(s): inputNumber=new TextField(22); inputNumberaddActionListener(this); show=new TextArea(); add(in

17、putNumber,BorderLayoutNORTH); add(show,BorderLayoutCENTER); setBounds(60,60,300,300);setVisible(true); validate(); addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0); ); public void actionPerformed(ActionEvent e)boolean boo=false;

18、 if(egetSource()= =inputNumber)String s=        ; char a=StoCharArray(); for(int i=0;i<alength;i+)if(!(CharacterisDigit(ai)boo=true; if(boo= =true)JOptionPaneshowMessageDialog(this,"您输入 了非法字符","警告对话框",  

19、;       ); inputNumbersetText(null); else if(boo= =false)int number=IntegerparseInt(s); showappend("n"+number+"平方:"+(num-bet*number): public class java2public static void main(String args) new Dwindow("java2"); 

20、;· 参考答案:第1处:inputNumbergetText()第2处:JOptionPaneWARNING_MESSAGE本题是一个Applet,它的功能是在窗口上添加12×12个标签,并且横向和纵向标签的颜色为黑白相间。 import javaapplet*; import javaawt*; import java,awtevent*; pubhc class java2extends AppletGridLayout grid; pubhc void init()grid=new GridLayout(12,12);

21、 setLayout(grid); Label         =new Label1212; for(int i=0;i<12;i+)for(int j=0;j<12;j+)labelij=        ; if(i+j)2= =0)labelijsetBackground(Colorblack); elselabelijsetBackground(colorw

22、hite); add(labelij);  · 参考答案:第1处:label第2处:new label()本题中定义了一个树型的通信录,窗El左侧是一个树, 右侧是一个文本域,单击树的结点,则在右侧文本域中显示 相关信息,如果单击的是树结点,则显示对应名字的电话 信息。 import javaxswing*; import javaxswingtree*; import javaawt*; import javaawtevent*; import javaxswingevent*;&#

23、160;class Mytree2 extends JFrame      JTree tree=null;JTextArea text=new JTextArea(20,20); Mytree2()Container con=getContentPane(); DefauhMutableTreeNode root=new Default-MutableTreeNode("同学通信录"); DefaultMutableTreeNode tl=new Default-MutableTre

24、eNode("大学同学"); DefaultMutableTreeNode t2=new Default-MutableTreeNode("研究生同学"); DefaultMutableTreeNode tl l=new Default-MutatleTreeNode("陈艳"); DefaultMutableTreeNode tl 2=new Default-MutableTreeNode("李小永"); DefaultMutableTreeNode t

25、2 1=new Defauh-MutableTreeNode("王小小"); DefauhMutableTreeNode t2 2=new Defauh-MutableTreeNode("董小"); setTitle("java2"); rootadd(t1);rootadd(t2); tladd(t1_1);tladd(t1_2);t2add(t2_1);t2 add(t2_2); tree=new JTree(root); JSerollPa

26、ne scrollpane=new JScrollPane(text); JSplitPane splitpane=new JSplitPane(JSplitPane HORIZONTAL SPLIT, true,tree,scrollpane); treeaddTreeSeleetionListener(this); conadd(splitpane); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)Systemexit(0

27、);); setVisible(true);setBounds(70,80,200,300); public void valueChanged(TreeSelectionEvent e)if(egetSouree()= =tree)DefauhMutableTreeNode node=(DefaultMutableTreeNode)treegetLastSelected-PathComponent(); if(nodeisLeaf()(String str=      ; if(strequa

28、ls("陈艳")(textsetText(str+":联系电话quot;);else if(strequals("李小永") textsetText(str+":联系电话quot;); else if(strequals("王小小")textsetText(str+":联系电话quot;);)else if(strequals("董小")textsetText(str+"

29、:联系电话quot;);elsetextsetText(nodegetUserObject()toString();  public class java2public static void main(String args)Mytree2 win=new Mytree2();winpack();· 参考答案:第1处:implements TreeSelectionListener第2处:nodetoString() 本题中,主窗口有一个按钮“打开对话框”和一个文本域,单击按钮“打开对话框”后会弹出一个对话框,对话框上有两个

30、按钮“Yes"和“N0”,单击对话框上的“Yes”和“N0”按 钮后返回主窗口,并在右侧文本域中显示刚才所单击的按钮信息。 import javaawtevent*; import javaawt*; class MyDialog        implements ActionListenerstatic final int YES=1,N0=0; int message=-1;Button yes,no; MyDialog(Frame fString

31、S,boolean b)super(f,S,b); ves=new Button("Yes");yesaddActionListener(this); no=new Button("No"); noaddActionListener(this)osetLayout(new FlowLayout(); add(yes);add(no); setBounds(60,60,100,100); addWindowListener(new WindowAdapter() public void windo

32、wClosing(WindowEvent e)message=-1;setVisible(false);); public void actionPerformed(ActionEvent e)if(egetSource()= =yes)message=YES; setVisible(false); else if(egetSource()= = no)message=NO; setVisible(false); public int getMessage()return message; class Dwindow extends

33、Frame implements ActionLis-tenerTextArea text;Button button;MyDialog dialog; Dwindow(String s)super(s); text=new TextArea(5,22);button=new Button("打开对话框"); buttonaddActionListener(this); setLayout(new FlowLayout(); add(button);add(text); dialog=new MyDialog(th

34、is,"Dialog",true); setBounds(60,60,300,300);setVisible(true); validate(); addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0);); public void actionPerformed(ActionEvent e)if(egetSource()= =button)      

35、;   ; if(dialoggetMessage()= =MyDialogYES)textappend("n你单击了对话框的yes按 钮"); else if(dialoggetMessage()= =MyDialogNO)textappend("n你单击了对话框的N0按 钮"); public class java2public static void main(String args)new Dwindow("java2"); ·

36、 参考答案:第1处:extends Dialog第2处:dialogsetVisible(true)题中,在下画线上填写代码,指定变量b为字节型,变量f为单精度实型,变量1为64位整型。 public class javalpublic static void main(String-args)        b=49;         f=89f;     

37、0;   l=0xfedl; Systemoutprintln("b="+b); Systemoutprintln("f="+f): SystemOUtprintln("l="+1); · 参考答案:第1处:byte第2处:float第3处:long本题的功能是读人运行程序时所传入的参数(一个或多个),并将参数依次显示出来,比如运行程序:java javalpartl part2,则打印输出为:partl part2。 public class

38、javalpublic static void main(Stringargs)int i=0: while(         )SystemOUtprint(        +"");         ; Systemoutprintln(); · 参考答案:第1处:i<

39、argslength  第2处:argsi第3处:i+本题的功能是统计成绩不及格的人数,分数有89,90, 56,90,89,45。23,45,60,59,61。 public class javalpublic static void main(Stringargs)(int 3score=56。90。89,23,45,61,60,59; int hum=0;         ; int i=0: while(   

40、;     )if(        )sum+: i+; Systemoutprintln(”<60:”+sum); · 参考答案:第1处:int sum=0第2处:i<scorelength第3处:scorei<60或scorei<=59本题的功能是用冒泡法对数组元素arr=30,1,-9,70)进行从小到大排列。冒泡法排序是比较相邻的两个元素的大小,然后把小的元素交换到前面。 public

41、 class javalpublic static void main(Stringargs)int i,j; int arr=30,1,-9,70); int n=         ; for(i=0;i<n-1;i+)for(j=i+1;j<n;j+)if(arri>arrj)int temp=arri;          ;  

42、60;       ; for(i=0;i<n;i+)Systemoutprint(arri+""); · 参考答案:第1处:arrlength第2处:arr=arrj 第3处:arrj=temp 本题的功能是对下拉菜单项的操作,包括添加和删除。页面包括一个下拉菜单、一个文本框和两个按钮“删除”和“添加”,选中下拉菜单的一项后,可以通过“删除”按钮从下拉菜单中删除该项,在文本框中填入字符串后,单击“添加”按钮就可以将该项添加到下拉菜单中,所有信息都将

43、显示在右侧的文本域中。 import javaawt*; import javaawtevent*; public class java2 extends javaappletApplet imple-ments hemListener,ActionListenerChoice choice; TextField text; TextArea area; Button add,del; public void init()  choice:new Choice(); text=new TextF

44、ield(8); area:new TextArea(6,15); choiceadd("音乐天地"); choiceadd("武术天地"); choiceadd("象棋乐园"); choiceadd("交友聊天"); add=new Button("添加"); del=new Button("删除"); addaddActionListener(this); deladdActionL

45、istener(this); choiceaddItemListener(this); add(choice); add(del);add(text);add(add);add(area); public void itemStateChanged(hemEvent e)String name=        ; int index=choicegetSelectedIndex(); areasetText("n"+index+&quo

46、t;:"+name); public void actionPerformed(ActionEvent e)if(egetSource()= =addegetSource()= =text)String name=textgetText(); if(namelength()>0)choiceadd(name); choiceselect(name); areaappend("n添加"+name); else if(egetSource()= =del)choiceremove(  

47、0;     ); areaappend("n删除"+choicegetSelectedItem(); · 参考答案:第1处:choicegetSelectedltem()第2处:choicegetSelectedIndex()本题使用下拉菜单来控制字体,窗口中有一个标签和一个下拉菜单,当选中下拉菜单中的任一项字体时,标签上字符串的字体就随之改变。 import javaawt*; import javaawtevent*; import javaxswing*;&

48、#160;class ComboBoxFrame extends JFrame          public ComboBoxFrame()setTitle("java2"); setSize(300,200); addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0); ); style=new JComboBox():

49、 stylesetEditable(true); styleaddhem("Serif"); styleaddItem("SansSerif"); styleaddhem("Monospaced"); styleaddhem("Dialog"); styleaddhem("Dialoglnput"); styleaddActionListener(this); JPanel p=new JPanel(); 

50、Padd(style); getContentPane()add(p,"South"); panel=new ComboBoxTestPanel(); getContentPane()add(panel,"Center"); public void actionPerformed(ActionEvent evt)JComboBox source=(JComboBox)          ; String i

51、tem=(String)sourcegetSelectedhem(): panelsetStyle(item); private ComboBoxTestPanel panel; private JComboBox style; class ComboBoxTestPanel extends JPanelpublic ComboBoxTestPanel()setStyle("Serif"); public void setStyle(String s)setFont(new Font(S,FontPLAIN,12);

52、0;repaint(); public void paintComponent(Graphics g)superpaintComponent(g); 9drawString("Welcome to China!",0,50);  public class java2public static void main(Stringargs)JFrame frame=new ComboBoxFrame(); frameshow(); · 参考答案:第1处:implements ActionListener第2处:

53、evtgetSource()本题中,生成一个窗口,该窗口的长、宽为屏幕长、宽的一半,并且窗口的大小不能改变。 import javaawt*; import javaxswing*; public class java2publicstatic void main(Stringargs)FrameSize frame=new FrameSize(); framesetDefaultCloseoperation(JFrameEXITON_CLOSE); frameshow(); class FrameSize extends JFram

54、epublic FrameSize()setTitle("java2"); Toolkit tk=ToolkitgetDefaultToolkit(); Dimension screenSize=          ; int screenHeight=screenSizeheight; int screenWidth=screenSizewidth; setSize(screenWidth2,sereenHeight2);&

55、#160;         ; · 参考答案:第1处:tkgetScreenSize() 第2处:setResizable(false) 本题中定义了一个简单的计算器,可以进行基本的四则运算。程序中包含16个按钮用来表示09、+、-、 *、一运算符和小数点,程序顶部的文本框用来显示操作数以及结果。 import javaawt*; import javaawtevent*; import javaxswing*; public

56、 class java2public static void main(Stringargs)tryUIManagersetLookAndFeel(UIManagergetSys-temLookAndFeelClassName(); catch(Exception e)JFrame frame=new CalculatorFrame(); frameshow();  class CalculatorPanel extends JPanel implements Ac-tionListenerprivate JTextField display; 

57、;private JButton btn; private double arg=0; private String op="="; private boolean start=true; public CalculatorPanel()setLayout(new BorderLayout(); display=new JTextField("0"); displaysetEditable(false); add(display,"North"); JPa

58、nel P=new JPanel(); PsetLayout(new GridLayout(4,4); String buttons="789456*123-0=+": for(int i=0;i<buttonslength();i+)btn=new JButton(buttonssubstring(i,i+1); Padd(btn);         ; add(P,"Center"); publi

59、c void actionPerformed(ActionEvent evt)String s=evtgetActionCommand(); if('0 '<=scharAt(0)scharAt(O)<='9' sequals("-")if(start)displaysetText(s): else displaysetText(displaygetText()+s); start=false; elseif(start)if(sequals("-")displayset

60、Text(s): start=false; else op=S; else(double x=         ; calculate(x); op=S: start=true; public void calculate(double n)if(opequals("+")arg+=n: else if(opequals("-")arg-=n; else if(opequals

61、("*")arg*=n; else if(opequals("")arg=n; else if(opequals("=")arg=n; displaysetText(""+arg); class CalculatorFrame extends JFramepublic CalculatorFrame()setTitle("java2"); setSize(220,180); addWindowListener(new WindowAda

62、pter()public void windowClosing(WindowEvent e)Systemexit(0); ); Container contentPane=getContentPane(); contentPaneadd(new CalculatorPanel(); · 参考答案:第1处:btnaddActionListener(this)第2处:DoubleparseDouble(displaygetText()本题是个表格式的成绩单,其中包括“姓名”、“英语成绩”、“数学成绩”和“总成绩”,姓名和成绩都可以进行修改,单击按钮

63、“计算每人总成绩”,则可以统计出每个人的总成绩并显示在总成绩栏中。· 参考答案:第1处:new JTable(a,name)第2处:aijtoString()本题中定义了个树型的通信录,窗口左侧是个树,右侧是个文本域,单击树的结点,则在右侧文本域中显示相关信息,如果单击的是树结点,则显示对应名字的电话信息。import javaxswing*;import j avaxswingtree*;import javaawt*;import javaawtevent*;import javaxswingevent*;class Mytree2 extends JFrameJTree tree=null;JTextArea text=new JTexLAyea(20,20);Mytree2()Container con=getContentPane();DefaultMutableTree

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论