Java程序填空.doc_第1页
Java程序填空.doc_第2页
Java程序填空.doc_第3页
Java程序填空.doc_第4页
Java程序填空.doc_第5页
免费预览已结束,剩余29页可下载查看

下载本文档

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

文档简介

本题的功能是计算二维数组arr=34,21,45,67,20),23,10,3,45,76,22,3,79,56,50中的最小值,并输出。参考答案:第1处:j5或j4第2处:j0第3处:i或i1或ii1本题的功能是通过鼠标确定两个点,然后画两点间的直线。窗口中有个按钮“Draw line”,单击该按钮后,它就处于按下状态,然后用鼠标在窗口中单击下,在单击的地方就会出现个坐标圆点,用鼠标在另外个地方单击下又会出现另外个圆点,并且此时在两个坐标圆点间画出条直线,且“Draw line”处于可用状态,再单击这个按钮就可以画另外条直线。 参考答案:第1处:Point第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;i20;i+)Systemoutprint(”a”+i+”=”+ai+”,“); 参考答案:第1处:new int020第2处:i0;i20第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;while(i3)while( )if(arrijmin)min=arrij;j+; ; ;Systemoutprintln(The min:+min); 参考答案:第1处:j5或j=O)SystemOUtprint(arrn+); 参考答案:第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);public static int add( );suma+b; 参考答案:第1处:int a,int b第2处:int sum第3处:return sum本题将一个整形变量opl的二进制的低3位全部变成1。public class javalpublic static void main(Stringargs)opl=21;int op2=7:res;Systemoutprintln(res); 参考答案:第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);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*;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);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()addMouseListener(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 MouseMotionHandlerpublic void mouseMoved(MouseEvent event)mousex=eventgetX();mousey=eventgetY();repaint();public void mouseDragged(MouseEvent event)mousex=eventgetX();mousey=eventgetY();repaint();private class MouseHandlerpublic void mousePressed(MouseEvent eveat)mousex=eventgetX();mousey=eventgetY(); 参考答案:第1处:implements MouseMotionListener第2处:extends MouseAdapter本题主窗口中包括一个文本框和一个文本域,在上面的文本框中输入一个整数并按回车键,就会在下面的文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。tmport javaawtevent*: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(inputNumber,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;if(egetSource()= =inputNumber)String s=;char a=StoCharArray();for(int i=0;ialength;i+)if(!(CharacterisDigit(ai)boo=true;if(boo= =true)JOptionPaneshowMessageDialog(this,您输入了非法字符,警告对话框,);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); 参考答案:第1处:inputNumbergetText()第2处:JOptionPaneWARNING_MESSAGE本题是一个Applet,它的功能是在窗口上添加1212个标签,并且横向和纵向标签的颜色为黑白相间。import javaapplet*;import javaawt*;import java,awtevent*;pubhc class java2extends AppletGridLayout grid;pubhc void init()grid=new GridLayout(12,12);setLayout(grid);Label=new Label1212;for(int i=0;i12;i+)for(int j=0;j12;j+)labelij=;if(i+j)2= =0)labelijsetBackground(Colorblack);elselabelijsetBackground(colorwhite);add(labelij); 参考答案:第1处:label第2处:new label()本题中定义了一个树型的通信录,窗El左侧是一个树,右侧是一个文本域,单击树的结点,则在右侧文本域中显示相关信息,如果单击的是树结点,则显示对应名字的电话信息。import javaxswing*;import javaxswingtree*;import javaawt*;import javaawtevent*;import javaxswingevent*;class Mytree2 extends JFrameJTree tree=null;JTextArea text=new JTextArea(20,20);Mytree2()Container con=getContentPane();DefauhMutableTreeNode root=new Default-MutableTreeNode(同学通信录);DefaultMutableTreeNode tl=new Default-MutableTreeNode(大学同学);DefaultMutableTreeNode t2=new Default-MutableTreeNode(研究生同学);DefaultMutableTreeNode tll=new Default-MutatleTreeNode(陈艳);DefaultMutableTreeNode tl2=new Default-MutableTreeNode(李小永);DefaultMutableTreeNode t21=new Defauh-MutableTreeNode(王小小);DefauhMutableTreeNode t22=new Defauh-MutableTreeNode(董小);setTitle(java2);rootadd(t1);rootadd(t2);tladd(t1_1);tladd(t1_2);t2add(t2_1);t2add(t2_2);tree=new JTree(root);JSerollPane scrollpane=new JScrollPane(text);JSplitPane splitpane=new JSplitPane(JSplitPaneHORIZONTAL SPLIT,true,tree,scrollpane);treeaddTreeSeleetionListener(this);conadd(splitpane);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)Systemexit(0););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(strequals(陈艳)(textsetText(str+:联系电话;else if(strequals(李小永)textsetText(str+:联系电话;else if(strequals(王小小)textsetText(str+:联系电话;)else if(strequals(董小)textsetText(str+:联系电话;elsetextsetText(nodegetUserObject()toString();public class java2public static void main(String args)Mytree2 win=new Mytree2();winpack(); 参考答案:第1处:implements TreeSelectionListener第2处:nodetoString()本题中,主窗口有一个按钮“打开对话框”和一个文本域,单击按钮“打开对话框”后会弹出一个对话框,对话框上有两个按钮“Yes和“N0”,单击对话框上的“Yes”和“N0”按 钮后返回主窗口,并在右侧文本域中显示刚才所单击的按钮信息。import javaawtevent*;import javaawt*;class MyDialogimplements ActionListenerstatic final int YES=1,N0=0;int message=-1;Button yes,no;MyDialog(Frame fString 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 windowClosing(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 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(this,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);if(dialoggetMessage()= =MyDialogYES)textappend(n你单击了对话框的yes按钮);else if(dialoggetMessage()= =MyDialogNO)textappend(n你单击了对话框的N0按钮);public class java2public static void main(String args)new Dwindow(java2); 参考答案:第1处:extends Dialog第2处:dialogsetVisible(true)题中,在下画线上填写代码,指定变量b为字节型,变量f为单精度实型,变量1为64位整型。public class javalpublic static void main(String-args)b=49;f=89f;l=0xfedl;Systemoutprintln(b=+b);Systemoutprintln(f=+f):SystemOUtprintln(l=+1); 参考答案:第1处:byte第2处:float第3处:long本题的功能是读人运行程序时所传入的参数(一个或多个),并将参数依次显示出来,比如运行程序:java javalpartl part2,则打印输出为:partl part2。public class javalpublic static void main(Stringargs)int i=0:while()SystemOUtprint(+);Systemoutprintln(); 参考答案:第1处:iargslength第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()if()sum+:i+;Systemoutprintln(”60:”+sum); 参考答案:第1处:int sum=0第2处:iscorelength第3处:scorei60或scorei=59本题的功能是用冒泡法对数组元素arr=30,1,-9,70)进行从小到大排列。冒泡法排序是比较相邻的两个元素的大小,然后把小的元素交换到前面。public class javalpublic static void main(Stringargs)int i,j;int arr=30,1,-9,70);int n=;for(i=0;in-1;i+)for(j=i+1;jarrj)int temp=arri;for(i=0;i0)choiceadd(name);choiceselect(name);areaappend(n添加+name);else if(egetSource()= =del)choiceremove();areaappend(n删除+choicegetSelectedItem(); 参考答案:第1处:choicegetSelectedltem()第2处:choicegetSelectedIndex()本题使用下拉菜单来控制字体,窗口中有一个标签和一个下拉菜单,当选中下拉菜单中的任一项字体时,标签上字符串的字体就随之改变。import javaawt*;import javaawtevent*;import javaxswing*;class ComboBoxFrame extends JFrame public ComboBoxFrame()setTitle(java2);setSize(300,200);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0););style=new JComboBox():stylesetEditable(true);styleaddhem(Serif);styleaddItem(SansSerif);styleaddhem(Monospaced);styleaddhem(Dialog);styleaddhem(Dialoglnput);styleaddActionListener(this);JPanel p=new JPanel();Padd(style);getContentPane()add(p,South);panel=new ComboBoxTestPanel();getContentPane()add(panel,Center);public void actionPerformed(ActionEvent evt)JComboBox source=(JComboBox);String item=(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);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处:evtgetSource()本题中,生成一个窗口,该窗口的长、宽为屏幕长、宽的一半,并且窗口的大小不能改变。import javaawt*;import javaxswing*;public class java2publicstatic void main(Stringargs)FrameSize frame=new FrameSize();framesetDefaultCloseoperation(JFrameEXITON_CLOSE);frameshow();class FrameSize extends JFramepublic FrameSize()setTitle(java2);Toolkit tk=ToolkitgetDefaultToolkit();Dimension screenSize=;int screenHeight=screenSizeheight;int screenWidth=screenSizewidth;setSize(screenWidth2,sereenHeight2); 参考答案:第1处:tkgetScreenSize()第2处:setResizable(false)本题中定义了一个简单的计算器,可以进行基本的四则运算。程序中包含16个按钮用来表示09、+、-、 *、一运算符和小数点,程序顶部的文本框用来显示操作数以及结果。import javaawt*;import javaawtevent*;import javaxswing*;public 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;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);JPanel P=new JPanel();PsetLayout(new GridLayout(4,4);String buttons=789456*123-0=+:for(int i=0;ibuttonslength();i+)btn=new JButton(buttonssubstring(i,i+1);Padd(btn);add(P,Center);public 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(-)displaysetText(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(*)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 WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0););Container contentPane=getContentPane();contentPaneadd(new CalculatorPanel(); 参考答案:第1处:btnaddActionListener(this)第2处:DoubleparseDouble(displaygetText()本题是个表格式的成绩单,其中包括“姓名”、“英语成绩”、“数学成绩”和“总成绩”,姓名和成绩都可以进行修改,单击按钮“计算每人总成绩”,则可以统计出每个人的总成绩并显示在总成绩栏中。 参考答案:第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();DefaultMutableTreeNode root=new DefaultMutableTreeNode(”同学通信录”);DefaultMutableTreeNode tl=new DefauhMutableTreeNode(”大学同学”);DefaultMutableTreeNode t2=new DefaultMutableTreeNode(”研究生同学”);DefaultMutableTreeNode tll=new DerauhMutableTreeNode(”陈艳”);DefaultMutableTreeNode tl2=new DefaultMutableTreeNode(”李小永”);DefaultMutableTreeNode t21=new DefaultMutableTreeNode(”王小小”);DefauhMutableTreeNode t22=new DefaultMutableTreeNode(”董小”);setTitle(”java2”);rootadd(t1);rootadd(t2);tladd(ti_1);tladd(t1_2);t2add(t2_1);t2add(t2_2):tree=new JTtee(root);JScrollPane scrollpane=newJScrollPane(text);JSplitPane split

温馨提示

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

评论

0/150

提交评论