问答题 本题是一个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){} }
判断题 有这样一列数字:100816449362516941,如果孤立地记忆,则有18个记忆单位,难度很大,但如果将它们看成10、9、8、7、6、5、4、3、2、1的平方,则有10个有规律的记忆单位,提高了记忆的成效,这是利用了记忆编码中的组块化策略。()
问答题 本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数。 import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class java3 extends JFrame { public static JTextPane textPane; public static JScrollPane scrollPane; JPanel panel; public java3() { super("java3()"); panel=new JPanel(); panel.setLayout(new BorderLayout()); panel.setBorder(BorderFactory.createEmptyBor- der(20,20,20,20)); textPane=new JTextPane(); textPane.setFont(new Font("monospaeed", Font.PLAIN,12)); scrollPane=new JScrollPane(textPane); panel.add(scrollPane); scrollPane.setPreferredsize(new Dimension(300, 250)); setContentPane(panel); setCloseOperation(JFrame.EXIT_ON_CLOSE); LineNumber lineNumber=new LineNumber(); scrollPane.setRowHeaderView(lineNumber); } public static void main(String[]args) { java3 ttp=new java3(); ttp.pack(); ttp.setVisible(true); } } class LineNumber extends JTextPane { private final static Color DEFAULT_BACK- GROUND=Color.gray; private final static Color DEFAULT_FORE- GROUND=Color.black; private final static Font DEFAUl。T—FONT=new Font("monospaced",Font.PLAIN,12); private final static int HEIGHT=Integer.MAX_ VALUE-1000000; private final static int MARGIN=5; private FontMetrics fontMetrics; private int lineHeight; private int currentRowWidth; private JComponent component; private int componentFontHeight; private int componentFontAscent; public LineNumber(JComponent component) { if(component= =null) { setBackground(DEFAULT_BACKGROUND); setForegroun"DEFAULT_FOREGROUND); setFont(DEFAULT FONT); this.component=this; } else { setBaekground(DEFAULT_BACKGROUND); setForeground(component.getForeground()); setFont(component.getFont()); this.component=component; } componentFontHeight=component.getFontMet- rics(component.getFont()).getHeight(); componentFontAscent=component.getFontMet- ries(component.getFont()).getAscent(); setPreferredWidth(9999); } public void setPreferredWidth(int row) { int width=fontMetrics.stringWidth(String.val- ueOf(row)); if(currentRowWidth<width) { currentRowWidth=width; setPreferredSize(new Dfimension(2 * MARGIN +width,HEIGHT)); } } public void setFont(Font font) { super.setFont(font); fontMetrics=getFontMetrics(getFont()); } public int getLineHeight() { if(hneHeight= =0) return componentFontHeight; else return lineHeight; } public void setLineHeight(int lineHeight) { if(hneHeight>0) this.lineHeight=lineHeight; } public int getStartOffset() { return component.getlnsets().top+component- FontAscent; } public void paintComponent(Graphics g) { int lineHeight=getLineHeight(); int startOffset=getStartOffset(); Rectangle drawHere=g.getClipBounds(); g.setColor(getBackground()); g.fillRect(drawHere.x,drawHere.Y,drawHere. width,drawHere.height); g.setColor(getForeground()); int startLineNumber=(drawHere.y/line- Height)+1; int endLineNUmber = startLineNumber+ (drawHere.height/lineHeight); int start=(drawHere.Y/hneHeight)*line- Height+startOffset; for(int i=startLineNumber;i<=endLineN- umber;i++) { String lineNumber=String.valueOf(i); int width=fontMetrics.stringWidth(lineNumber ); g.drawstring(lineNumber,MARGIN+current- RowWidth-width,start); start+=lineHeight: } setPreferredWidth(endLineNumber); } }
问答题 本题主窗口中包括一个文本框和一个文本域,在上面的文本框中输入一个整数并按回车键,就会在下面的文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。 tmport java.awt.event.*: import java.awt.*; import javax.swing.JOptionPane; class Dwindow extends Frame implements ActionLis- tener {TextField inputNumber; TextArea show; Dwindow(String s) {super(s): inputNumber=new TextField(22); inputNumber.addActionListener(this); show=new TextArea(); add(inputNumber,BorderLayout.NORTH); add(show,BorderLayout.CENTER); setBounds(60,60,300,300);setVisible(true); validate(); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); } }); } public void actionPerformed(ActionEvent e) {boolean boo=false; if(e.getSource()= =inputNumber) {String s= ; char a[]=S.toCharArray(); for(int i=0;i<a.length;i++) {if(!(Character.isDigit(a[i]))) boo=true; } if(boo= =true) {JOptionPane.showMessageDialog(this,"您输入 了非法字符","警告对话框", ); inputNumber.setText(null); } else if(boo= =false) {int number=Integer.parseInt(s); show.append("\n"+number+"平方:"+(num- bet*number)): } } } } public class java2 {public static void main(String args[]) { new Dwindow("java2"); } }
问答题 本题的功能是求1~100的自然数的累加,并打印输出 计算结果。 public class javal{ public static void main(String[]args){ int sum=0; int i=1; for(;;){ if( ){ sum=sum+i: }else ’ ; } System.OUt.println("sums="+sum); } }
判断题 非零售信贷管理系统允许评级发起人员进行评级结果试算。( )
问答题 本题的功能是监听鼠标左右键的单击,以及面板中滚动条的添加。在窗口的画板中单击鼠标左键,在单击的位置绘制一个圆,当绘制的圆大于画板的大小时,画板就添加滚动条,在画板中单击鼠标右键,则清除画板中的所有图形。 import javax.swing.*; import javax.swing.event.MouselnputAdapter; import java.awt.*; import java.awt.event.*; import java.util.*; public class java3 extends JPanel{ private Dimension size; private Vector objects; private final Color colors[]={ Color.red,Color.blue,Color.green,Color.or- ange, Color.cyan,Color.magenta,Color.darkGray, Color.yellow); private final int color_n=colors.length; JPanel drawingArea; public java3(){ setopaque(true); size=new Dimension(0,0); objects=new Vector(); JLabel instructionsLeft=new JLabel("单击鼠标 左键画圆."); JLabel instructionsRight=new JLabel("单击鼠 标右键清空画板."); JPanel instructionPanel=new JPanel(new Grid- Layout(0,1)); instructionPanel.add(instructionsLeft); instructionPanel.add(instructionsRight); drawingArea=new JPanel(){ protected void paintComponent(Graphics g){ super.paintComponent(g); Rectangle rect; for(int i=0;i<objects.size();i++){ rect=(Rectangle)objects.elementAt(i): g.setColor(colors[(i%color_n)]); g.fillOval(rect.X,rect.Y,rect.width,rect. height); } } }; drawingArea.setBackground(Color.white); drawingArea.addMouseListener(new MouseLis- tener()); JScrollPane scroller=new JScrollPane(drawing- Area); scroller.setPreferredSize(new Dimension(200, 200)); setLayout(new BorderLayout()); add(instructionPanel,BorderLayout.NORTH); add(scroller,BorderLayout.CENTER): } class MyMouseListener extends mouseInputAdapt- er{ final int W=100; final int H=100; public void mouseReleased(MouseEvent e){ boolean changed=false; if(SwingUtilities.isRightMouseButton(e)){ objects.removeAllElements(); size.width=0; size.height=0; changed=true; }else{ int X=e.getX()-W/z; int Y=e.getY()-H/2 if(x<0)x=0; if(y<O)Y=0; Rectangle rect=new Rectangle(X,Y,W,H); objeets.addElement(rect); drawingArea.scrollRectToVisible(rect); int this_width=(x+W+2); if(this width>size.width) {size.width=this_width;changed=true;} int this=height=(y+H+2); if(this_height>size.height) {size.height=this_height;changed=true;} } if(changed){ drawingArea.setPreferredSize(size); drawingArea.revalidateI(); } drawingArea.paint(); } } public static void main(String args[]){ JFrame frame=new JFrame("java3"): frame.addWindowListener(new WindowAdapter (){ public void windowClosing(WindowEvent e) {System.exit(0);} }); frame.setContentPane(new java3()); frame.pack(); frame.setVisible(true); } }
问答题 本题中,主窗口有一个按钮“打开对话框”和一个文本域,单击按钮“打开对话框”后会弹出一个对话框,对话框上有两个按钮“Yes"和“N0”,单击对话框上的“Yes”和“N0”按 钮后返回主窗口,并在右侧文本域中显示刚才所单击的按钮信息。 import java.awt.event.*; import java.awt.*; class MyDialog implements ActionListener {static final int YES=1,N0=0; int message=-1;Button yes,no; MyDialog(Frame f.String S,boolean b) {super(f,S,b); ves=new Button("Yes");yes.addActionListener (this); no=new Button("No"); no.addActionListener (this)o setLayout(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(e.getSource()= =yes) {message=YES; setVisible(false); } else if(e.getSource()= = no) {message=NO; setVisible(false); } } public int getMessage() {return message; } } class Dwindow extends Frame implements ActionLis- tener {TextArea text;Button button;MyDialog dialog; Dwindow(String s) {super(s); text=new TextArea(5,22);button=new Button ("打开对话框"); button.addActionListener(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) {System.exit(0);} }); } public void actionPerformed(ActionEvent e) {if(e.getSource()= =button) {; if(dialog.getMessage()= =MyDialog.YES) {text.append("\n你单击了对话框的yes按 钮"); } else if(dialog.getMessage()= =MyDialog.NO) {text.append("\n你单击了对话框的N0按 钮"); } } } } public class java2 {public static void main(String args[]) {new Dwindow("java2"); } }
问答题 本题中数组arr中存储了学生的成绩,分别为87,45,56,78,67,56,91,62,82,63,程序的功能是计算低于平均分的人数,并打印输出结果。请在程序空缺部分填写适当内容。使程序能正确运行。 public class javal{ public static void main(String[]args){ int arr[]={56,91,78,67,56,87,45,62,82,63}; int num=arr.length int i=0; int sumScore= 0; int sumNum=0; double average; while(i<num){ sumScore=sumScore+arr[i]; ; } average= ; i=0; do{ if(arr[i]<average) sumNum++: i++; }while( ); System.OUt.println("average:"+average+",be- lows average:"+sumNum); } }
判断题 规模较大的高校可实行“统一领导、分级管理”的财务管理体制。( )