第7章+插入式界面样式.doc
《第7章+插入式界面样式.doc》由会员分享,可在线阅读,更多相关《第7章+插入式界面样式.doc(14页珍藏版)》请在三一文库上搜索。
1、第7章 插入式界面样式插入式界面样式是建立在第3章“Swing组件体系结构”中讨论的组件体系结构基础之上的,即一个组件的界面样式是通过把一个特殊的UI代表插入这个组件来设置的。Swing还提供了一个API来管理界面样式。界面样式的管理包括如下几个方面:定义界面样式、指定当前的界面样式和为当前的界面样式添加附加界面样式等。例如,图7-1示出了一个以不同界面样式运行的小应用程序(顺时针方向,从左上开始分别是Metal、Windows、Motif和Macintoch的界面样式)。这个小应用程序提供了一个组合框,用于为该小应用程序的所有组件选取界面样式。这个小应用程序借助Swing的UIManager
2、类,根据在该组合框中的选择安装界面样式(注:未列出图7-1中示出的小应用程序的代码,例7-3给出了一个改变界面样式的例子)。 7.1 插入式界面样式7.1.1 界面样式结构例7-1 一个多行标签import javax.swing.*;import javax.swing.plaf.BorderUIResource;import java.awt.*;import java.awt.event.*;import java.util.*; public class Test extends JApplet public void init() Container contentPane = ge
3、tContentPane();JLabel label = new JLabel(A long string that is high on verbiage and +low on content.);MultilineLabel multilineLabel = new MultilineLabel(A long string that is high on verbiage and +low on content.); contentPane.setLayout(new BorderLayout(2,10);contentPane.add(label, BorderLayout.NORT
4、H);contentPane.add(multilineLabel, BorderLayout.CENTER);class MultilineLabel extends JTextArea public MultilineLabel(String s) super(s);public void updateUI() super.updateUI();/ turn on wrapping and disable editing and highlightingsetLineWrap(true);setWrapStyleWord(true);setHighlighter(null);setEdit
5、able(false);/ Set the text areas border, colors and font to/ that of a labelLookAndFeel.installBorder(this, Label.border);LookAndFeel.installColorsAndFont(this, Label.background, Label.foreground,Label.font);7.1.2 界面样式缺省值例7-2 修改UI缺省值import java.awt.Color;import javax.swing.*; public class Test exten
6、ds JApplet public void init() UIManager.put(Tree.background, Color.lightGray);UIManager.put(Tree.textBackground, Color.lightGray);/ScrollBar.width is peculiar to Metal L&FUIManager.put(ScrollBar.width, new Integer(25);getContentPane().add(new JScrollPane(new JTree();7.1.3 UI管理器例7-3 变换一个组件树的界面样式impor
7、t javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*; import com.sun.java.swing.plaf.motif.MotifLookAndFeel;import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;import javax.swing.plaf.metal.MetalLookAndFeel;import javax.swing.plaf.ColorUIResource;public class Test exten
8、ds JApplet public void init() Container contentPane = getContentPane();contentPane.add(new ControlPanel(), BorderLayout.NORTH);contentPane.add(new ComponentPanel(), BorderLayout.CENTER);class ComponentPanel extends JPanel public ComponentPanel() JList list;JScrollBar sb;setBorder(BorderFactory.creat
9、eTitledBorder(Components);add(new JCheckBox(check Me);add(new JRadioButton(radio button);add(new JButton(button);add(new JLabel(A Label:);add(new JTextField(text field);add(new JScrollPane(list = new JList(new Object item 1, item 2, item 3,item 4, item 5, item 6,item 7, item 8, item 9,);add(sb = new
10、 JScrollBar(SwingConstants.HORIZONTAL);sb.setPreferredSize(new Dimension(150,17);add(sb = new JScrollBar(SwingConstants.VERTICAL);sb.setPreferredSize(new Dimension(20,175);list.setVisibleRowCount(5);class ControlPanel extends JPanel JCheckBox checkBox = new JCheckBox(UIResource);JRadioButton motifBu
11、tton = new JRadioButton(Motif),windowsButton = new JRadioButton(Windows),metalButton = new JRadioButton(Metal);public ControlPanel() ActionListener listener = new RadioHandler();ButtonGroup group = new ButtonGroup();group.add(motifButton);group.add(windowsButton);group.add(metalButton);motifButton.a
12、ddActionListener(listener);windowsButton.addActionListener(listener);metalButton.addActionListener(listener);add(motifButton);add(windowsButton);add(metalButton);class RadioHandler implements ActionListener public void actionPerformed(ActionEvent e) JRadioButton src = (JRadioButton)e.getSource();try
13、 if(src = motifButton)UIManager.setLookAndFeel(com.sun.java.swing.plaf. +motif.MotifLookAndFeel);else if(src = windowsButton)UIManager.setLookAndFeel(com.sun.java.swing.plaf. +windows.WindowsLookAndFeel);else if(src = metalButton)UIManager.setLookAndFeel(javax.swing.plaf.metal. +MetalLookAndFeel);ca
14、tch(Exception ex) ex.printStackTrace();SwingUtilities.updateComponentTreeUI(getContentPane();7.1.4 UI资源例7-4 UI资源import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*; import com.sun.java.swing.plaf.motif.MotifLookAndFeel;import javax.swing.plaf.metal.MetalLookAndFeel;impo
15、rt javax.swing.plaf.ColorUIResource;public class Test extends JApplet private JButton button = new JButton(App Resource);public void init() Container contentPane = getContentPane();contentPane.add(new ControlPanel(), BorderLayout.NORTH);contentPane.add(button, BorderLayout.CENTER);class ControlPanel
16、 extends JPanel boolean resource = false;JButton colorSetButton = new JButton(Set Color .);JCheckBox checkBox = new JCheckBox(UIResource);JRadioButton motifButton = new JRadioButton(Motif),metalButton = new JRadioButton(Metal);public ControlPanel() ActionListener listener = new RadioHandler();Button
17、Group group = new ButtonGroup();group.add(motifButton);group.add(metalButton);motifButton.addActionListener(listener);metalButton.addActionListener(listener);metalButton.setSelected(true);add(motifButton);add(metalButton);add(Box.createHorizontalStrut(25);add(colorSetButton);add(checkBox);Font butto
18、nFont = button.getFont();button.setFont(new Font(buttonFont.getFamily(),Font.ITALIC, 56);colorSetButton.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) updateButtonColor(););checkBox.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) re
19、source = checkBox.isSelected();updateButtonColor(););private void updateButtonColor() Color c = JColorChooser.showDialog(getContentPane(), / parent componentChoose a Color, / titlegetBackground(); / initial colorif(resource) button.setText(UI Resource);button.setForeground(new ColorUIResource(c);els
20、e button.setText(App Resource);button.setForeground(c);class RadioHandler implements ActionListener public void actionPerformed(ActionEvent e) JRadioButton src = (JRadioButton)e.getSource();try if(src = motifButton)UIManager.setLookAndFeel(com.sun.java.swing.plaf. +motif.MotifLookAndFeel);else if(sr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 插入 界面 样式
链接地址:https://www.31doc.com/p-2534015.html