12.3.3 指定同一层中组件的位置
例12-7 同一层中放置组件的缺省方式
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
private JLayeredPane lp = new JLayeredPane();
private Integer[] layers = {
JLayeredPane.FRAME_CONTENT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.PALETTE_LAYER,
JLayeredPane.MODAL_LAYER,
JLayeredPane.POPUP_LAYER,
JLayeredPane.DRAG_LAYER,
};
private final Component[] comps = {
new JButton("Frame Content"), new JButton("Default"),
new JButton("default 1"), new JButton("default 2"),
new JButton("Palette"), new JButton("Modal"),
new JButton("Popup"), new JButton("Drag"),
};
public void init() {
setContentPane(lp);
lp.setLayout(null);
for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];
lp.setLayer(button, layers[i].intValue());
lp.add(button);
}
for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];
String t = button.getText();
String replacement = new String("Layer: ");
replacement += t + "(" + lp.getLayer(button) + "),";
replacement += " Index: " + lp.getIndexOf(button);
replacement += " Position: " +
lp.getPosition(button);
button.setText(replacement);
button.setBounds(i*50, i*50, 350, 75);
}
}
}
例12-8 显式地设置同层中组件的位置
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
private JLayeredPane lp = new JLayeredPane();
private Integer[] layers = {
JLayeredPane.FRAME_CONTENT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.PALETTE_LAYER,
JLayeredPane.MODAL_LAYER,
JLayeredPane.POPUP_LAYER,
JLayeredPane.DRAG_LAYER,
};
private final Component[] comps = {
new JButton("Frame Content"), new JButton("Default"),
new JButton("default 1"), new JButton("default 2"),
new JButton("Palette"), new JButton("Modal"),
new JButton("Popup"), new JButton("Drag"),
};
public void init() {
setContentPane(lp);
lp.setLayout(null);
for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];
String t = button.getText();
lp.setLayer(button, layers[i].intValue());
lp.add(button);
}
for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];
String t = button.getText();
String replacement = new String("Layer: ");
if(t.equals("Default"))
lp.setPosition(button, 2);
else if(t.equals("default 2"))
lp.setPosition(button, 0);
replacement += t + "(" + lp.getLayer(button) + "),";
replacement += " Index: " + lp.getIndexOf(button);
replacement += " Position: " +
lp.getPosition(button);
button.setText(replacement);
button.setBounds(i*50, i*50, 350, 75);
}
}
}
12.3.4 使用拖动层
例12-9 使用拖动层
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public void init() {
setContentPane(new CustomContentPane());
}
}
class CustomContentPane extends JLayeredPane {
private ImageIcon rain = new ImageIcon("rain.gif");
private ImageIcon punch = new ImageIcon("punch.gif");
private ImageIcon skelly = new ImageIcon("skelly.gif");
private int rainw = rain.getIconWidth();
private int rainh = rain.getIconHeight();
private JLabel[] labels = {
new JLabel("I stay put", punch, SwingConstants.RIGHT),
new JLabel("Drag me around!",
skelly, SwingConstants.RIGHT),
};
public CustomContentPane() {
Dragger listener = new Dragger();
JCheckBox onDragLayer = new JCheckBox("Drag Layer");
// JLayeredPane has a null layout by default
setLayout(new FlowLayout());
onDragLayer.setOpaque(false);
add(onDragLayer);
add(labels[0]);
add(labels[1]);
labels[1].addMouseMotionListener(listener);
labels[1].addMouseListener(listener);
setLabelText();
onDragLayer.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
setLayer(labels[1],
JLayeredPane.DRAG_LAYER.intValue());
}
else {
setLayer(labels[1],
JLayeredPane.DEFAULT_LAYER.intValue());
}
setLabelText();
validate();
}
});
}
public void paintComponent(Graphics g) {
Dimension size = getSize();
for(int row=0; row < size.height; row += rainh)
for(int col=0; col < size.width; col += rainw)
rain.paintIcon(this,g,col,row);
}
private void setLabelText() {
for(int i=0; i < labels.length; ++i) {
JLabel label = labels[i];
String t = new String("Layer: ");
t += "(" + getLayer(label) + "),";
t += " Index: " + getIndexOf(label);
label.setText(t);
}
}
}
class Dragger extends MouseAdapter
implements MouseMotionListener {
Point press = new Point();
boolean dragging = false;
public void mousePressed(MouseEvent event) {
press.x = event.getX();
press.y = event.getY();
dragging = true;
}
public boolean isDragging() {
return dragging;
}
public void mouseReleased(MouseEvent event) {
dragging = false;
}
public void mouseClicked(MouseEvent event) {
dragging = false;
}
public void mouseMoved(MouseEvent event) {
// don't care
}
public void mouseDragged(MouseEvent event) {
Component c = (Component)event.getSource();
if(dragging) {
Point loc = c.getLocation();
Point pt = new Point();
pt.x = event.getX() + loc.x - press.x;
pt.y = event.getY() + loc.y - press.y;
c.setLocation(pt.x, pt.y);
c.getParent().repaint();
}
}
}
12.3.5 JLayeredPane属性
12.3.6 JLayeredPane类总结
12.3.7 AWT兼容
12.4 JTablePane
例12-10 JTabbedPane的一个简单实例
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JTabbedPane tp = new JTabbedPane();
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();
panelOne.add(new JButton("button in panel 1"));
panelTwo.add(new JButton("button in panel 2"));
tp.add(panelOne, "Panel One");
tp.addTab("Panel Two",
new ImageIcon("document.gif"),
panelTwo,
"tooltip text");
contentPane.setLayout(new BorderLayout());
contentPane.add(tp);
}
}
12.4.1 选项卡的位置
12.4.2 JTabbedPane的属性
例12-11 为JTabbedPane的实例设置选项卡放置位置
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
private JTabbedPane tp = new JTabbedPane(SwingConstants.BOTTOM);
private JComboBox combo = new JComboBox();
public Test() {
Container contentPane = getContentPane();
JPanel comboPanel = new JPanel();
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();
tp.add(panelOne, "Panel One");
tp.addTab("Panel Two",
new ImageIcon("document.gif"),
panelTwo,
"tooltip text");
combo.addItem("TOP");
combo.addItem("LEFT");
combo.addItem("RIGHT");
combo.addItem("BOTTOM");
setComboValue();
comboPanel.add(new JLabel("Tab Placement:"));
comboPanel.add(combo);
contentPane.setLayout(new BorderLayout());
contentPane.add(comboPanel, BorderLayout.NORTH);
contentPane.add(tp, BorderLayout.CENTER);
combo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JComboBox cb = (JComboBox)e.getSource();
int state = e.getStateChange();
if(state == ItemEvent.SELECTED) {
String s = (String)cb.getSelectedItem();
if(s.equals("TOP"))
tp.setTabPlacement(JTabbedPane.TOP);
else if(s.equals("LEFT"))
tp.setTabPlacement(JTabbedPane.LEFT);
else if(s.equals("RIGHT"))
tp.setTabPlacement(JTabbedPane.RIGHT);
else if(s.equals("BOTTOM"))
tp.setTabPlacement(JTabbedPane.BOTTOM);
tp.validate();
}
}
});
}
private void setComboValue() {
int placement = tp.getTabPlacement();
String selectedItem = null;
switch(placement) {
case JTabbedPane.TOP:
selectedItem = "TOP";
break;
case JTabbedPane.LEFT:
selectedItem = "LEFT";
break;
case JTabbedPane.RIGHT:
selectedItem = "RIGHT";
break;
case JTabbedPane.BOTTOM:
selectedItem = "BOTTOM";
break;
}
combo.setSelectedItem(selectedItem);
}
}
12.4.3 JTabbedPane的事件
例12-12 响应JTabbedPane的变化事件
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JTabbedPane tp = new JTabbedPane();
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();
tp.add(panelOne, "Panel One");
tp.addTab("Panel Two",
new ImageIcon("document.gif"),
panelTwo,
"tooltip text");
contentPane.setLayout(new BorderLayout());
contentPane.add(tp);
tp.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JTabbedPane tabbedPane =
(JTabbedPane)e.getSource();
int index = tabbedPane.getSelectedIndex();
String s = tabbedPane.getTitleAt(index);
showStatus(s + " selected");
}
});
}
}
12.4.4 JTabbedPane类总结
12.5 JSplitPane类
例12-13 运行中的JSplitPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JButton left, right;
JSplitPane sp = new JSplitPane();
ControlPanel cp = new ControlPanel(sp);
contentPane.setLayout(new BorderLayout());
contentPane.add(sp, BorderLayout.CENTER);
contentPane.add(cp, BorderLayout.NORTH);
left = (JButton)sp.getTopComponent();
right = (JButton)sp.getBottomComponent();
System.out.println("left button minimum size: " +
left.getMinimumSize());
System.out.println("right button minimum size: " +
right.getMinimumSize());
}
}
class ControlPanel extends JPanel {
private JSplitPane sp;
public ControlPanel(JSplitPane splitPane) {
sp = splitPane;
JComboBox dividerSize = new JComboBox();
JComboBox orientation = new JComboBox();
JCheckBox continuous = new JCheckBox(
"Continuous Layout");
JCheckBox oneTouch = new JCheckBox(
"One Touch Expandable");
Integer initialSize = new Integer(sp.getDividerSize());
dividerSize.addItem(initialSize.toString());
dividerSize.addItem("10");
dividerSize.addItem("20");
dividerSize.addItem("30");
dividerSize.addItem("40");
orientation.addItem("horizontal");
orientation.addItem("vertical");
int initialOrientation = sp.getOrientation();
if(initialOrientation == JSplitPane.HORIZONTAL_SPLIT)
orientation.setSelectedItem("horizontal");
else
orientation.setSelectedItem("vertical");
boolean initialContinuousLayout = sp.isContinuousLayout();
if(initialContinuousLayout)
continuous.setSelected(true);
add(oneTouch);
add(continuous);
add(new JLabel("Divider Size:"));
add(dividerSize);
add(new JLabel("Orientation:"));
add(orientation);
oneTouch.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED)
sp.setOneTouchExpandable(true);
else
sp.setOneTouchExpandable(false);
}
});
continuous.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED)
sp.setContinuousLayout(true);
else
sp.setContinuousLayout(false);
}
});
dividerSize.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JComboBox combo = (JComboBox)e.getSource();
String s = (String)combo.getSelectedItem();
sp.setDividerSize(Integer.parseInt(s));
}
});
orientation.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JComboBox combo = (JComboBox)e.getSource();
String s = (String)combo.getSelectedItem();
if(s.equals("horizontal"))
sp.setOrientation(
JSplitPane.HORIZONTAL_SPLIT);
else
sp.setOrientation(JSplitPane.VERTICAL_SPLIT);
}
});
}
}
12.5.1 JSplitPane属性
12.5.2 JSplitPane事件
例12-14 监视一个拆分窗格的分隔体的位置
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JSplitPane sp = new JSplitPane();
contentPane.add(sp, BorderLayout.CENTER);
sp.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
if(e.getPropertyName().equals(
JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
JSplitPane jsp = (JSplitPane)e.getSource();
int dl = jsp.getDividerLocation();
JButton lb = (JButton)jsp.getLeftComponent();
JButton rb = (JButton)jsp.getRightComponent();
showStatus("Divider Location: " + dl + " / " +
lb.getText() + ": " + lb.getSize() + " / " +
rb.getText() + ": " + rb.getSize());
}
}
});
}
}
12.5.3 JSplitPane类总结
12.5.4 AWT兼容
12.6 本章回顾