Membuat Data Diri

6/02/2017 10:08:00 AM Add Comment
Pertama, Siapkan aplikasi untuk menuliskan perintah coding.
Jika menggunakan Sistem Operasi Windows, anda bisa menggunakan Textpad / Notepad kemudian akan di Compile/ Execute dengan Java.
Jika menggunakan Linux, anda bisa menggunakan Geany / Notepadqq kemudian akan di Compile /Execute dengan Java.

Lalu Tulis Perintah Coding seperti ini:

import javax.swing.JOptionPane;
public class DataDiri
{
public static void main( String[] args ){
String name = "";
String alamat = "";
int number1;
int number2;
int number3;
double hasilA;
double hasilB;
double hasilC;
name = JOptionPane.showInputDialog("Please enter your name");
alamat = JOptionPane.showInputDialog("Please enter your address");
number1 = Integer.parseInt(JOptionPane.showInputDialog("Date"));
number2 = Integer.parseInt(JOptionPane.showInputDialog("Month"));
number3 = Integer.parseInt(JOptionPane.showInputDialog("Year"));
hasilA = (2012-number3);
hasilB = (number2-9);
hasilC = (32-number1);
String msg = ("You'r Data" + "\nName : " + name + "\nAddress : " + alamat + "\nAge : "+hasilA+ " Year's "+" more "+hasilB+ " Month "+" and "+hasilC+ " Days ");
JOptionPane.showMessageDialog(null, msg);
}
}

lalu hasil outputnya akan seperti ini : 

Membuat Chat Client

6/02/2017 10:03:00 AM Add Comment
Membuat Chat Client
Pertama, Siapkan aplikasi untuk menuliskan perintah coding.
Jika menggunakan Sistem Operasi Windows, anda bisa menggunakan Textpad / Notepad kemudian akan di Compile/ Execute dengan Java.
Jika menggunakan Linux, anda bisa menggunakan Geany / Notepadqq kemudian akan di Compile /Execute dengan Java.

Lalu Tulis Perintah Coding seperti ini:

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ChatClient extends JFrame{
JLabel lblPesan = new JLabel ("Kirim Pesan:");
TextArea taPesan = new TextArea(4,50);
JLabel lblBlasan = new JLabel ("Dari Teman:");
TextArea taBlasan = new TextArea(4,50);
JButton btnSend = new JButton("Send");
JButton btnOpen = new JButton("Open Connection");
JButton btnClose = new JButton("Close Connection");
Socket con = null;
ObjectOutputStream toServer;
ObjectInputStream fromServer;
String Blasan = null;
String inputIPServer;

public void openConnection(){
try{
inputIPServer=JOptionPane.showInputDialog("Inputkan IP Server");
con = new Socket(InetAddress.getByName(inputIPServer),2000);
toServer = new ObjectOutputStream(con.getOutputStream());
}
catch(EOFException ex){
;
}
catch(IOException io){
System.out.println("IO Exception");
io.printStackTrace();
}
}

public void sendData(){
try{
toServer.writeObject(taPesan.getText());
taPesan.setText("");
taPesan.requestFocus();
}
catch(EOFException ex){
;
}
catch(IOException io){
System.out.println("IO Exception");
io.printStackTrace();
}
}

public void getData(){
try{
fromServer = new ObjectInputStream(con.getInputStream());
Blasan = (String) fromServer.readObject();
taBlasan.setText(Blasan);
}
catch(ClassNotFoundException ex){
System.out.println("Error");
}
catch(EOFException ex){
;
}
catch(IOException io){
System.out.println("IO Exception");
io.printStackTrace();
}
}

public void closeConnection(){
try{
toServer.writeObject("bye");
con.close();
con = null;
}
catch (EOFException ex){
;
}
catch (IOException io){
System.out.println("IO Exception");
io.printStackTrace();
}
}

public ChatClient(){
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(lblPesan);
c.add(taPesan);
c.add(lblBlasan);
c.add(taBlasan);
c.add(btnOpen);
c.add(btnSend);
c.add(btnClose);

btnOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
openConnection();
}
});

btnSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
sendData();
getData();
}
});

btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
closeConnection();
}
});
}

public static void main(String[]args)
{
ChatClient klien = new ChatClient();
klien.setTitle("Chatting - Client");
klien.setLocation(300,300);
klien.setSize(500,200);
klien.setVisible(true);
klien.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
}
}

Program ChatServdr
import java.net.*;

lalu hasil outputnya akan seperti ini : 

Membuat Chat Server

6/02/2017 10:02:00 AM Add Comment
Membuat Chat Server
Pertama, Siapkan aplikasi untuk menuliskan perintah coding.
Jika menggunakan Sistem Operasi Windows, anda bisa menggunakan Textpad / Notepad kemudian akan di Compile/ Execute dengan Java.
Jika menggunakan Linux, anda bisa menggunakan Geany / Notepadqq kemudian akan di Compile /Execute dengan Java.

Lalu Tulis Perintah Coding seperti ini:

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ChatServer extends JFrame{
JLabel lblPesan = new JLabel ("Kirim Pesan:");
TextArea taPesan = new TextArea(4,50);
JLabel lblBlasan = new JLabel ("Dari Teman:");
TextArea taBlasan = new TextArea(4,50);
JButton btnSend = new JButton("Send");
JButton btnClose = new JButton("Close Connection");
ServerSocket sktServer;
Socket conClient;

ObjectInputStream fromClient;
ObjectOutputStream toClient;
String s = null;
Container c;

public void sendData(){
try{
toClient = new ObjectOutputStream(conClient.getOutputStream());
toClient.writeObject(taPesan.getText());
System.out.println(taPesan.getText());
taPesan.requestFocus();
}
catch (EOFException ex){
;
}
catch(NullPointerException npe){
JOptionPane.showMessageDialog(null, "Koneksi Belum Tersambung ! ", "Pesan", JOptionPane.ERROR_MESSAGE);
}
catch (SocketException se){
JOptionPane.showMessageDialog(null, "Koneksi Putus !", "Pesan",JOptionPane.ERROR_MESSAGE);
}
catch (IOException io){
System.out.println("IO Exception");
io.printStackTrace();
}
}

public void closeConnection(){
try{
conClient.close();
conClient = null;
System.exit(0);
}
catch (EOFException ex){
;
}
catch (IOException io){
System.out.println("IO Exception");
io.printStackTrace();
}
}

public ChatServer()throws IOException{
c = getContentPane();
c.setLayout(new FlowLayout());
c.add(lblPesan);
c.add(taPesan);
c.add(lblBlasan);
c.add(taBlasan);
c.add(btnSend);
c.add(btnClose);

btnSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
sendData();
}
});

btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
closeConnection();
}
});
}

public void terimaKoneksi() throws IOException{
sktServer = new ServerSocket(2000);
conClient = sktServer.accept();
JOptionPane.showMessageDialog(null, "Tersambung dengan Client" + conClient.getInetAddress().toString(), "Pesan", JOptionPane.INFORMATION_MESSAGE);
sktServer.close();

try{
fromClient = new ObjectInputStream(conClient.getInputStream());
do{
try{
s=(String) fromClient.readObject();
taBlasan.setText(s);
}
catch(ClassNotFoundException ex){
System.out.println("Error");
}
}
while(!s.equals("bye"));
}
catch (EOFException ex){
;
}
catch (IOException io){
System.out.println("IO Exception");
io.printStackTrace();
}
finally{
System.out.println("Closed");
conClient.close();
}
}

{
ChatServer svr = new ChatServer();
svr.setTitle("Chatting - Server");
svr.setLocation(300,300);
svr.setSize(500,250);
svr.setVisible(true);
svr.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
svr.terimaKoneksi();
}
}