问答题请完成下列Java程序:建立一个Applet程序,包括创建一个画布构件、一个面板构件,面板构件上包含3个按钮,用来设置画布和面板的背景颜色,这3个按钮(Red,Green,Blue)分别控制画布和面板背景色改变为三原色:红、绿、蓝。要求画布宽度300高度200。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:importjava.awt.*;importjava.applet.*;/*<appletcode="ex6_2.class"width=800height=400></applet>*/publicclassex6_2extendsApplet{privatePanelpane16_2;privateCanvascanvas6_2;privateButtonbtnl,btn2,btn3;publicvoidinit(){canvas6_2=newCanvas();canvas6_2.______________;canvas6_2.setBackground(Color.black);add(canvas6_2);pane16_2=newPanel();pane16_2.setBackground(Color.black);btn1=newButton("Red");btn2=newButton("Green");btn3=newButton("Blue");pane16_2.add(btn1;pane16_2.add(btn2;pane16_2.add(btn3;____________;}publicbooleanactionEvente,Objectoif(e.target==btn1){pane16_2.setBackground(Color.red);canvas6_2.setBackground(Color.red);}elseif(e.target==btn2){pane16_2.setBackground(Color.green);canvas6_2.setBackground(Color.green);}elseif(e.target==btn3){canvas6_2.setBackground(Color.blue);pane16_2.setBackground(Color.blue);}returntrue;}}
问答题本程序是一个Applet,它实时显示当前的系统时间,即为一个时钟,如图所示。importjava.util.Date;importjava.awt.Graphics;publicclassexam_65extendsjava.applet.Applet______{ThreadclockThread;publicvoidstart(){ifclockThread==null){clockThread=newThread(this,"Clock");clockThread.start();}}publicvoidrun(){while(clockThread!=null){______;try{clockThread.sleep(1000);}catch(InterruptedExceptione){)}}publicvoidpaint(Graphicsg){Datenow=newDate();g.drawString(now.toString(),5,10);}publicvoidstop(){clockThread.stop();clockThread=null;}}exam_65.html:<html><head><title>exam_65</title></head><body><appletcode="exam_65.class"width="500"height="50"></applet></body></html>
问答题请完成下列Java程序:实现打印出自己的源文件的功能。
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
import java.io.*;
import java.util.StringTokenizer;
public class ex27_2{
public static void main(String args[])throws IOException{
FileInputStream fis=new FileInputStream("ex27_2.java");
DataInputStream dis=new DataInputStream(fis);
String str=null;
while(true){
__________________;
if(str==null){
__________________;
}
StringTokenizer st=new StringTokenizer(str);
while(st.hasMoreTokens()){
System.out.print(st.nextToken()+ " " );
}
System.out.println();
}
}
}
问答题____________________.exit(0);
问答题以下程序中,当用户单击“移动”按钮以后,就可以使用方向键控制屏幕上句子的移动,单击“停止”按钮,则句子不再随着方向键移动。运行结果如下图所示注意:请勿改动其他已有语句内容,仅在横线处填入适当语句。importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassExample2_8extendsAppletimplementsKeyListener{publicvoidkeyTyped(KeyEvente){}publicvoidkeyReleased(KeyEvente){}Buttonbutton;ButtonstopButton;Labelout;intx,y;publicvoid_______(){button=newButton("移动");button.addActionListener(newAddMoveListener(this));stopButton=newButton("停止移动");stopButton.addActionListener(newRemoveListener(this));stopButton.setEnabled(false);out=newnabel("按下按钮以后我可以随方向键移动");add(button);add(stopButton);add(out);}publicvoidstart(){super,start();}publicvoidkeyPressed(KeyEvente){x=out.getBounds().x;y=out.getBounds().y;if(e.getKeyCode()==KeyEvent.VK_UP){y=y-2;if(y<=0)y=0;out.setLocation(x,y);}elseif(e.getKeyCode()==KeyEvent.VK_DOWN){y=y+2;if(y>=300)y=300;out.setLocation(x,y);}elseif(e.getKeyCode()==KeyEvent.VK_LEFT){x=x-2;if(x<=0)x=0;out.setLocation(x,y);}elseif(e.getKeyCode()==KeyEvent.VK_RiGHT){x=x+2;if(x>=300)x=300;out.setLocation(x,y);}}classAddMoveListenerimplementsActionListener{Example2_8lis;publicAddMoveListener(Example2_8lis){this.lis=lis;}publicvoidactionPerformed(ActionEvente){button._________(lis);stopButton,setEnabled(true);}}classRemoveListenerimplementsActionListener{Example2_8lis;publicRemoveListener(Example2_8lis){this.lis=lis;}publicvoidactionPerformed(ActionEvente){button,removeKeyListener(lis);stopButton.setEnabled(false);}}}Example2_8.html<HTML><HEAD><TITLE>Example2_8</TITLE></HEAD><BODY><APPLETCODE="Example2_8.class"WIDTH=400HEIGHT=500></APPLET></BODY></HTML>
问答题下面是一个Apple(程序,程序的功能是在Applet显示区内画一个动态的、多维的绿色椭圆环。本题主要是通过数学方法sin(),cos()画出一个圆环。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序的执行结果:importjava.awt.*;importjava.awt.event.*;importjava.applet.*;publicclassYuanHuanextendsApplet{publicvoidpaint(Graphicsg){doublew=getSize().width/2;doubleh=getSize().height/2;{{U}}g.getColor(Color.green);{{/U}}for(doubleth=0;th<10;th+=0.00003){doubler=Math.cos(16*th)+th;doublex=r*Math.cos(th)+w;doubley=r*Math.sin(th)+h;g.drawOval((int)x-1,(int)y-1,3,3);}}publicstaticvoidmain(Stringargs[]){Framef=newFrame("Draw");YuanHuanp=newYuanHuan();{{U}}p.inti(){{/U}};p.start();f.add(p);f.setSize(400,300);f.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){{{U}}System.out.exit(0);{{/U}}}});f.show();}}{{B}}ex33_3.htm1:{{/B}}<html><head><title>ASimpleProgram</title></head><body><appletcode="YuanHuan.class"width=800height=400></applet></body></html>
问答题本程序的功能是定义一个数组,然后将数组元素下标的值赋值给数组元素,最后按照数组元素下标的逆序输出数组。
public class exam_18{
public static void main(String[] args){
______arr=new int[10];
int i;
for(______)
err[i]=i;
for(______)
System.out.print("arr["+i+"]="+arr[i]+" ");
}
}
问答题本程序求一个二维整型数组a中的最小数值并赋给变量min,其中数组a的第一行数据分别为:23、45、52、13、67、83、12,第二行数据分别为:45、67、14、52、7,第三行数据分别为:91、85、11,最后打印输出结果。
public class exam_92{
public static void main(String[] args) {
int a[][]={{23,45,52,13,67,83,12},{45,67,14,52,7},{91,85,11}};
int i,j;
int min=a[0][0];
for(i=0;______;i++){
for(j=0;______;j++){
if(a[i][j]<min)
______;
}
}
System.out.println("min="+min);
}
}
问答题注意:下面出现的“考生文件夹”均为%USER%。在考生文件夹下存有文件名为Java_2.java文件,本题功能是完成对字符串缓存类中与字符相关方法的应用。请完善Java_2.java文件,并进行调试,程序运行的某次结果如下:由于Java_2.java文件不完整,请在注释行“//*********Found*********”下一行语句的下画线地方填入正确内容,然后删除下画线,请勿删除注释行或其他已有语句内容。存盘时,文件必须存放在考生文件夹下,不得改变原有文件的文件名。给定源程序:importjavax.swing.*;publicclassJava_2{publicstaticvoidmain(Stringargs[]){StringBufferbuf=newstringBuffer("你好!祝你成功!");Stringoutput="buf="+buf.toString()+"/nCharacterat0:"+buf.charAt(0)+"/nCharacterat4:"+buf.charAt(4);//*********Found**********charcharArray[]=______char[buf.length()];//*********Found**********buf.______(0,buf.length(),charArray,0);output+="/n/n在字符串缓存中的字符是:";//*********Found**********for(inti=0;i<______;++i)output+=charArray[i];buf.setCharAt(0,'您');buf.setCharAt(6,'材');output+="/n/nbuf="+bur.toString();//*********Found**********buf.______;output+="/n/nbuf="+buf.toString();JOptionPane.showMessageDialog(null,output,"字符串缓存的字符相关方法示范",JOptionPane.INFORMATION_MESSAGE);System.exit(0);}}
问答题本程序的功能是统计用户点击按钮的次数。程序窗口中有一个按钮“请点击按钮”和一个文字标签,每单击一次按钮,计数就增1,同时文字标签显示出单击按钮的次数,如图所示。importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassexam_51{privatestaticStringlabelPrefix="点击按钮的次数是:";privateintnumClicks=O;publicComponentcreateComponents(){finalJLabellabel=newJLabel(labelPrefix+"0");.JButtonbutton=newJButton("请点击按钮");button.setMnemonic(KeyEvent.VK_I);button.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){______;label.setText(labelPrefix+numClicks);}});JPanelpane=newJPanel();pane.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));pane.setLayout(newGridLayout(0,1));pane.add(button);pane.add(label);returnpane;}publicstaticvoidmain(Stringargs[]){try{UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());}catch(Exceptione){}JFrameframe=newJFrame("exam_51");exam_51app=newexam_51();Componentcontents=app.createComponents();frame.getContentPane().add(contents,BorderLayout.CENTER);frame.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});frame.pack();______;}}
问答题本题中定义了一个简单的计算器,可以进行基本的四则运算。程序中包含16个按钮用来表示0~9、+、-、*、/、=运算符和小数点,程序顶部的文本框用来显示操作数以及结果。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class java2 public static void main(String[]args) try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); catch(Exception e) JFrame frame=new CalculatorFrame(); frame.show(); class CalculatorPanel extends JPanel implements ActionListener private 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"); display.setEditable(false); add(display,"North"); JPanel p=new JPanel(); p.setLayout(new GridLayout(4,4)); String buttons="789/456*123-0.=+"; for(int i=0;i<buttons.length();i++) btn=new JButton(buttons.substring(i,i+1)); p.add(btn); ______; add(p,"Center"); public void actionPerformed(ActionEvent evt) String s=evt.getActionCommand(); if('0'<=s.charAt(0) else display.setText(display.getText()+s); start=false; else if(start) if(s.equals("-")) display.setText(s); start=false; else op=s; else double x=______; calculate(x); op=s; start=true; public void calculate(double n) if(op.equals("+"))arg+=n; else if(op.equals("-"))arg--n; else if(op.equals("*"))arg*=n; else if(op.equals("/"))arg/=n; else if(op.equals("="))arg=n; display.setText(""+arg); class CalculatorFrame extends JFrame public CalculatorFrame() setTitle("java2"); setSize(220,180); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); Container contentPane=getContentPane(); contentPane.add(new CalculatorPanel());
问答题请完成下列Java程序。程序的功能是利用迭代法求一个数的平方根(求平方根的迭代公式为:Xn+1=1/2(Xn+a/Xn))。
注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
public class PingFangGen{
public static void main(String args[ ]){
System.out.println(Math.sqrt(2.0));
}
static double sqrt(__________________){
double x=1.0;
do
{
____________________
}
while( Math.abs(x*x-a)/a>le-6);
return x;
}
}
问答题本题是一个Applet,页面中有两个文本域,当左侧文本域中的文本发生变化时,该文本域中的文本以行为单位按长度由短到长排列在右边的文本域中。 import java.util.*; import java.applet.*; import java.awt.*; import java.awt.event.*; public class java2 extends Applet implements TextListener TextArea text1,text2; public void init() text1=new TextArea(6,15); text2=new TextArea(6,15); add(text1);add(text2); text2.setEditable(false); ______; public void______ if(e.getSource()==text1) String s=text1.getText(); StringTokenizer fenxi=new StringTokenizer(s,",'/n'"); int n=fenxi.countTokens(); String a[]=new String[n]; for(int i=0;i<=n-1;i++) String temp=fenxi.nextToken(); a[i]=temp; for(int i=0;i<=n-1;i++) for(int j=i+1;j<=n-1;j++) if(a[j].compareTo(a[i])<0) String t=a[j];a[j]=a[i];a[i]=t; text2.setText(null); for(int i=0;i<n;i++) text2.append(a[i]+"n");
问答题本题的功能是通过按钮来选择窗口显示的风格。窗口中有三个按钮:“Metal”、“Motif”和“Windows”,单击任何一个按钮,就能将窗口的风格改变为按钮名称所对应的风格。 import java.awt.*; import java.awt.event.*; import javax.swing.*; class PlafPanel extends JPanei implements ActionListener public______() metalButton=new JButton("Metal"); motifButton=new JButton("Motif"); windowsButton=new JButton("Windows"); add(metalButton); add(motifButton); add(windowsButton); metalButton.addActionListener(this); motifButton.addActionListener(this); windowsButton.addActionListener(this); public void actionPerformed(ActionEvent evt) Object source=evt.getSource(); String plaf=""; if(source==metalButton) plaf="javax.swing.plaf.metal.MetalLookAndFeel"; else if(source==motifButton) plaf="com.sun.java.swing.plaf.motif.MotifLookAndFeel"; else if(source==windowsButton) plaf="com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; try UIManager.setLookAndFeel(______); SwingUtilities.updateComponentTreeUI(this); catch(Exception e) private JButton metalButton; private JButton motifButton; private JButton windowsButton; class PlafFrame extends JFrame public PlafFrame() setTitle("simple"); setSize(300,200); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); Container contentPane=getContentPane(); contentPane.add(new PlafPanel()); public class java2 public static void main(String[]args) JFrame frame=new PlafFrame(); frame.show();
问答题System.out.println("length:"+file2_1.____________ );
问答题本程序展示了一个Windows风格的多级树,非叶子节点的图标是打开或折叠的文件夹,叶子节点的图标是圆点,并且树的节点是可以编辑的,如图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.tree.*;publicclassexam_58{publicstaticvoidmain(String[]args){try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exceptione){}JFrameframe=newSimpleTreeFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}classSimpleTreeFrameextendsJFrame{privatestaticfinalintWIDTH=350;privatestaticfinalintHEIGHT=250;publicSimpleTreeFrame(){setTitle("exam_58");setSize(WIDTH,HEIGHT);DefaultMutableTreeNoderoot=newDefaultMutableTreeNode("AdvancedandPracticalJavaProgramming");DefaultMutableTreeNodechapter=newDefaultMutableTreeNode("Introduction");root.add(chapter);DefaultMutableTreeNodesection=newDefaultMutableTreeNode("ABriefIntroductiontoJava");chapter.add(section);section=newDefaultMutableTreeNode("AnExampleofAdvancedJavaApplication");chapter.add(section);section=newDefaultMutableTreeNode("TheContentsandOrgnizationofTheBook");chapter.add(section);chapter=newDefaultMutableTreeNode("JavaProgrammingforGUI");root.add(chapter);section=newDefaultMutableTreeNode("Introduction");chapter.add(section);section=newDefaultMutableTreeNode("SwingOverview");chapter.add(section);section=newDefaultMutableTreeNode("OverallEffectofGUI");chapter.add(section);DefaultMutableTreeNodesubsection=newDefaultMutableTreeNode("LayoutManagement");section.add(subsection);subsection=newDefaultMutableTreeNode("LookandFeel");section.add(subsection);section=newDefaultMutableTreeNode("TextInputandOutput");chapter.add(section);subsection=newDefaultMutableTreeNode("TextField");section.add(subsection);subsection=newDefaultMutableTreeNode("TextArea");section.add(subsection);subsection=newDefaultMutableTreeNode("StyledTextArea");section.add(subsection);section=newDefaultMutableTreeNode("Button,CheckBoxandRadioButton");chapter.add(section);subsection=newDefaultMutableTreeNode("Button");section.add(subsection);subsection=newDefaultMutableTreeNode("CheckBox");section.add(subsection);subsection=newDefaultMutableTreeNode("RadioButton");section.add(subsection);section=newDefaultMutableTreeNode("ComboBox");chapter.add(section);section=newDefaultMutableTreeNode("List");chapter.add(section);JTreetree=______;______(true);ContainercontentPane=getContentPane();contentPane.add(newJScrollPane(tree));}}
问答题本程序是一个Applet。页面加载时会弹出一个对话框,选择要画的图形,输入选择后单击“确定”按钮进入页面,同时页面绘出选择的图形,如图所示。importjava.awt.Graphics;importjavax.swing.*;publicclassexam_71extendsJApplet{intchoice;publicinit(){Stringinput;input=JOptionPane.showInputDialog("输入1绘制直线/n"+"输入2绘制矩形/n"+"输入3绘制园形/n");choice=Integer.parseInt(input);}publicvoidpaint(){for(inti=0;i<10;i++){switch(choice){case1:g.drawLine(10,10,250,10+i*10);break;case2:g.drawRect(10+i*10,10+i*10,50+i*10,50+i*10);break;case3:g.drawOval(10+i*10,10+i*10,50+i*10,50+i*10);break;default:showMessageDialog(null,"输入数据错误");}}}}exam_71.html:<html><title>exam_71</title><body><appletcode="exam_71.class"width="300"height="300"></applet></body></html>
问答题下面是一个Applet程序,其功能是根据公式:y=a*sin(x)绘制正弦曲线。要求窗口中有一个文本区作为曲线峰值a的输入,可以判断输入的数字是否符合要求,一个ok按钮,点击则在画布中绘制曲线,以画布中的横坐标值作为sin()的参数x。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:importjava.awt.*;importjava.awt.event.*;importjava.applet.Applet;importjava.lang.Math.*;/*<appletcode="ex18_3.class"width=800height=400></applet>*/publicclassex18_3extendsAppletimplementsActionListener{Panelpane=newPanel();drawWnddw;Label11=newLabel("峰值");TextFieldtf1=newTextField(15);Buttonbtn=newButton("OK");privatefloata=0;publicvoidinit(){pane.setLayout(newFlowLayout(FlowLayout.CENTER,25,5));pane.add(11);pane.add(tf1);pane.add(btn);btn.addActionListener(this);add("North",pane);dw=newdrawWnd();add("South",dw);}classdrawWndextendsCanvas{drawWnd(){setSize(300,100);setBackground(Color.GRAY);}publicvoidpaint(Graphicsg){g.setColor(Color.GREEN);for(inti=0;i<getSize().width-1;i++){inty=0;inty1=y+(int)(Math.sin(i)*a);{{U}}inty2=y1+(int)(a*Math.sin(i+1));{{/U}}g.drawLine(i,y1,i+1,y2);}}}publicvoidactionPerformed(ActionEventae){try{{{U}}a=Integer.parseInt(tf1.getText());{{/U}}dw.repaint();}catch(NumberFormatExceptionnfe){tf1.setText("error!");}}}{{B}}ex18_3.html{{/B}}<HTML><HEAD><TITLE>ex18_3</TITLE></HEAD><BODY><appletcode="ex18_3.class"width=800height=400></applet></BODY></HTML>
问答题综合应用题下面是一个Applet程序,其功能是在绘图区域中通过鼠标的移动来绘制直线,并且有清除绘图区域按钮,用来清除已经绘制的图像。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。程序运行结果如下:注意:不改动程序的结构,不得增行或删行。importjava.awt.*;importjava.applet.*;/**/publicclassex1_3extendsApplet{privateButtonbtn;privatebooleanbDraw,bClear;privateintupX,upY,downX,downY;publicvoidinit(){setLayout(null);bClear=false;bDraw=false;btn=newButton("clear");btn.reshape(250,150,70,30);add(btn);}publicvoidpaint(Graphicsg){if(bClear){g.clearRect(0,0,getSize().width,getSize().height);bClear=false;}if(bDraw){g.drawLine(upY,upX,downY,downX);bDraw=false;}}publicvoidupdate(Graphicsg){paint(g);}publicbooleanmouseDown(Eventevent,intx,inty){downX=x;downY=y;returntrue;}publicbooleanmouseUp(Eventevent,intx,inty){upX=x;upY=y;bDraw=false;repaint();returntrue;}publicbooleanaction(Eventevent,Objectobject){if(event.target!=clear){bClear=true;repaint();}returntrue;}}ex1_3.htmlex1_3
问答题下面是一个Applet程序,其功能是建立一个图形用户界面的窗口,包括一个文本显示区和一个按钮,点击按钮,可以在文本区已有的文本基础上追加显示10条“WelcometotheNCRExamination!”信息,并且文本区由滚动条控制文本的上下滚动。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;/*<appletcode="ex10_3.class"width=800height=400></applet>*/publicclassex10_3extendsJApplet{JButtonjb=newJButton("AddText");JTextPaneitp=newJTextPane();publicvoidinit(){jb.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){for(inti=1;i<10;i++){{U}}jtp.getText(jtp.setText()+"WelcometotheNCRExamination!/n"){{/U}};}});Containercp=getContentPane();cp.add(newJScrollPane(jtp));{{U}}cp.add(BorderLayout.SOUTH,jtp){{/U}};}publicstaticvoidmain(String[]args){ex10_3obj10_3=newex10_3();Stringstr=obj10_3.getClass().toString();if(str.indexOf("class")!=-1)str=str.substring(6);JFramefrm=newJFrame(str);frm.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventwe){System.exit(O);}});{{U}}frm.getContentPane().add(ex10_3){{/U}};frm.setSize(300,400);frm.setVisible(true);}}{{B}}ex10_3.html{{/B}}<HTML><HEAD><TITLE>ex10_3</TITLE></HEAD><BODY><appletcode="exl0_3.class"width=800height=400></applet></BODY></HTML>
