import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class AnagramGUI extends JFrame { JTextArea textArea; JTextField stringField; JScrollPane scrollPane; public AnagramGUI () { // Set some parameters of the frame (window) that's brought up. this.setSize (600, 600); this.setTitle ("Anagram Tester"); this.setResizable (true); // This is how stuff is put into a frame. Container cPane = this.getContentPane(); textArea = new JTextArea (); scrollPane = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); cPane.add (scrollPane, BorderLayout.CENTER); // Make the controls. JPanel panel = new JPanel (); JLabel label = new JLabel ("Enter two strings: "); panel.add (label); stringField = new JTextField (30); panel.add (stringField); JButton button = new JButton ("Go"); button.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { handleButtonClick(); } } ); panel.add (button); cPane.add (panel, BorderLayout.SOUTH); this.setVisible (true); } String inputStr; // When the user clicks the button, this method gets called. // It's where we need to respond. void handleButtonClick () { // Extract the string from the textfield where the user typed the strings. inputStr = stringField.getText (); // Break into two strings, separating by space. String[] strings = inputStr.split (" "); // Check if anagrams. boolean yes = areAnagrams (strings[0].toCharArray(), strings[1].toCharArray()); // Prepare output. String outputStr = strings[0] + " and " + strings[1] + " are not anagrams"; if (yes) { outputStr = strings[0] + " and " + strings[1] + " are anagrams"; } // Put the output string in the text box. String text = textArea.getText (); text += outputStr + "\n"; textArea.setText (text); } static boolean areAnagrams (char[] A, char[] B) { if (A.length != B.length) { return false; } // Make a copy. char[] C = new char [B.length]; for (int i=0; i