Jumat, 12 Desember 2008

Source Code Java penggunaan JFrame

import javax.swing.*;
import java.awt.*;

class SwingDemo
{
JFrame frame;
JPanel panel;
JTextField textfield;
JButton button;
Container contentPane;
void launchFrame()
{
//inisialisasi
frame = new JFrame("My First Swing Application");
panel = new JPanel();
textfield = new JTextField("Default Text");
button = new JButton("Click Me !");
contentPane = frame.getContentPane();
//Menambahkan komponen-komponen ke panel menggunakan FlowLayout sebagai default
panel.add(textfield);
panel.add(button);
//menambahkan komponen-komponen content pane menggunakan BorderLayout
contentPane.add(panel, BorderLayout.CENTER);
frame.pack();
//menyebabkan ukuran frame menjadi dasar pengaturan komponen
frame.setVisible(true);
}

public static void main(String args[])
{
SwingDemo sd = new SwingDemo();
sd.launchFrame();
}
}


Source code diambil dari Java Education Network Indonesia(JENI)

Source Code Java penggunaan Flow Layout, BorderLayout, Panel

import java.awt.*;

class ComplexLayout extends Frame
{
public static void main(String args[])
{
ComplexLayout cl = new ComplexLayout();
Panel panelNorth = new Panel();
Panel panelCenter = new Panel();
Panel panelSouth = new Panel();

//panel utara
//panel menggunakan FlowLayout sebagai default
panelNorth.add(new Button("ONE"));
panelNorth.add(new Button("TWO"));
panelNorth.add(new Button("THREE"));

//panel tengah
panelCenter.setLayout(new GridLayout(4,4));
panelCenter.add(new TextField("1st"));
panelCenter.add(new TextField("2nd"));
panelCenter.add(new TextField("3rd"));
panelCenter.add(new TextField("4th"));

//Panel Selatan
panelSouth.setLayout(new BorderLayout());
panelSouth.add(new Checkbox("Choose Me !"), BorderLayout.CENTER);
panelSouth.add(new Checkbox("I am here !"), BorderLayout.EAST);
panelSouth.add(new Checkbox("Pick Me !"), BorderLayout.WEST);

//menambahkan panel pada kontainer Frame
//Frame menggunakan BorderLayout sebagai frame
cl.add(panelNorth, BorderLayout.NORTH);
cl.add(panelCenter, BorderLayout.CENTER);
cl.add(panelSouth, BorderLayout.SOUTH);
cl.setSize(300,300);
cl.setVisible(true);
}
}

Source code diambil dari Java Education Network Indonesia (JENI).

Surce Code Java mengubah Spasi menjadi underscore

Berikut adalah contoh source code Java mengubah karakter spasi dalam file teks menjadi karakter underscore atau "_"

Selamat mencoba

import java.io.*;


public class UbahFile
{
public UbahFile(String sumber, String tujuan)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(sumber);
}catch(FileNotFoundException ex)
{
System.out.println("File Not Found");
}

try
{
char data;
int temp;
FileOutputStream fos;
fos = new FileOutputStream(tujuan);
do
{
temp = fis.read();
data = (char)temp;
if(temp != -1)
{
try
{
if((char)temp == ' ')
{
data = '_';
fos.write(data);
}else
{
fos.write(data);
}
}catch(FileNotFoundException ex)
{
System.out.println("File Cannot be opened for writing");
}
}
}while(temp != -1);

} catch(IOException ex)
{
System.out.println("Problem for reading of this file");
}
}

public static void main(String args[]) throws IOException
{
System.out.println("What is the name of the source file ?");
String sumber;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sumber = br.readLine();
System.out.println("Enter data to write to "+sumber+"...");
System.out.println("What is the name of the aim file ?");
String tujuan;
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
tujuan = bd.readLine();
System.out.println("Enter data to write to "+tujuan+"...");
new UbahFile(sumber,tujuan);

}
}