问答题 本题是一个Applet,功能是用鼠标画不同颜色的图形。页面中有5个按钮“画红色图形”、“画绿色图形”、“画蓝色图形”、“橡皮”和“清除”,单击前三个按钮中的一个,按住鼠标左键或右键在面板中拖动,就能两出对应颜色的线条,单击“橡皮”按钮,按住鼠标左键或右键在面板中拖动就能将面板中的图形擦除掉,单击“清除”按钮,就能将面板中所有的图形清除掉。 import java.applet.*; import java.awt.*; import java.awt.event.*; public class java3 extends Applet implements ActionListener {int x=-1,y=-1,rubberNote=0,clearNote=0; Color C=new Color(255,0,O); int con=3; Button b_red,b_blue,b_green,b_clear,b_quit; public void init() { addMouseMotionListener(this); b_red=new Button("画红色图形"); b_blue=new Button("画蓝色图形"); b_green=new Button("画绿色图形"); b_quit=new Button("橡皮"); b_clear=new Button("清除"); add(b_red); add(b_green); add(b_blue); add(b_quit); add(b_clear); b_red.addActionListener(this); b_green.addActionListener(this); b_blue.addActionListener(this); b_quit.addActionListener(this); b_dear.addActionListener(this); } public void paint() (if(x!=-l&&y!=-l&rubberNote= = 0R&clearNote= =0) {g.setColor(c); g.filloval(X,Y,con,con); } else if(rubberNote= =1&&clearNote= =O) {g.clearRect(x,Y,10,10); } else if(clearNote= =1&&rubberNote= =O) {g.clearRect(0,0,getSize().width,getSize(). height); } } public void mouseDragged(MouseEvent e) {x=(int)e.getX();y=(int)e.getY();repaint(); } public void mouseMoved(MouseEvent e){) public void update(Graphics g) {paint(g); } public void actionPerformed(Event e) {if(e.getSource()= =b-red) {rubberNote=0;clearNote=0;c=new Color (255,0,0); } else if(e.getSource()= =b_green) {rubberNore=0;clearNote=0;C=new Color(0, 255,0); } else if(e.getSource()= =b_blue) {rubberNote=0;clearNote=0;C=new Color(0, 0,255); } if(e.getSource()= =b—quit) {rubberNote=1;clearNote=0; } if(e.getSource()= =b—clear) {clearNote=1;rubberNote=0;repaint(); } } }
问答题 本题的功能是通过鼠标确定两个点,然后画两点间的直线。窗口中有一个按钮“Draw line”,单击该按钮后,它就处于按下状态,然后用鼠标在窗口中单击一下,在单击的地方就会出现一个坐标圆点,用鼠标在另外一个地方单击一下又会出现另外一个圆点,并且此时在两个坐标圆点间画出一条直线,且“Draw line”处于可用状态,再单击这个按钮就可以画另外一条直线。 Import java.awt.* ; import java.awt.event.*; import javax.swing.*; class EventQueuePanel extends JPanel implements Ac- tionListener {EventQueuePanel() {JButton button=new JButton("Draw line"); add(button); button.addActionListener(this); } public void actionPerformed(ActionEvent evt) (Graphics g=getGraphics(); P=getClick(); g.drawOval(P.x-2,P.Y-2,4,4); Point q=getClick(); g.drawOval(q.x-2,q.y-2,4,4); g.drawLine(P.X,P.Y,q.x,q.y); g.dispose(); } public Point getClick() {EventQueue eq=Toolkit.getDefaultToolkit(). getSystemEventQueue(); while(true) {try {AWTEvent evt=eq.getNextEvent(); if(evt.getID()= =MouseEvent.MOUSE. PRESSED) {MouseEvent mevt=(MouseEvent)evt; Point P= (); Point top=getRootPane().getLocation(); P.X-=top.x; P.Y-=top.Y; return P; } } catch(InterruptedException e) {} } } private int Y=60; } class EventQueueFrame extends JFrame {public EventQueueFrame() {setTitle("java2"); setSize(300,200); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); } }); Container contentPane=getContentPane(); contentPane.add(new EventQueuePanel()); } } public class java2 {public static void main(String[]args) {Frame frame=new EventQueueFrame(); frame.show(); } }
判断题 合作游戏是指由教师建议或开始一个游戏,教师事先定好某些角色,以部分控制游戏的进行。()
问答题 本题中定义了一个树型的通信录,窗El左侧是一个树, 右侧是一个文本域,单击树的结点,则在右侧文本域中显示 相关信息,如果单击的是树结点,则显示对应名字的电话 信息。 import javax.swing.*; import javax.swing.tree.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; 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- MutableTreeNode("大学同学"); DefaultMutableTreeNode t2=new Default- MutableTreeNode("研究生同学"); DefaultMutableTreeNode tl l=new Default- MutatleTreeNode("陈艳"); DefaultMutableTreeNode tl 2=new Default- MutableTreeNode("李小永"); DefaultMutableTreeNode t2 1=new Defauh- MutableTreeNode("王小小"); DefauhMutableTreeNode t2 2=new Defauh- MutableTreeNode("董小"); setTitle("java2"); root.add(t1);root.add(t2); tl.add(t1_1);tl.add(t1_2);t2.add(t2_1);t2. add(t2_2); tree=new JTree(root); JSerollPane scrollpane=new JScrollPane(text); JSplitPane splitpane=new JSplitPane(JSplitPane. HORIZONTAL SPLIT, true,tree,scrollpane); tree.addTreeSeleetionListener(this); con.add(splitpane); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);}}); setVisible(true);setBounds(70,80,200,300); } public void valueChanged(TreeSelectionEvent e) {if(e.getSouree()= =tree) {DefauhMutableTreeNode node= (DefaultMutableTreeNode)tree.getLastSelected- PathComponent(); if(node.isLeaf()) (String str= ; if(str.equals("陈艳")) (text.setText(str+":联系电话:0411- 4209876");} else if(str.equals("李小永")) {text.setText(str+":联系电话:010- 62789876");} else if(str.equals("王小小")) {text.setText(str+":联系电话:0430- 63596677");) else if(str.equals("董小")) {text.setText(str+":联系电话:020- 85192789");} } else {text.setText(node.getUserObject().toString ()); } } } } public class java2 {public static void main(String args[]) {Mytree2 win=new Mytree2();win.pack();} }
判断题 住房市场存在政府干预且受社会高度关注,除政府政策调整、社会舆论导向等的影响外,示范效应、从众心理等也会影响购买者决策,出现羊群效应等非理性购买行为。( )
问答题 本题中定义了长度为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){} }
判断题 有这样一列数字: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); } }