计算机二级JAVA-98_第1页
计算机二级JAVA-98_第2页
计算机二级JAVA-98_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

1、计算机二级JAVA-98(总分:100.00,做题时间:90分钟)一、综合应用题(总题数:3,分数:100.00)按钮改变背景颜色,1. 本程序的功能是定义了自己的组件类。窗口中排布着12个按钮,鼠标移到按钮上时,用鼠标单击按钮时,后台将显示该按钮对应的字符,如图所示。class exam_77 extends Frame String keys="123456789*0#"exam_77() super("exam_77"); addWindowListener(new WindowAdapter() public void windowClosing

2、(WindowEvent e) System.exit(0););setLayout(new GridLayout(4, 3, 6, 6); for ( int i=0; i< keys .l ength; i+ ) KeyButton kb=new KeyButton(keys.charAt(i); kb.addKeyListener(this); kb.setBackground(Color.pink); kb.setForeground(Color.black);add(kb);setSize(200, 200);show();public static void main(Str

3、ing args) new exam_77();class KeyButton extends Component KeyListener keyListener;boolean highlighted;char key;KeyButton(char k) this.key=k; addMouseListener(new MouseEventHandler(); public void paint(Graphics g) int w=getSize().width;int h=getSize().height;String s=""+key;FontMetrics fm=g

4、.getFontMetrics();if (highlighted) g.setColor(getBackground();g.fillRoundRect(O, 0, w-1, h-1, 10, 10);g.setColor(getForeground();g.drawRoundRect(0, 0, w-1, h-1, 10, 10);g.drawString(s, (w-fm.stringWidth(s)/2, (h-fm.getHeight()/2+fm.getAscent();class MouseEventHandler extends MouseAdapter public void

5、 mousePressed(MouseEvent evt) if (keyListener != null) keyListener.keyTyped(new KeyEvent(KeyButton.this, KeyEvent.KEY_TYPED,System.currentTimeMillis(),0, KeyEvent.VK_UNDEFINED, key);public void mouseEntered(MouseEvent evt) highlighted=true;repaint();public void mouseExited(MouseEvent evt) highlighte

6、d=false;repaint();public synchronized void addKeyListener(KeyListener l) keyListener=AWTEventMulticaster.add(keyListener, l);public synchronized void removeKeyListener(KeyListener l) keyListener=AWTEventMulticaster.remove(keyListener, l);(分数:33.00 ) 正确答案:()解析:第 1 处:int i=0; i < keys.length(); i+第

7、 2 处:kb.addKeyListener(new KeyEventHandler()第 3 处:2. 本程序的功能是监听鼠标在哪个组件上方。窗口中有四个按钮和一个文本标签,当鼠标移到其中一个按 钮上面时,文本标签将显示在按钮上显示的对应提示信息,如图所示。class exam_78 extends Frame Label statusbar=new Label("Ready");exam_78() super("exam_78");addWindowListener(new WindowAdapter() public void windowClos

8、ing(WindowEvent e) System.exit(O););MainButton b;mhandler=new MouseEventHandler();Panel toolbar=new Panel(new FlowLayout();b=new MainButton("Create", "Create a new document.");b.addMouseListener(mhandler);toolbar.add(b);b=new MainButton("Open", "Open an existing do

9、cument."); b.addMouseListener(mhandler);toolbar.add(b);b=new MainButton("Save", "Save the current document."); b.addMouseListener(mhandler);toolbar.add(b);b=new MainButton("Close", "Close the current document.");b.addMouseListener(mhandler);toolbar.add(b)

10、;add(toolbar, BorderLayout.CENTER);add(statusbar, BorderLayout.SOUTH);pack();show();class MouseEventHandler extends MouseAdapter public void mouseOvered(MouseEvent evt) statusbar.setText(MainButton)evt.getSource().getDescription();public void mouseExited(MouseEvent evt) statusbar.setText(null);publi

11、c static void main(String args) new exam_78();class MainButton extends Button String desc;MainButton(String label) super(label); this.desc=desc;String getDescription() return desc;(分数:33.00 ) 正确答案:()解析:第 1 处: MouseEventHandler mhandler = new MouseEventHandler()第 2 处:public void mouseEntered(MouseEve

12、nt evt)第 3 处:MainButton(String label, String desc)3. 本程序的功能是监听鼠标在窗口中的位置和操作。程序窗口背景色为黄色,窗口中有四个方向不同的白 色三角形,当鼠标置于其中一个三角形上时,三角形变为红色,当鼠标按键按下时,三角形变为蓝色,当 鼠标按钮松开或鼠标移开后,颜色恢复原样,如图所示。public class exam_79 extends Frame exam_79() super("exam_79");addWindowListener(new WindowAdapter() public void windowC

13、losing(WindowEvent e) System.exit(0););Component c;setBackground(yellow);GridBagLayout gbl=new GridBagLayout();setLayout(gbl);add(c=new ArrowButton(ArrowButton.NORTH);setConstraints(gbl, c, 1,0);add(c=new ArrowButton(ArrowButton.WEST);setConstraints(gbl, c, 0, 1);add(c=new ArrowButton(ArrowButton.SO

14、UTH);setConstraints(gbl, c, 1,2);add(c=new ArrowButton(ArrowButton.EAST);setConstraints(gbl, c, 2, 1);pack();show();void setConstraints(GridBagLayout gbl, Component comp, int x, int y) GridBagConstraints cbc=new GridBagConstraints();cbc.gridx=x;cbc.gridy=y;cbc.insets=new Insets(2, 2, 2, 2);gbl.setCo

15、nstraints(comp, cbc);public static void main(String args) new exam_79();ArrowButton extends Component implements MouseListener static final int NORTH=0;static final int WEST=1;static final int SOUTH=2;static final int EAST=3;corners=1,0, 0, 0, 0,1, 1,1 ;double tips=.5, 0, 0, .5, .5, 1, 1, .5;int wid

16、th=30;int height=30;int orient;boolean highlight;boolean down;Polygon shape=new Polygon();ArrowButton(int o) orient=o;int o1=(o+1)% corners.length;int o2=(o+2)% corners.length;int o3=(o+3)% corners.length;int w=width-1;int h=height-1;shape.addPoint(int) (tipso0*w), (int) (tipso1*h); shape.addPoint(c

17、ornerso20*w, cornerso21*h); shape.addPoint(cornerso30*w, cornerso31*h); addMouseListener(this);public void paint(Graphics g) if (highlight) if (down) g.setColor(Color.blue); else g.setColor(Color.red); else g.setColor(Color.white);g.fillPolygon(shape);g.setColor(Color.black);g.drawPolygon(shape);pub

18、lic void mouseClicked(MouseEvent evt) public void mousePressed(MouseEvent evt) down=true;repaint();public void mouseReleased(MouseEvent evt) if (down) processActionEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ""+orient);down=false; repaint();public void mouseEntered(MouseEvent evt) highlight=true;repaint();public void mouseExited(MouseEvent evt) highlight=false;repaint();public boolean contains(int x, int y) return shape.contains(x, y);public Dimension getMinimumSize() return new Dimension(width, height);public Dimen

温馨提示

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

评论

0/150

提交评论