Cool Program

Cloning peteranswers.com

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Formatter;
import java.util.regex.*;
import javax.swing.*;

public class Cool extends JFrame implements ActionListener
{
	public JTextField textFieldInput1;
	public JTextField textFieldInput2;
	
	public JButton buttonNew;
	public JLabel jawaban;
	
	private UIManager.LookAndFeelInfo looks[];
	
	private Font font = new Font( "Tahoma", Font.BOLD, 14 );
	
	private String t1 = "";
	private String jawab = "";
	private boolean mode = false;
	
	private String petition = "Charles, please answer my following question:";
	private String petition2 = "Charles, please answer:";
	
	private String error1 = "Charles answers: You don't have any connection to me...";
	private String error2 = "Charles answers: You just made a mistake in your petition...";
	
	public Cool()
	{
		super( "Cool program... :)" );
		
		// Setting tampilan window
		looks = UIManager.getInstalledLookAndFeels();
		setLookAndFeel( 2 );
		
		textFieldInput1 = new JTextField();
		textFieldInput1.setFont( font );
		add( textFieldInput1 );
		
		textFieldInput2 = new JTextField();
		textFieldInput2.setFont( font );
		add( textFieldInput2 );
		
		textFieldInput1.setBounds( 120, 175, 360, 25 );
		textFieldInput2.setBounds( 50, 280, 360, 25 );
		
		buttonNew = new JButton( "New" );
		add( buttonNew );
		buttonNew.setBounds( 50, 350, 60, 25 );
		buttonNew.addActionListener( this );
		
		jawaban = new JLabel( "Charles answers: Test" );
		add( jawaban );
		jawaban.setBounds( 120, 350, 400, 25 );
		
		textFieldInput2.setEnabled( false );
		jawaban.setText( "" );
		t1 = "";
		mode = false;
		jawab = "";
		
		textFieldInput1.addKeyListener (new KeyAdapter() {
			public void keyTyped (KeyEvent ke) 
			{
				char c = ke.getKeyChar();
				
				if( c == '\b' )
				{
					if( t1.length() != 0 )
					{
						t1 = t1.substring( 0, t1.length() - 1 );
						ke.consume();
					}
					
					if( mode == true && jawab.length() > 0 )
					{
						jawab = jawab.substring( 0, jawab.length() - 1 );
					}
				}
				else if( ( c < 32 || c > 126 ) && c != '\n' )
				{
					ke.consume();
				}
				else if( t1.length() == petition.length() )
				{
					mode = false;
					t1 += c;
				}
				else if( c == '.' && t1.length() == 0 )
				{
					t1 += petition.charAt( 0 );
					mode = true;
					ke.consume();
				}
				else if( mode == true )
				{
					t1 += petition.charAt( t1.length() );
					
					if( c == '.' ) mode = false;
					else jawab += c;
					
					ke.consume();
				}
				else if( c == ':' || c == '\n' )
				{
					textFieldInput2.setEnabled( true );
					textFieldInput2.grabFocus();
				}
				else
				{
					t1 += c;
					ke.consume();
				}
				
				textFieldInput1.setText( t1 );
			}
  		}
		);
		
		textFieldInput2.addKeyListener (new KeyAdapter() {
			public void keyTyped (KeyEvent ke) 
			{
				char c = ke.getKeyChar();
				
				if( c == '?' || c == '\n' )
				{
					textFieldInput1.setEnabled( false );
					textFieldInput2.setEnabled( false );
					
					String test = textFieldInput1.getText();
					
					if( !test.equals( petition ) && !test.equals( petition2 ) ) jawaban.setText( error2 );
					else if( jawab.length() == 0 ) jawaban.setText( error1 );
					else jawaban.setText( "Charles answers: " + jawab );
				}
			}
  		}
		);
		
		JLabel label1 = new JLabel( new ImageIcon( getClass().getResource( "Image/image.png" ) ) );
		
		add( label1 );
		label1.setBounds( 0, 0, 550, 435 );
	}

	// untuk set tampilan windows
	public void setLookAndFeel(int i)
	{	
		try
		{
			UIManager.setLookAndFeel( looks[i].getClassName() );
			SwingUtilities.updateComponentTreeUI( this );
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
		
	public void actionPerformed( ActionEvent e )
	{
		if( e.getSource() == buttonNew )
		{
			textFieldInput1.setEnabled( true );
			textFieldInput2.setEnabled( false );
			textFieldInput1.setText( "" );
			textFieldInput2.setText( "" );
			jawaban.setText( "" );
			t1 = "";
			mode = false;
			jawab = "";
			textFieldInput1.grabFocus();
		}
	}
	
	public static void main( String[] args )
	{
		Cool apps = new Cool();
		
		apps.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		apps.setSize( 550, 435 );
		apps.setLocationRelativeTo( null );
		apps.setResizable( false );
		apps.setVisible( true );
	}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: