《2022年计算机二级考试JAVA练习试题及答案3.docx》由会员分享,可在线阅读,更多相关《2022年计算机二级考试JAVA练习试题及答案3.docx(4页珍藏版)》请在金锄头文库上搜索。
1、 2022年计算机二级考试JAVA练习试题及答案3三、简洁应用题(共24分)此题的功能是通过按钮来选择窗口显示的风格。窗口中有三个按钮:“Metal”、“Motif”和“Windows”,单击任何一个按钮,就能将窗口的风格转变为按钮名称所对应的风格。import javaawt*;import javaawtevent.*;import javaxswing*;class PlafPanel extends JPanel implements ActionLis-tenerpublic ()metaIButton=new JButton(“Metal“);motifButtOn=new J B
2、utton(“Motif“);windowsButton=new JButton(“Windows“);add(metalButton);add(motifButton);add(windowsButton);metalButtonaddActionListener(this);motifButtonaddActionListener(this);windowsButtonaddActionListener(this);Dublic void actionPerformed(ActionEvent evt)Object source=evtgetSource();String plaf=“:i
3、f(source= =metalButton)plaf=“javaxswingplafmetalMetalLookAnd-Feel“;else if(source= =motifButton)plaf=“comsunjavaswingplafmotifMoti-fLookAndFeel“;else if(source= =windowsButton)Dlaf=“comsunjavaswingplafwindowsWin-dowsLookAndFeel“;tryUIManagersetLookAndFeel( );SwingUtilitiesupdateComponentTreeUI(this)
4、;catch(Exception e)private JButton metalButton;private JButton motifButton;private JButton windowsButton;class PlafFrame extends JFramepublic PlafFrame() setTitle(“simple“);setSize(300,200);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(O););Container contentPane=getContentPane();contentPaneadd(new PlafPanel();public class java2public static void main(Stringargs)f JFrame frame=new PlafFrame();frameshow(); 三、简洁应用题第1处:PlafPanel第2处:plaf【解析】第一处由题意可知,类PlafPanel缺少构造函数,所以填PlafPanel。其次处是通过String类型的对象plaf,和UIManagersetLookAndFeel()方法实现显示风格的切换。