|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.awt.event.*; |
| 4 | +import javax.swing.*; |
| 5 | +import javax.swing.border.*; |
| 6 | + |
| 7 | +public class OverlapLayoutDemo |
| 8 | +{ |
| 9 | + public static void main(String[] args) |
| 10 | + { |
| 11 | + SwingUtilities.invokeLater(new Runnable() { |
| 12 | + public void run() { |
| 13 | + createAndShowGUI(); |
| 14 | + } |
| 15 | + }); |
| 16 | + } |
| 17 | + |
| 18 | + public static void createAndShowGUI() |
| 19 | + { |
| 20 | + JFrame frame = new JFrame("Overlap Layout"); |
| 21 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 22 | + frame.add( new DemoPanel() ); |
| 23 | + frame.setSize(600, 340); |
| 24 | + frame.setLocationRelativeTo(null); |
| 25 | + frame.setVisible(true); |
| 26 | + } |
| 27 | + |
| 28 | + static class DemoPanel extends JPanel |
| 29 | + implements ActionListener, MouseListener |
| 30 | + { |
| 31 | + private JScrollPane scrollPane; |
| 32 | + private JRadioButton above; |
| 33 | + private JRadioButton below; |
| 34 | + private JSpinner xOffset; |
| 35 | + private JSpinner yOffset; |
| 36 | + private JSpinner top; |
| 37 | + private JSpinner bottom; |
| 38 | + private JSpinner left; |
| 39 | + private JSpinner right; |
| 40 | + private JCheckBox includeInvisible; |
| 41 | + private ImageIcon duke; |
| 42 | + private OverlapLayout layout; |
| 43 | + |
| 44 | + public DemoPanel() |
| 45 | + { |
| 46 | + String path = "dukeWaveRed.gif"; |
| 47 | + java.net.URL imgURL = getClass().getResource(path); |
| 48 | + duke = new ImageIcon(imgURL, path); |
| 49 | + |
| 50 | + setLayout( new BorderLayout() ); |
| 51 | + |
| 52 | + scrollPane = new JScrollPane(); |
| 53 | + add(scrollPane); |
| 54 | + |
| 55 | + add(createControlPanel(), BorderLayout.EAST); |
| 56 | + |
| 57 | + JButton button = new JButton("Recreate Layout With Cards"); |
| 58 | + button.addActionListener( this ); |
| 59 | + add(button, BorderLayout.SOUTH); |
| 60 | + |
| 61 | + createLayout(); |
| 62 | + } |
| 63 | + |
| 64 | + private JPanel createControlPanel() |
| 65 | + { |
| 66 | + RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS); |
| 67 | + rl.setAlignment(RelativeLayout.LEADING); |
| 68 | + rl.setFill( true ); |
| 69 | + JPanel panel = new JPanel( rl ); |
| 70 | + panel.setBorder( new TitledBorder("Configure Layout") ); |
| 71 | + |
| 72 | + JPanel r1 = new JPanel( new GridLayout(1, 0, 5, 5) ); |
| 73 | + r1.setBorder( new TitledBorder("Overlap:") ); |
| 74 | + ButtonGroup bg1 = new ButtonGroup(); |
| 75 | + above = new JRadioButton("Above"); |
| 76 | + above.setSelected( true ); |
| 77 | + r1.add( above ); |
| 78 | + bg1.add( above ); |
| 79 | + below = new JRadioButton("Below"); |
| 80 | + r1.add( below ); |
| 81 | + bg1.add( below ); |
| 82 | + panel.add( r1 ); |
| 83 | + |
| 84 | + JPanel r2 = new JPanel( new GridLayout(1, 0, 5, 5) ); |
| 85 | + r2.setBorder( new TitledBorder("Overlap Position:") ); |
| 86 | + xOffset = new JSpinner(new SpinnerNumberModel(20, -100, 100 ,5)); |
| 87 | + r2.add( xOffset ); |
| 88 | + r2.add( new JLabel("X") ); |
| 89 | + yOffset = new JSpinner(new SpinnerNumberModel(0, -100, 100 ,5)); |
| 90 | + r2.add( yOffset ); |
| 91 | + r2.add( new JLabel("Y") ); |
| 92 | + panel.add( r2 ); |
| 93 | + |
| 94 | + JPanel r3 = new JPanel( new GridLayout(2, 0, 5, 5) ); |
| 95 | + r3.setBorder( new TitledBorder("Popup Insets:") ); |
| 96 | + top = new JSpinner(new SpinnerNumberModel(20, 0, 100 ,5)); |
| 97 | + r3.add( top ); |
| 98 | + r3.add( new JLabel("Top") ); |
| 99 | + bottom = new JSpinner(new SpinnerNumberModel(0, 0, 100 ,5)); |
| 100 | + r3.add( bottom ); |
| 101 | + r3.add( new JLabel("Bottom") ); |
| 102 | + left = new JSpinner(new SpinnerNumberModel(0, 0, 100 ,5)); |
| 103 | + r3.add( left ); |
| 104 | + r3.add( new JLabel("Left") ); |
| 105 | + right = new JSpinner(new SpinnerNumberModel(0, 0, 100 ,5)); |
| 106 | + r3.add( right ); |
| 107 | + r3.add( new JLabel("Right") ); |
| 108 | + panel.add( r3 ); |
| 109 | + |
| 110 | + includeInvisible = new JCheckBox("Include Invisible"); |
| 111 | + includeInvisible.setSelected( true ); |
| 112 | + panel.add( includeInvisible ); |
| 113 | + |
| 114 | +// panel.add(Box.createGlue(), new Float(1)); |
| 115 | + panel.add(Box.createGlue(), Float.valueOf(1)); |
| 116 | + |
| 117 | + JLabel label = new JLabel("Note: click on card for \"popup\""); |
| 118 | + panel.add( label ); |
| 119 | + |
| 120 | + return panel; |
| 121 | + } |
| 122 | + |
| 123 | + private void createLayout() |
| 124 | + { |
| 125 | + Point overlap = new Point((Integer)xOffset.getValue(), (Integer)yOffset.getValue()); |
| 126 | + layout = new OverlapLayout(overlap, above.isSelected()); |
| 127 | + Insets popupInsets = new Insets( |
| 128 | + (Integer)top.getValue(), (Integer)left.getValue(), (Integer)bottom.getValue(), (Integer)right.getValue()); |
| 129 | + layout.setPopupInsets( popupInsets ); |
| 130 | + layout.setIncludeInvisible( includeInvisible.isSelected() ); |
| 131 | + |
| 132 | + createPanel( layout ); |
| 133 | + } |
| 134 | + |
| 135 | + private void createPanel(OverlapLayout lm) |
| 136 | + { |
| 137 | + JPanel panel = new JPanel(lm); |
| 138 | + panel.setBorder( new EmptyBorder(10, 10, 10, 10) ); |
| 139 | + |
| 140 | + createCard(panel, "1", duke, false) ; |
| 141 | + createCard(panel, "2", duke, false) ; |
| 142 | + createCard(panel, "3", duke, false) ; |
| 143 | + createCard(panel, "4", duke, true) ; |
| 144 | + createCard(panel, "5", duke, false) ; |
| 145 | + |
| 146 | + Component c = panel.getComponent( layout.convertIndex(1) ); |
| 147 | + layout.addLayoutComponent(c, OverlapLayout.POP_UP); |
| 148 | + |
| 149 | + scrollPane.setViewportView(panel); |
| 150 | + } |
| 151 | + |
| 152 | + private void createCard(Container c, String text, Icon icon, boolean invisible) |
| 153 | + { |
| 154 | + JPanel card = createCard(text, icon); |
| 155 | + c.add(card); |
| 156 | + |
| 157 | + if (invisible) |
| 158 | + { |
| 159 | + card.setVisible(false); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public JPanel createCard(String text, Icon icon) |
| 164 | + { |
| 165 | + JPanel card = new JPanel( new BorderLayout() ); |
| 166 | + card.setToolTipText("Duke " + text); |
| 167 | + card.setBackground( Color.WHITE ); |
| 168 | + card.setBorder( new LineBorder(Color.BLACK) ); |
| 169 | + |
| 170 | + JLabel north = new JLabel( text ); |
| 171 | + north.setHorizontalAlignment(JLabel.LEFT); |
| 172 | + JLabel center = new JLabel( icon ); |
| 173 | + JLabel south = new JLabel( text ); |
| 174 | + south.setHorizontalAlignment(JLabel.RIGHT); |
| 175 | + |
| 176 | + card.add(north, BorderLayout.NORTH); |
| 177 | + card.add(center); |
| 178 | + card.add(south, BorderLayout.SOUTH); |
| 179 | + |
| 180 | + card.setName(text); |
| 181 | + card.addMouseListener( this ); |
| 182 | + |
| 183 | + return card; |
| 184 | + } |
| 185 | + |
| 186 | + public void actionPerformed(ActionEvent e) |
| 187 | + { |
| 188 | + createLayout(); |
| 189 | + } |
| 190 | + |
| 191 | + public void mousePressed(MouseEvent e) |
| 192 | + { |
| 193 | + Component c = e.getComponent(); |
| 194 | + Boolean constraint = layout.getConstraints(c); |
| 195 | + |
| 196 | + if (constraint == null || constraint == OverlapLayout.POP_DOWN) |
| 197 | + layout.addLayoutComponent(c, OverlapLayout.POP_UP); |
| 198 | + else |
| 199 | + layout.addLayoutComponent(c, OverlapLayout.POP_DOWN); |
| 200 | + |
| 201 | + ((JComponent)c).revalidate(); |
| 202 | + } |
| 203 | + |
| 204 | + public void mouseMoved(MouseEvent e) {} |
| 205 | + public void mouseClicked(MouseEvent e) {} |
| 206 | + public void mouseEntered(MouseEvent e) {} |
| 207 | + public void mouseExited(MouseEvent e) {} |
| 208 | + public void mouseReleased(MouseEvent e) {} |
| 209 | + } |
| 210 | +} |
0 commit comments