Anda di halaman 1dari 3

TesTextArea.

java
===================================================================================
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class TesTextArea extends Frame{


TextArea tArea;
Button bSimpan, bMuat, bHapus;
String namaBerkas = "TArea.out";

public static void main(String[] args) {


TesTextArea apl = new TesTextArea();
}
public TesTextArea() {
super ("Tes TextArea");
setSize(350,150);

tArea = new TextArea(


"Silahkan menuliskan teks\n" +
"dan klik Simpan untuk menyimpan",
5, 30);
bSimpan = new Button("Simpan");
bMuat = new Button("Muat");
bHapus = new Button("Hapus");

Panel p1 = new Panel();


p1.add (tArea);

Panel p2 = new Panel();


p2.setLayout(new GridLayout(3,1));
p2.add(bSimpan);
p2.add(bMuat);
p2.add(bHapus);

add("West", p1);
add("East", p2);

bSimpan.addActionListener(
new TesTextArea.PenanganTombol());
bMuat.addActionListener(
new TesTextArea.PenanganTombol());
bHapus.addActionListener(
new TesTextArea.PenanganTombol());

show();

}
class PenanganTombol implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if(s.equals("Simpan")) {
try {
FileWriter berkasKeluaran =
new FileWriter(namaBerkas);
String isiTextArea = tArea.getText();
berkasKeluaran.write(isiTextArea);
berkasKeluaran.flush();
berkasKeluaran.close();
}
catch (IOException i) {
System.err.println(
"Gagal menyimpan berkas");
System.exit(1);
}
tArea.setText("Teks sudah disimpan di berkas \n"+
"Cek dengen mengklik muat");
}
else if (s.equals("Muat")) {
try{
FileReader berkasMasukan =
new FileReader(namaBerkas);
BufferedReader streamMasukan =
new BufferedReader(berkasMasukan);

String teks = " ";


while(true) {
String barisData =
streamMasukan.readLine();
if (barisData == null)
break;

teks = teks + barisData + '\n';


}

berkasMasukan.close();
tArea.setText(teks);
}
catch(IOException i) {
tArea.setText("Berkas tak dapat dibaca");
}
}
else if (s.equals("Hapus")) {
tArea.setText("");
}
}
}
}

===================================================================================
TesCheckBox.Java
===================================================================================
import java.awt.*;
import java.awt.event.*;

public class TesCheckBox extends Frame{


Checkbox cb1,cb2,cb3;
TextArea tArea;

public static void main(String[] args) {


TesCheckBox apl = new TesCheckBox();
}

public TesCheckBox() {
super("Tes Checkbox");
setSize(400,120);

cb1 = new Checkbox("Java");


cb2 = new Checkbox("C++");
cb3 = new Checkbox("PHP");

tArea = new TextArea("",3,35,


TextArea.SCROLLBARS_NONE);
tArea.setEditable(false);

Panel p1 = new Panel();


p1.setLayout(new GridLayout(3,1));
p1.add(cb1);
p1.add(cb2);
p1.add(cb3);

Panel p2 = new Panel();

p2.add(tArea);

add("West", p1);
add("East", p2);

cb1.addItemListener(
new TesCheckBox.PenanganItem());
cb2.addItemListener(
new TesCheckBox.PenanganItem());
cb3.addItemListener(
new TesCheckBox.PenanganItem());

show();
}
public void tampilkanInfo() {
String sCB1, sCB2, sCB3;

sCB1 = "Status Java: " + cb1.getState();


sCB2 = "Status C++: " + cb2.getState();
sCB3 = "Status PHP: " + cb3.getState();

tArea.setText(sCB1 + "\n" + sCB2 +


"\n" + sCB3);
}
class PenanganItem implements ItemListener {
public void itemStateChanged(ItemEvent e) {
Object o = e.getItem();
if(o.equals("Java")) {
tampilkanInfo();
}
else if(o.equals("C++")) {
tampilkanInfo();
}
else if(o.equals("PHP")) {
tampilkanInfo();
}
}
}