相关考题

问答题 本题中定义了长度为20的一维整型数组a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。 public class javal{ public static void main(String[]args){ int a[]= ; int i; for( ;i++) a[i]=i; for(i=0;i<20;i++){ if( ) System.OUt.print("a[It+i+"]="+a[i]+","); } } }

问答题 本题的功能是监听键盘键的敲击,并显示在窗口中。 import javax,.swing.*; importjava.awt.*; , import java.awt.event.*; public class java3 extends JFrame extends KeyListener { private String linel=""line2="" private String line3="" private JTextArea textArea; public java3() { super("java3"); textArea=new JTextArea(10,15); textArea.setText("Press any key on the key- board…"); textArea.setEnabled(false); addKeyListener(this); getContentPane().add(textArea); setSize(350,100); show(); } public void keyPressed(KeyEvent e) { linel="Key pressed:"+e.getKeyText(e. getKeyCode()); setLines2and3(e); } public void keyReleased(KeyEvent e) { linel="Key released:"+e.getKeyText(e. getKeyCode()); setLines2and3(e): } public void keyTyped(KeyEvent e) { Linel="Key typed:"+e.getKeychar(); setLines2and3(e); } private void setLines2and3(KeyEvent e) { line2="This key is"+(e.isActionKey()?"" :"not")+"an action key"; String temp=e.getKeyModifiersText(e.get- Modifiers()); hne3="Modifier keys pressed:"+(temp.e- quals("")?"none":temp); textArea.setText(linel+"\n"+line2+"\n" +line3+"\n"); } public static void main(String args[]) { java3 app=new java3(); addWindowListener(new Windowadapted() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }

问答题 本题是一个Applet,页面中有10个按钮,名称从“0~ 9”,用鼠标任意单击其中一个按钮后,通过键盘上的上下左右键可以控制按钮在窗口中移动。 import java.applet.*; import java.awt.*; import java.awt.event.*; public class java2 extends Applet {Button b[]=new Button[10]; int x,Y; public void init() {for(int i=0;i<=9;i++) {b[i]=new Button(""+i); b[i].addKeyListener(this); addCb[i]); } } public void {Button button=(Button)e.getSource(); x=button.getBounds().x; y—button.getBounds().y; if(e,getKeyCode()= =KeyEvent.VK_UP) {y=y-2; if(y<=O)y=0; button.setLocation(x,y); } else if(e.getKeyCode()= =KeyEvent.VK_ DOWN) {y=y+2; if(y>=300)y=300; button,.setLocation(X,y); } else if(e.getKeyCode()= =KeyEvent.VK_ LEFT) {x=x-2; if(x<=0)x=0; button.setLocation(x,y); } else if(e.getKeyCode()= =KeyEvent.VK_ RIGHT) (x=X+2; if(x>=300)x=300; button.setLoeation(X,y); } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} }