三、简单应用题(共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()方法实现显示风格的切换。
正在阅读:
2016年成人英语三级考试重点核心短语一04-24
运输公司的工会工作总结09-03
2019安徽亳州市谯城区选调中小学幼儿园教师公告【210人】04-14
你的健康,我的责任——有关医院宣传标语08-08
上课吃零食检讨书800字范文【三篇】08-05
关于狮子座的专属座右铭08-13
2018年重庆中考数学试题B卷(已公布)06-18
小升初英语作文题目及范文,小升初英语作文:MyHobbies08-24
如何快速融入澳洲移民生活07-14
7年级地理暑期作业下学期答案【五篇】02-01