|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +//import java.beans.*; |
| 4 | +import javax.swing.*; |
| 5 | +import javax.swing.border.*; |
| 6 | +import javax.swing.event.*; |
| 7 | + |
| 8 | +public class MarqueePanelDemo extends JPanel |
| 9 | + implements ActionListener, ChangeListener, ItemListener |
| 10 | +{ |
| 11 | + private MarqueePanel marquee; |
| 12 | + private JSpinner scrollFrequency; |
| 13 | + private JSpinner scrollAmount; |
| 14 | + private JSpinner preferredWidth; |
| 15 | + private JSpinner wrapAmount; |
| 16 | + |
| 17 | + MarqueePanelDemo() |
| 18 | + { |
| 19 | + setLayout( new BorderLayout(10, 10) ); |
| 20 | + setBorder( new EmptyBorder(10, 10, 10, 10) ); |
| 21 | + setBackground( Color.ORANGE ); |
| 22 | + |
| 23 | + JComponent north = createNorthPanel(); |
| 24 | + JComponent center = createCenterPanel(); |
| 25 | + JComponent south = createSouthPanel(); |
| 26 | + |
| 27 | + add(north, BorderLayout.NORTH); |
| 28 | + add(center, BorderLayout.CENTER); |
| 29 | + add(south, BorderLayout.SOUTH); |
| 30 | + } |
| 31 | + |
| 32 | + private JComponent createNorthPanel() |
| 33 | + { |
| 34 | + marquee = new MarqueePanel(); |
| 35 | + marquee.setPreferredWidth( 199 ); |
| 36 | + marquee.setOpaque(false); |
| 37 | + |
| 38 | + JLabel label1 = new JLabel(); |
| 39 | + label1.setText("Here is a long string of text."); |
| 40 | + Font f = (new Font("SansSerif", Font.PLAIN, 24)); |
| 41 | + label1.setFont( f ); |
| 42 | + marquee.add( label1 ); |
| 43 | + |
| 44 | + // add an image here |
| 45 | + |
| 46 | + String path = "dukeWaveRed.gif"; |
| 47 | + java.net.URL imgURL = getClass().getResource(path); |
| 48 | + ImageIcon duke = new ImageIcon(imgURL, path); |
| 49 | + JLabel label2 = new JLabel( duke ); |
| 50 | + marquee.add( label2 ); |
| 51 | + |
| 52 | + JPanel panel = new JPanel(); |
| 53 | + panel.setOpaque(false); |
| 54 | + panel.add( marquee ); |
| 55 | + return panel; |
| 56 | + } |
| 57 | + |
| 58 | + public JPanel createCenterPanel() |
| 59 | + { |
| 60 | + RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS); |
| 61 | + rl.setAlignment(RelativeLayout.LEADING); |
| 62 | + rl.setFill( true ); |
| 63 | + JPanel main = new JPanel( rl ); |
| 64 | + |
| 65 | + JPanel insets = new JPanel( new GridLayout(0, 2, 5, 5) ); |
| 66 | + scrollFrequency = new JSpinner(new SpinnerNumberModel(5, 1, 1000 ,2)); |
| 67 | + scrollFrequency.addChangeListener( this ); |
| 68 | + insets.add( scrollFrequency ); |
| 69 | + insets.add( new JLabel("Scroll Frequency") ); |
| 70 | + scrollAmount = new JSpinner(new SpinnerNumberModel(5, 1, 10 ,1)); |
| 71 | + scrollAmount.addChangeListener( this ); |
| 72 | + insets.add( scrollAmount ); |
| 73 | + insets.add( new JLabel("Scroll Amount") ); |
| 74 | + preferredWidth = new JSpinner(new SpinnerNumberModel(199, -1, 399 ,50)); |
| 75 | + preferredWidth.addChangeListener( this ); |
| 76 | + insets.add( preferredWidth ); |
| 77 | + insets.add( new JLabel("Preferred Width") ); |
| 78 | + main.add( insets ); |
| 79 | + |
| 80 | + JCheckBox wrap = new JCheckBox("Wrap"); |
| 81 | + wrap.setSelected( false ); |
| 82 | + wrap.addItemListener( this ); |
| 83 | + main.add( wrap ); |
| 84 | + |
| 85 | + JPanel wrapPanel = new JPanel( new GridLayout(0, 2, 5, 5) ); |
| 86 | + wrapAmount = new JSpinner(new SpinnerNumberModel(50, 0, 500 ,25)); |
| 87 | + wrapAmount.setEnabled( false ); |
| 88 | + wrapAmount.addChangeListener( this ); |
| 89 | + wrapPanel.add( wrapAmount ); |
| 90 | + wrapPanel.add( new JLabel("Wrap Amount") ); |
| 91 | + main.add( wrapPanel ); |
| 92 | + |
| 93 | + JCheckBox scrollWhenFocused = new JCheckBox("Scroll When Focused"); |
| 94 | + scrollWhenFocused.setSelected( true ); |
| 95 | + scrollWhenFocused.addItemListener( this ); |
| 96 | + main.add( scrollWhenFocused ); |
| 97 | + |
| 98 | + JPanel panel = new JPanel(); |
| 99 | + panel.add( main ); |
| 100 | + |
| 101 | + return panel; |
| 102 | + } |
| 103 | + |
| 104 | + private JComponent createSouthPanel() |
| 105 | + { |
| 106 | + JPanel panel = new JPanel(); |
| 107 | + |
| 108 | + JButton start = new JButton( "Start" ); |
| 109 | + start.addActionListener( this ); |
| 110 | + panel.add( start ); |
| 111 | + |
| 112 | + JButton stop = new JButton( "Stop" ); |
| 113 | + stop.addActionListener( this ); |
| 114 | + panel.add( stop ); |
| 115 | + |
| 116 | + JButton pause = new JButton( "Pause" ); |
| 117 | + pause.addActionListener( this ); |
| 118 | + panel.add( pause ); |
| 119 | + |
| 120 | + JButton resume = new JButton( "Resume" ); |
| 121 | + resume.addActionListener( this ); |
| 122 | + panel.add( resume ); |
| 123 | + |
| 124 | + return panel; |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + public void actionPerformed(ActionEvent e) |
| 129 | + { |
| 130 | + String command = e.getActionCommand(); |
| 131 | + |
| 132 | + if ("Start".equals( command ) ) |
| 133 | + marquee.startScrolling(); |
| 134 | + |
| 135 | + if ("Stop".equals( command ) ) |
| 136 | + marquee.stopScrolling(); |
| 137 | + |
| 138 | + if ("Pause".equals( command ) ) |
| 139 | + marquee.pauseScrolling(); |
| 140 | + |
| 141 | + if ("Resume".equals( command ) ) |
| 142 | + marquee.resumeScrolling(); |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + public void itemStateChanged(ItemEvent e) |
| 147 | + { |
| 148 | + JCheckBox checkBox = (JCheckBox)e.getSource(); |
| 149 | + String command = checkBox.getText(); |
| 150 | + |
| 151 | + if ("Wrap".equals(command)) |
| 152 | + { |
| 153 | + marquee.setWrap( checkBox.isSelected() ); |
| 154 | + wrapAmount.setEnabled( checkBox.isSelected() ); |
| 155 | + } |
| 156 | + else |
| 157 | + marquee.setScrollWhenFocused( checkBox.isSelected() ); |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + public void stateChanged(ChangeEvent e) |
| 162 | + { |
| 163 | + marquee.setScrollFrequency( (Integer)scrollFrequency.getValue() ); |
| 164 | + marquee.setScrollAmount( (Integer)scrollAmount.getValue() ); |
| 165 | + marquee.setPreferredWidth( (Integer)preferredWidth.getValue() ); |
| 166 | + marquee.setWrapAmount( (Integer)wrapAmount.getValue() ); |
| 167 | + } |
| 168 | + |
| 169 | + public static void main(String[] args) |
| 170 | + { |
| 171 | + SwingUtilities.invokeLater(new Runnable() { |
| 172 | + public void run() { |
| 173 | + createAndShowGUI(); |
| 174 | + } |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + public static void createAndShowGUI() |
| 179 | + { |
| 180 | + JFrame frame = new JFrame("Marquee Panel"); |
| 181 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 182 | + frame.add( new MarqueePanelDemo() ); |
| 183 | + frame.pack(); |
| 184 | + frame.setLocationRelativeTo(null); |
| 185 | + frame.setVisible(true); |
| 186 | + } |
| 187 | +} |
0 commit comments