Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package WordModelView;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- /** RotatableWord represents a single word that can be "rotated"
- * left or right.
- */
- public class RotatableWord extends JFrame implements ActionListener
- {
- public static void main(String [] args)
- {
- RotatableWord app = new RotatableWord("exhilaration");
- }
- private JLabel wordLabel = new JLabel();
- private JButton left;
- private JButton right;
- private String theWord;
- public RotatableWord(String letters)
- {
- this.theWord = letters;
- wordLabel.setText(theWord);
- getContentPane().add(wordLabel);
- left = new JButton("<");
- right = new JButton(">");
- left.addActionListener(this);
- right.addActionListener(this);
- setLayout(new FlowLayout());
- getContentPane().add(left);
- getContentPane().add(right);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setLocation(0,100);
- pack();
- setVisible(true);
- }
- public void actionPerformed(ActionEvent evt)
- {
- if (evt.getSource() == left)
- {
- goLeft();
- }
- if (evt.getSource() == right)
- {
- goRight();
- }
- // display the updated word
- wordLabel.setText(theWord);
- }
- /** Rotate the word to the left */
- public void goLeft()
- {
- theWord = theWord.substring(1) + theWord.charAt(0);
- }
- /** Rotate the word to the right */
- public void goRight()
- {
- theWord = theWord.charAt(theWord.length()-1) + theWord.substring(0,theWord.length()-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement