import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; // for LinkedList public class Ch22_2 // implement a GUI that inputs integer numbers and stores them in a LinkedList { // JButtons allow us to manipulate the LinkedList, with the output displayed in a JTextArea public static void main(String[] args) { JFrame f=new JFrame(); // set up our GUI f.setSize(300,200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); NumberPanel np=new NumberPanel(); f.add(np); f.setVisible(true); } // JPanel inner class which will store our LinkedList (list), have 3 JButtons and a JTextField for input and a JTextArea public static class NumberPanel extends JPanel implements ActionListener // for output { private LinkedList list; // our list of Integer values private JTextArea jta; // for output private JTextField input; // for input // note: JButtons could be declared here if desired public NumberPanel() // set up the GUI { JLabel lab=new JLabel("Enter a number:"); // JLabel to describe what to input input=new JTextField("",10); // create our JTextField, have it implement ActionListener input.addActionListener(this); // to respond when something is typed in followed by jta=new JTextArea(3,20); // our output area, 3 rows, 20 columns jta.setLineWrap(true); // using word wrap jta.setWrapStyleWord(true); JButton sort=new JButton("Sort"); // our JButtons which will also implement ActionListener JButton shuffle=new JButton("Shuffle"); JButton reverse=new JButton("Reverse"); sort.addActionListener(this); shuffle.addActionListener(this); reverse.addActionListener(this); JPanel p1=new JPanel(); // now position them nicely p1.add(lab); p1.add(input); JPanel p2=new JPanel(); p2.add(sort); p2.add(shuffle); p2.add(reverse); JPanel p3=new JPanel(new BorderLayout()); p3.add(p1,BorderLayout.NORTH); p3.add(jta,BorderLayout.CENTER); p3.add(p2,BorderLayout.SOUTH); add(p3); list=new LinkedList<>(); // create our LinkedList, initially empty } private void display() // helper method, only called from actionPerformed { String temp=""; // we will build a new String to output in jta for(Integer temp2:list) // add each Integer in list to it, one at a time temp+=""+temp2+" "; jta.setText(temp); // display this String in jta } public void actionPerformed(ActionEvent e) // invoked if user enters something in input or clicks a JButton { if(e.getSource()==input) // if input, grab the String, make it an Integer, add it to LinkedList { Integer temp=new Integer(Integer.parseInt(input.getText())); input.setText(""); // reset the input box to empty list.add(temp); display(); // call display to display the new version of our list } else if(e.getActionCommand().equals("Sort")) // upon sort, use Collections class to sort list and display it { Collections.sort(list); display(); } else if(e.getActionCommand().equals("Shuffle")) // upon shuffle, use Collections class to shuffle list and display it { Collections.shuffle(list); display(); } else if(e.getActionCommand().equals("Reverse")) // upon reverse, use Collections class to reverse list and display it { Collections.reverse(list); display(); } } } }