/** * IfStatementEx * * This program demonstrates if/else structures. * * @author Brad Rippe */ import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Container; import javax.swing.UIManager; public class IfStatementEx { public static void main(String[] args) { /** DON'T BE CONCERNED WITH THIS SECTION **/ JFrame frame = new JFrame( "If/else Demo" ); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { } frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); final JTextArea textArea = new JTextArea( 35, 35 ); JScrollPane scrollPane = new JScrollPane( textArea ); scrollPane.setPreferredSize( new Dimension( 600, 300 ) ); frame.getContentPane().add( scrollPane, BorderLayout.CENTER ); JButton codeButton = new JButton( "Show Code" ); codeButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { textArea.setFont( new Font("Courier", Font.BOLD, 16 ) ); textArea.setText( "If\\Else Statement\n\n" + "if( input.equals( \"T\" ) ) {\n\n" + "\t tA.setText( \"You are correct!\" );\n\n" + "} else if( input.equals( \"F\" ) ) {\n\n" + "\t tA.setText( \"Incorrect, java does expect a true or false value!\" );\n\n" + "} else {\n\n" + "\t tA.setText( \"You have typed an invalid option. Please try again!\" );\n\n" + "}\n" ); } }); frame.getContentPane().add( codeButton, BorderLayout.SOUTH ); frame.pack(); frame.setVisible(true); testIfElse( frame, textArea ); } public static void testIfElse(JFrame frame, JTextArea tA) { String input = JOptionPane.showInputDialog( frame, "If statements expect the conditional statement to evaluate to either true or false.\n" + "Type \"T\" for true and \"F\" for false" ); /***** THIS IS THE SECTION OF INTEREST *****/ if( input.equals( "T" ) ) { tA.setText( "You are correct!" ); } else if( input.equals( "F" ) ) { tA.setText( "Incorrect, java does expect a true or false value!" ); } else { tA.setText( "You have typed an invalid option. Please try again!" ); } } }