`
dongzhumao86
  • 浏览: 16224 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

《Java 2 图形设计卷Ⅱ- SWING》第7章 插入式界面样式

阅读更多

7章 插入式界面样式

  插入式界面样式是建立在第3“Swing组件体系结构中讨论的组件体系结构基础之上的,即一个组件的界面样式是通过把一个特殊的UI代表插入这个组件来设置的。
  Swing还提供了一个API来管理界面样式。界面样式的管理包括如下几个方面:定义界面样式、指定当前的界面样式和为当前的界面样式添加附加界面样式等。例如,图7-1示出了一个以不同界面样式运行的小应用程序(顺时针方向,从左上开始分别是MetalWindowsMotifMacintoch的界面样式)。这个小应用程序提供了一个组合框,用于为该小应用程序的所有组件选取界面样式。这个小应用程序借助SwingUIManager类,根据在该组合框中的选择安装界面样式(注:未列出图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 = getContentPane();
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.NORTH);
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 highlighting

setLineWrap(true);
setWrapStyleWord(true);
setHighlighter(null);
setEditable(false);

// Set the text area's border, colors and font to
// that of a label

LookAndFeel.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 extends JApplet {
public void init() {
UIManager.put("Tree.background", Color.lightGray);
UIManager.put("Tree.textBackground", Color.lightGray);

//ScrollBar.width is peculiar to Metal L&F
UIManager.put("ScrollBar.width", new Integer(25));
getContentPane().add(new JScrollPane(new JTree()));
}
}


7.1.3 UI管理器

 

7-3 变换一个组件树的界面样式


import 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 extends 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.createTitledBorder("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 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 motifButton = 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.addActionListener(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 {
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");
}
catch(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;

import 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 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();
ButtonGroup 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 buttonFont = 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) {
resource = checkBox.isSelected();
updateButtonColor();
}
});
}
private void updateButtonColor() {
Color c = JColorChooser.showDialog(
getContentPane(), // parent component
"Choose a Color", // title
getBackground()); // initial color

if(resource) {
button.setText("UI Resource");
button.setForeground(new ColorUIResource(c));
}
else {
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(src == metalButton)
UIManager.setLookAndFeel(
"javax.swing.plaf.metal." +
"MetalLookAndFeel");
}
catch(Exception ex) {
ex.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(
getContentPane());
}
}
}
}


7.2 Java界面样式

 

7-5 Metal客户属性


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Test extends JApplet {
JDesktopPane desktopPane = new JDesktopPane();

JInternalFrame jif = new JInternalFrame(
"Internal Frame ", // title
true, // resizable
true, // closable
true, // maximizable
true); // iconifiable

JScrollBar scrollbar = new JScrollBar();
JSlider slider = new JSlider();
JToolBar toolbar = new JToolBar();
JTree tree = new JTree();

public void init() {
Container contentPane = getContentPane();


jif.setPreferredSize(new Dimension(550, 450));
jif.getContentPane().setLayout(new FlowLayout());
jif.getContentPane().add(new ComponentPanel());

desktopPane.setLayout(new FlowLayout());
desktopPane.add(jif);

contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(desktopPane, BorderLayout.CENTER);

}
class ComponentPanel extends JPanel {
public ComponentPanel() {
JPanel panel = new JPanel();

setLayout(new BorderLayout());
add(toolbar, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);

panel.add(scrollbar);
panel.add(slider);
panel.add(new JScrollPane(tree));

tree.setPreferredSize(new Dimension(200,100));

toolbar.add(new JButton("button"));
}
}
class ControlPanel extends JPanel {
JCheckBox rollover = new JCheckBox(
"JToolBar.rollover");
JCheckBox palette = new JCheckBox(
"JInternalFrame.palette");
JCheckBox filled = new JCheckBox(
"JSlider.isFilled");
JCheckBox freeStanding = new JCheckBox(
" JScrollBar.freeStanding");

JRadioButton none = new JRadioButton("none");
JRadioButton horizontal = new JRadioButton("horizontal");
JRadioButton angled = new JRadioButton("angled");

public ControlPanel() {
ActionListener checkBoxListener =
new CheckBoxListener();
ActionListener radioButtonListener =
new RadioButtonListener();

palette.addActionListener(checkBoxListener);
filled.addActionListener(checkBoxListener);
rollover.addActionListener(checkBoxListener);
freeStanding.addActionListener(checkBoxListener);

none.addActionListener(radioButtonListener);
horizontal.addActionListener(radioButtonListener);
angled.addActionListener(radioButtonListener);

ButtonGroup group = new ButtonGroup();
group.add(none);
group.add(horizontal);
group.add(filled);

none.setSelected(true);
freeStanding.setSelected(true);

add(palette);
add(filled);
add(rollover);
add(freeStanding);
add(Box.createHorizontalStrut(10));
add(new JLabel("Tree: "));
add(none);
add(horizontal);
add(angled);
}
class RadioButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JRadioButton rb = (JRadioButton)e.getSource();

if(rb == none) {
tree.putClientProperty(
"JTree.lineStyle", "None");
}
if(rb == horizontal) {
tree.putClientProperty(
"JTree.lineStyle", "Horizontal");
}
if(rb == angled) {
tree.putClientProperty(
"JTree.lineStyle", "Angled");
}
tree.repaint();
}
}
class CheckBoxListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JCheckBox cb = (JCheckBox)e.getSource();

if(cb == palette) {
palette.putClientProperty(
"JInternalFrame.isPalette",
new Boolean(cb.isSelected()));

jif.revalidate();
}
else if(cb == filled) {
slider.putClientProperty(
"JSlider.isFilled",
new Boolean(cb.isSelected()));

slider.repaint();
}
else if(cb == rollover) {
toolbar.putClientProperty(
"JToolBar.isRollover",
new Boolean(cb.isSelected()));

toolbar.repaint();
}
else if(cb == freeStanding) {
scrollbar.putClientProperty(
"JScrollBar.isFreeStanding",
new Boolean(cb.isSelected()));

scrollbar.repaint();
}
}
}
}
}


7.2.1客户属性

 

7.2.2主题

7-6 一个简单的定制主题


import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.metal.*;
import javax.swing.plaf.*;

public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();

contentPane.setLayout(new FlowLayout());

setMetalTheme(new ExperimentalTheme());

contentPane.add(new JButton("button"));
contentPane.add(new JCheckBox("check me"));
}
private void setMetalTheme(MetalTheme theme) {
MetalLookAndFeel.setCurrentTheme(new ExperimentalTheme());

// Metal Look and Feel must be (re)loaded for the
// new theme to take effect ...

try {
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
}
catch(IllegalAccessException e1) {}
catch(UnsupportedLookAndFeelException e2) {}
catch(InstantiationException e3) {}
catch(ClassNotFoundException e4) {}
}
}
class ExperimentalTheme extends DefaultMetalTheme {
public FontUIResource getControlTextFont() {
return new FontUIResource("SanSerif",
Font.BOLD + Font.ITALIC, 24);
}
}


 

7-7 安装一个附加界面样式


import java.awt.*;
import javax.swing.*;

public class Test extends JApplet {
public void init() {
UIManager.addAuxiliaryLookAndFeel(
new ExampleAuxiliaryLookAndFeel());

Container contentPane = getContentPane();
JButton button = new JButton("button");

contentPane.setLayout(new FlowLayout());
contentPane.add(button);
}
}


7-8 附加界面样式的例子


import java.awt.*;
import javax.swing.*;

public class ExampleAuxiliaryLookAndFeel extends LookAndFeel {
public String getDescription(){
return "example auxiliary look and feel";
}
public String getID() {
return "example";
}
public String getName() {
return "example auxiliary";
}
public boolean isNativeLookAndFeel() {
return false;
}
public boolean isSupportedLookAndFeel() {
return true;
}
public UIDefaults getDefaults() {
UIDefaults table = new UIDefaults();

Object[] uiDefaults = {
"ButtonUI", "AuxiliaryButtonUI"
};

table.putDefaults(uiDefaults);
return table;
}
}


7-9 一个附加UI


import java.awt.*;
import java.awt.event.*;
import javax.accessibility.*;
import javax.swing.*;
import javax.swing.plaf.*;

public class AuxiliaryButtonUI extends ComponentUI {
private static ComponentUI cui = new AuxiliaryButtonUI();
private static AuxiliaryButtonMouseListener ml =
new AuxiliaryButtonMouseListener();

// must be implemented
public static ComponentUI createUI(JComponent c) {
return cui;
}
public void installUI(JComponent c) {
c.addMouseListener(ml);
}
public void uninstallUI(JComponent c) {
c.removeMouseListener(ml);
}
public void update(Graphics g, JComponent c) {
// don't want ComponentUI default behavior, which is
// to clear the background
}
}
class AuxiliaryButtonMouseListener extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
JComponent c = (JComponent)e.getSource();
AccessibleContext ac = c.getAccessibleContext();
String role = ac.getAccessibleRole().toString();
String name = ac.getAccessibleName();

System.out.println("mouse entered component of type " +
role + " named " + name);
}
}


 

 

7.3 附加UI

 略

7.4 本章回顾

 略

0
6
分享到:
评论

相关推荐

    Java 2 图形设计卷Ⅱ:SWING

    《Java 2 图形设计卷Ⅱ:SWING》是一本深入探讨Java图形用户界面(GUI)编程的专业书籍,主要聚焦于SWING框架。在Java世界中,SWING是用于构建桌面应用程序的重要库,它提供了丰富的组件和功能,使得开发者能够创建...

    Java 2 图形设计卷Ⅱ:SWING.rar

    Java 2 图形设计卷Ⅱ:SWING.rarJava 2 图形设计卷Ⅱ:SWING.rarJava 2 图形设计卷Ⅱ:SWING.rarJava 2 图形设计卷Ⅱ:SWING.rarJava 2 图形设计卷Ⅱ:SWING.rarJava 2 图形设计卷Ⅱ:SWING.rar

    Java 2图形设计 卷Ⅱ:SWING(下).rar

    在Java 2图形设计卷Ⅱ:SWING(下)中,我们可以深入探讨Swing组件、布局管理、事件处理以及Swing的高级特性。 1. **Swing组件**: - JFrame:这是Swing中的顶级容器,通常作为应用程序的主窗口。它包含了JFrame内的...

    Java2图形设计卷Ⅱ:Swing

    资源名称:Java2 图形设计 卷Ⅱ:Swing 内容简介:Swing是一流的Java图形用户界面开发工具。本书详细介绍了Swing的设计思想、体系结构、使用技巧,内容丰富、深入细致、分析透彻。本书用大量实例代码介绍了每个组件...

    Java2图形设计卷II:Swing(第一部分)

    一本比较经典的Swing的书。PDF版本。因为文件太大分为两部分。

    Java图形设计卷2 - swing

    本资源"Java图形设计卷2 - Swing"很可能是对Swing组件、布局管理、事件处理、高级特性以及Swing在实际应用中的实践进行深入讲解的教程或指南。 Swing组件是轻量级的,意味着它们不依赖于操作系统提供的图形接口,...

    【中文】【Java 2 图形设计卷Ⅱ:SWING】【chm】

    根据提供的文件信息,我们可以推断出这是一份关于Java 2 Swing图形用户界面设计的教程或书籍资源。接下来,我们将围绕这份资料展开一系列与Swing相关的知识点介绍。 ### Java 2 Swing简介 Java 2平台提供了强大的...

    Java2图形设计卷II:SWING

    《Java2图形设计卷II:SWING》是深入探讨Java平台上的GUI(图形用户界面)开发的专业书籍,主要聚焦于SWING库的使用。SWING是Java标准版(Java SE)的一部分,为开发者提供了丰富的组件和功能,用于构建美观、交互性...

    Java 2 图形设计卷Ⅱ(Swing)

    本资源"Java 2 图形设计卷Ⅱ(Swing)"显然是深入探讨Swing组件和应用设计的专业教程。下面将详细阐述Swing在Java GUI开发中的重要性、主要组件以及其特性和用法。 Swing是在早期AWT(Abstract Window Toolkit)基础...

    java2D图形设计卷-SWING

    在"Java 2D图形设计卷-SWING"这本书中,你将深入了解到如何利用Java 2D API和SWING库来创建美观且交互性强的图形用户界面。以下是一些关键知识点的详细说明: 1. **Java 2D API**: Java 2D API 提供了对二维图形、...

    Java+2图形设计卷ⅠAWT.chm Java_2_图形设计卷Ⅱ:SWING.chm

    总结来说,Java 2图形设计卷ⅠAWT和卷ⅡSwing这两部分的学习将涵盖从基础的窗口和组件创建到复杂的用户交互和界面设计。理解并熟练掌握这两个库对于开发功能丰富的Java桌面应用至关重要。通过深入学习和实践,开发者...

    Java2图形设计卷II:SWING.part2(共2个)

    Java2图形设计卷II:SWING.part2.rar 有目录,无缺页。

    java_2_图形设计卷Ⅱ:swing.rar

    这个"java_2_图形设计卷Ⅱ:swing.rar"压缩包文件显然包含了深入学习Swing的相关资源,特别是包含了一个名为"Java_2_图形设计卷Ⅱ:SWING.chm"的帮助文档,这通常是一份详细的教程或指南。 在Java Swing中,你可以...

Global site tag (gtag.js) - Google Analytics