Advertisement
jdalbey

RotatableWord homework

Jan 27th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package WordModelView;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. /** RotatableWord represents a single word that can be "rotated"
  7.  *  left or right.
  8.  */
  9. public class RotatableWord extends JFrame implements ActionListener
  10. {
  11.    public static void main(String [] args)
  12.    {
  13.        RotatableWord app = new RotatableWord("exhilaration");
  14.        
  15.    }
  16.  
  17.     private JLabel wordLabel = new JLabel();
  18.     private JButton left;
  19.     private JButton right;    
  20.     private String theWord;
  21.    
  22.     public RotatableWord(String letters)
  23.     {
  24.         this.theWord = letters;
  25.         wordLabel.setText(theWord);
  26.         getContentPane().add(wordLabel);
  27.         left = new JButton("<");
  28.         right = new JButton(">");
  29.         left.addActionListener(this);
  30.         right.addActionListener(this);
  31.         setLayout(new FlowLayout());
  32.         getContentPane().add(left);
  33.         getContentPane().add(right);      
  34.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  35.         setLocation(0,100);
  36.         pack();
  37.         setVisible(true);
  38.     }      
  39.  
  40.     public void actionPerformed(ActionEvent evt)
  41.     {
  42.         if (evt.getSource() == left)
  43.         {
  44.            goLeft();
  45.         }
  46.         if (evt.getSource() == right)
  47.         {
  48.            goRight();
  49.         }
  50.         // display the updated word
  51.         wordLabel.setText(theWord);
  52.     }
  53.  
  54.  
  55.     /** Rotate the word to the left */
  56.     public void goLeft()
  57.     {
  58.         theWord = theWord.substring(1) + theWord.charAt(0);
  59.     }
  60.     /** Rotate the word to the right */
  61.     public void goRight()
  62.     {
  63.         theWord = theWord.charAt(theWord.length()-1) + theWord.substring(0,theWord.length()-1);
  64.     }  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement