/** * Modifies WordGameB by adding colors, borders * and centering the text. * Author: Anthony Larrain * */ import java.awt.*; import java.awt.event.*; import javax.swing.*; class WordGameB extends JFrame implements ActionListener { private JTextField jtfGuess = new JTextField(); private JButton jbtPlay = new JButton("Play"); private JLabel jlShow = new JLabel(); private JLabel jlAnother = new JLabel("Press for another word"); private JPanel jpSouth = new JPanel(); private WordList list = new WordList(); public WordGameB(){ setTitle( "Word Scramble Game" ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setSize(500,200 ); Container content = getContentPane(); content.setBackground(Color.yellow); //Setting the text that goes on the label to the center of the label. //SwingConstants is an interface that contains a collection of constants. jlShow.setHorizontalAlignment(SwingConstants.CENTER); //All JComponents can have borders. Class BorderFactory contains a bunch // of static methods that create different border styles. jlShow.setBorder(BorderFactory.createLineBorder(Color.blue,5)); // Setting the labels font jlShow.setFont( new Font("Comic Sans MS", Font.BOLD, 20) ); // Setting the collor of the Text; jlShow.setForeground(Color.red); // Setting layout manager of the Panel jpSouth.setLayout( new FlowLayout() ); // The JPanel is a container so we can add components to it jpSouth.add(jlAnother); jpSouth.add(jbtPlay); //Default layout manager for JFrame is BorderLayout. content.add( jtfGuess, BorderLayout.NORTH ); // Adding the Jlabel referenced by jlShow to the center of the content pane. content.add(jlShow, BorderLayout.CENTER); // Adding the panel South content.add( jpSouth, BorderLayout.SOUTH); jbtPlay.addActionListener(this); jtfGuess.addActionListener(this); // Select an initial word. list.pickWord(); jlShow.setText( list.scramble() ); } public void actionPerformed(ActionEvent evt){ Object eventSource = evt.getSource(); if(eventSource == jtfGuess){ String token = jtfGuess.getText(); token = token.trim().toLowerCase(); if ( token.equals(list.getWord())) jlShow.setText(" Correct "); else jlShow.setText(" Sorry incorrect ! Word is " + list.getWord()); jtfGuess.setText(""); } else{ list.pickWord(); jlShow.setText( list.scramble()); } } public static void main( String args[] ){ WordGameB frame = new WordGameB(); frame.setVisible(true); } }