// CS102, U of C, Spring 2006 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.LinkedList; import java.util.Random; /* Note the following differences between this Applet and the Shuffler * GUI we built in class: * - this class extends the JApplet class * - there are all the same GUI components here except for the JFrame mainFrame * - what happened in the GUI constructor now happens in init() * - rather than add the mainPanel to the mainFrame, we add it to "this" * - there is no constructor * - there is no main method * * Please note however that in "all the ways that count" this applet is just * the same as the standalone GUI. * * To include this applet on a web page, put the compiled file ShufflerApplet.class * in the same directory as the following web page: * ... * * */ public class ShufflerApplet extends JApplet implements ActionListener { // GUI components private JButton enterButton; private JButton shuffleButton; private JButton resetButton; private JTextField tf; private JLabel names; private JPanel subPanel; private JPanel mainPanel; // logical private LinkedList ss; public void init() { ss = new LinkedList(); enterButton = new JButton(); enterButton.setText("Enter"); enterButton.addActionListener(this); shuffleButton = new JButton(); shuffleButton.setText("Shuffle"); shuffleButton.addActionListener(this); resetButton = new JButton(); resetButton.setText("Reset"); resetButton.addActionListener(this); tf = new JTextField(); names = new JLabel(); names.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); subPanel = new JPanel(new GridLayout(4,1)); subPanel.add(tf); subPanel.add(enterButton); subPanel.add(shuffleButton); subPanel.add(resetButton); mainPanel = new JPanel(new GridLayout(1,2)); mainPanel.add(subPanel); mainPanel.add(names); this.add(mainPanel); } public void actionPerformed(ActionEvent e) { if (e.getSource() == enterButton) { String s = tf.getText(); if (s.length() > 0) ss.add(s); tf.setText(""); } else if (e.getSource() == shuffleButton) { Random r = new Random(System.currentTimeMillis()); int size = ss.size(); for (int i=0; i(); } String d = ""; for (String s : ss) d += s + "
"; d += ""; names.setText(d); } }