日期:2014-05-20 浏览次数:21046 次
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ExpandingPanels extends MouseAdapter {
ActionPanel[] aps;
JPanel[] panels;
public ExpandingPanels() {
assembleActionPanels();
assemblePanels();
}
public void mousePressed(MouseEvent e) {
ActionPanel ap = (ActionPanel) e.getSource();
if (ap.target.contains(e.getPoint())) {
ap.toggleSelection();
togglePanelVisibility(ap);
}
}
private void togglePanelVisibility(ActionPanel ap) {
int index = getPanelIndex(ap);
if (panels[index].isShowing())
panels[index].setVisible(false);
else
panels[index].setVisible(true);
ap.getParent().validate();
}
private int getPanelIndex(ActionPanel ap) {
for (int j = 0; j < aps.length; j++)
if (ap == aps[j])
return j;
return -1;
}
private void assembleActionPanels() {
String[] ids = { "level 1", "level 2", "level 3", "level 4" };
aps = new ActionPanel[ids.length];
for (int j = 0; j < aps.length; j++)
aps[j] = new ActionPanel(ids[j], this);
}
private void assemblePanels() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 1, 2, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
JPanel p1 = new JPanel(new GridBagLayout());
gbc.gridwidth = gbc.RELATIVE;
p1.add(new JButton