2017年计算机二级考试时间:2017年计算机二级考试JAVA练习试题及答案3

副标题:2017年计算机二级考试JAVA练习试题及答案3

时间:2023-09-07 03:13:01 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。


  三、简单应用题(共24分)
  本题的功能是通过按钮来选择窗口显示的风格。窗口
  中有三个按钮:“Metal”、“Motif”和“Windows”,单击任何一
  个按钮,就能将窗口的风格改变为按钮名称所对应的风格。
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  class PlafPanel extends JPanel implements ActionLis-
  tener
  {public ()
  {metaIButton=new JButton("Metal");
  motifButtOn=new J Button("Motif");
  windowsButton=new JButton("Windows");
  add(metalButton);
  add(motifButton);
  add(windowsButton);
  metalButton.addActionListener(this);
  motifButton.addActionListener(this);
  windowsButton.addActionListener(this);
  }
  Dublic void actionPerformed(ActionEvent evt)
  {Object source=evt.getSource();
  String plaf="":
  if(source= =metalButton)
  plaf="javax.swing.plaf.metal.MetalLookAnd-
  Feel";
  else if(source= =motifButton)
  plaf="com.sun.java.swing.plaf.motif.Moti-
  fLookAndFeel";
  else if(source= =windowsButton)
  Dlaf="com.sun.java.swing.plaf.windows.Win-
  dowsLookAndFeel";
  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(O);
  }
  });
  Container contentPane=getContentPane();
  contentPane.add(new PlafPanel());
  }
  }
  public class java2
  {public static void main(String[]args)
  f JFrame frame=new PlafFrame();
  frame.show();
  }


  三、简单应用题
  第1处:PlafPanel
  第2处:plaf
  【解析】第一处由题意可知,类PlafPanel缺少构造函数,所以填PlafPanel。第二处是通过String类型的对象plaf,和UIManager.setLookAndFeel()方法实现显示风格的切换。

2017年计算机二级考试JAVA练习试题及答案3.doc

本文来源:https://www.wddqw.com/LJ5n.html