// purpose: behave like an instant messenger import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class XIMU implements ActionListener { public static void main( String[] args ) { XIMU im = new XIMU(); } IM im; String buddy; TextBox incoming; TextBox outgoing; public static final Color cream = new Color( 255, 255, 220 ); public XIMU() { JFrame window = Window.make( "XIMU", 475, 450, cream, false, false ); this.im = null; this.buddy = null; this.incoming = new TextBox( "Incoming" ); this.incoming.addTo( window ); this.outgoing = new TextBox( "Outgoing" ); this.outgoing.addTo( window ); this.makeButtons( window ); window.setVisible( true ); } public void makeButtons( JFrame w ) { JButton buddy = new JButton( "Buddy" ); JButton login = new JButton( "Login" ); JButton send = new JButton( "Send" ); JButton receive = new JButton( "Receive" ); JButton logout = new JButton( "Logout" ); buddy.addActionListener( this ); login.addActionListener( this ); send.addActionListener( this ); receive.addActionListener( this ); logout.addActionListener( this ); w.add( buddy ); w.add( login ); w.add( send ); w.add( receive ); w.add( logout ); } public void actionPerformed( ActionEvent e ) { JButton button = (JButton) e.getSource(); String name = button.getText(); if ( name.equals( "Login" ) ) { String user = JOptionPane.showInputDialog( null, "Login", "", JOptionPane.QUESTION_MESSAGE ); if ( user != null ) { if ( this.im != null ) { this.im.logout(); } this.im = new IM( user ); } } else if ( name.equals( "Buddy" ) ) { if ( this.im != null ) { ArrayList list = this.im.whoseLoggedOn(); Object[] array = list.toArray(); this.buddy = (String) JOptionPane.showInputDialog( null, "Friend", "", JOptionPane.QUESTION_MESSAGE, null, array, array[0] ); } } else if ( name.equals( "Receive" ) ) { if ( ( this.im != null ) && ( this.buddy != null ) ) { String message = this.im.getMessage( this.buddy ); this.incoming.setText( message ); } } else if ( name.equals( "Send" ) ) { if ( ( this.im != null ) && ( this.buddy != null ) ) { String message = this.outgoing.getText(); im.sendMessage( this.buddy, message ); } } else if ( name.equals( "Logout" ) ) { if ( this.im != null ) { this.im.logout(); System.exit( 1 ); } } } }