CS335 (Fall 2008) Solution of Homework 4 (20 points) Due: 10/2/08 1) ////////////////////////////////// // Main.java ////////////////////////////////// import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main { public static void main(String[] args) { // Create the frame JFrame app = new JFrame("Summation Program"); // Create all the buttons and textfields final JButton plusButton, equalButton; final JTextField numOne, numTwo, sumField; numOne = new JTextField(6); numTwo = new JTextField(6); sumField = new JTextField(6); plusButton = new JButton(" + "); equalButton = new JButton(" = "); // Create a container and define layout style Container cp = app.getContentPane(); cp.setLayout( new FlowLayout() ); cp.add(numOne, BorderLayout.CENTER); cp.add(plusButton, BorderLayout.CENTER); cp.add(numTwo, BorderLayout.CENTER); cp.add(equalButton, BorderLayout.CENTER); cp.add(sumField, BorderLayout.CENTER); equalButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ int sum, first, second; String First = numOne.getText(); String Second = numTwo.getText(); first = Integer.parseInt(First); second = Integer.parseInt(Second); sum = first + second; String answer = ""+sum; sumField.setText(answer); } } ); app.setSize(500,80); app.setVisible(true); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 2) ///////////////////////////////////////////////////// // File: TextFieldTest.java ///////////////////////////////////////////////////// import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JOptionPane; public class TextFieldTest extends JFrame { // private JTextField text1, text2, text3; // private JPasswordField password; JTextField text1, text2, text3; JPasswordField password; public TextFieldTest( ) { super( "Testing JTextField and JPasswordField" ); Container c = getContentPane( ); c.setLayout( new FlowLayout( ) ); // construct textfield with default sizing text1 = new JTextField( 10 ); c.add( text1 ); // construct textfield with default text text2 = new JTextField( "Enter text here" ); c.add( text2 ); // construct textfield with default text and 20 // visible elements and no event handler text3 = new JTextField( "Uneditable text field", 20 ); text3.setEditable( false ); c.add( text3 ); // construct textfield with default text password = new JPasswordField( "Hidden text" ); c.add( password ); //TextFieldHandler handler = new TextFieldHandler(); text1.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { String s = ""; TextFieldTest tt = new TextFieldTest(); if ( e.getSource() == tt.text1 ) s = "text1: " + e.getActionCommand(); else if ( e.getSource() == tt.text2 ) s = "text2: " + e.getActionCommand(); else if ( e.getSource() == tt.text3 ) s = "text3: " + e.getActionCommand(); else if ( e.getSource() == tt.password ) { JPasswordField pwd = (JPasswordField) e.getSource(); s = "password: " + new String( pwd.getPassword() ); } JOptionPane.showMessageDialog( null, s ); } }); text2.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { String s = ""; TextFieldTest tt = new TextFieldTest(); if ( e.getSource() == tt.text1 ) s = "text1: " + e.getActionCommand(); else if ( e.getSource() == tt.text2 ) s = "text2: " + e.getActionCommand(); else if ( e.getSource() == tt.text3 ) s = "text3: " + e.getActionCommand(); else if ( e.getSource() == tt.password ) { JPasswordField pwd = (JPasswordField) e.getSource(); s = "password: " + new String( pwd.getPassword() ); } JOptionPane.showMessageDialog( null, s ); } }); text3.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { String s = ""; TextFieldTest tt = new TextFieldTest(); if ( e.getSource() == tt.text1 ) s = "text1: " + e.getActionCommand(); else if ( e.getSource() == tt.text2 ) s = "text2: " + e.getActionCommand(); else if ( e.getSource() == tt.text3 ) s = "text3: " + e.getActionCommand(); else if ( e.getSource() == tt.password ) { JPasswordField pwd = (JPasswordField) e.getSource(); s = "password: " + new String( pwd.getPassword() ); } JOptionPane.showMessageDialog( null, s ); } }); password.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { String s = ""; TextFieldTest tt = new TextFieldTest(); if ( e.getSource() == tt.text1 ) s = "text1: " + e.getActionCommand(); else if ( e.getSource() == tt.text2 ) s = "text2: " + e.getActionCommand(); else if ( e.getSource() == tt.text3 ) s = "text3: " + e.getActionCommand(); else if ( e.getSource() == tt.password ) { JPasswordField pwd = (JPasswordField) e.getSource(); s = "password: " + new String( pwd.getPassword() ); } JOptionPane.showMessageDialog( null, s ); } }); setSize( 325, 100 ); show( ); } public static void main( String args[] ) { TextFieldTest app = new TextFieldTest(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }