Happy Birthday To You Yogi Sundana
EditorTeks.java (Pemrograman Berorientasi Objek)
EditorTeks.java
import
java.awt.*;
import
java.awt.event.*;
import
java.io.*;
public
class EditorTeks extends Frame {
int lebar = 400;
int tinggi = 250;
String namaFile = " " ;
TextArea tArea;
MenuItem itemBuka;
MenuItem itemSimpan;
MenuItem itemKeluar;
MenuItem itemPutih;
MenuItem itemAbuabu;
MenuItem itemHitam;
MenuItem itemBiru;
Button panelTombol;
public static void main (String[] args) {
EditorTeks ap1 = new EditorTeks();
}
public EditorTeks() {
super ("Editor Teks Sederhana");
setSize (lebar, tinggi);
Button tombolPerbesar = new
Button("Perbesar");
Button tombolPerkecil = new
Button("Perkecil");
Button tombolSelesai = new
Button("Selesai");
Panel panelTombol = new Panel();
panelTombol.add(tombolPerbesar);
panelTombol.add(tombolPerkecil);
panelTombol.add(tombolSelesai);
add("South", panelTombol);
tombolPerbesar.addActionListener(new
EditorTeks.PenanganTombol());
tombolPerkecil.addActionListener(new
EditorTeks.PenanganTombol());
tombolSelesai.addActionListener(new
EditorTeks.PenanganTombol());
MenuBar menuAp1 = new MenuBar();
setMenuBar (menuAp1);
Menu menuFile = new Menu
("File");
Menu menuWarna = new Menu
("Warna");
Menu menuView = new Menu
("View");
Menu menuHelp = new Menu
("Help");
menuAp1.add (menuFile);
menuAp1.add (menuWarna);
menuAp1.add (menuView);
menuAp1.add (menuHelp);
MenuItem itemGaris;
itemBuka = new MenuItem
("Buka...");
itemSimpan = new MenuItem
("Simpan...");
itemGaris = new MenuItem
("-------");
itemKeluar = new MenuItem
("Keluar");
menuFile.add (itemBuka);
menuFile.add (itemSimpan);
menuFile.add (itemGaris);
menuFile.add (itemKeluar);
Menu submenuWarnaLB = new Menu
("Warna Latar Belakang");
Menu submenuWarnaTeks = new Menu
("Warna Teks");
menuWarna.add (submenuWarnaLB);
menuWarna.add (submenuWarnaTeks);
MenuItem itemPutih = new MenuItem
("Putih");
MenuItem itemAbuabu = new MenuItem
("Abu-abu");
MenuItem itemPink = new MenuItem
("Pink");
submenuWarnaLB.add (itemPutih);
submenuWarnaLB.add (itemAbuabu);
submenuWarnaLB.add (itemPink);
MenuItem itemHitam = new MenuItem
("Hitam");
MenuItem itemBiru = new MenuItem
("Biru");
MenuItem itemCyan = new MenuItem
("Cyan");
MenuItem itemHijau = new MenuItem
("Hijau");
MenuItem itemKuning = new MenuItem
("Kuning");
submenuWarnaTeks.add (itemHitam);
submenuWarnaTeks.add (itemBiru);
submenuWarnaTeks.add (itemCyan);
submenuWarnaTeks.add (itemHijau);
submenuWarnaTeks.add (itemKuning);
tArea = new TextArea (" ");
tArea.setFont (new
Font("monospaced", Font.PLAIN, 12));
add (tArea);
itemBuka.addActionListener (new
EditorTeks.PenanganItemMenu());
itemSimpan.addActionListener (new
EditorTeks.PenanganItemMenu());
itemKeluar.addActionListener (new
EditorTeks.PenanganItemMenu());
itemPutih.addActionListener (new
EditorTeks.PenanganItemMenu());
itemAbuabu.addActionListener (new
EditorTeks.PenanganItemMenu());
itemPink.addActionListener (new
EditorTeks.PenanganItemMenu());
itemHitam.addActionListener (new
EditorTeks.PenanganItemMenu());
itemBiru.addActionListener (new
EditorTeks.PenanganItemMenu());
itemCyan.addActionListener (new
EditorTeks.PenanganItemMenu());
itemHijau.addActionListener (new
EditorTeks.PenanganItemMenu());
itemKuning.addActionListener (new
EditorTeks.PenanganItemMenu());
addWindowListener (
new EditorTeks.PenanganKejadian());
show();
}
class PenanganTombol implements ActionListener
{
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if (s.equals("Perbesar")) {
lebar += 20;
tinggi += 10;
setSize(lebar, tinggi);
}
else
if (s.equals("Perkecil")) {
lebar -= 20;
tinggi -= 10;
setSize(lebar, tinggi);
}
else
if (s.equals("Selesai")) {
System.exit(0);
}
}
}
class PenanganKejadian extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public void buka() {
FileDialog fd = new FileDialog (this,
"Buka Berkas");
fd.show();
this.namaFile = fd.getFile();
String namaDir = fd.getDirectory();
this.namaFile = namaDir + this.namaFile;
if (this.namaFile == null)
return;
FileReader berkasMasukan = null;
String data = " ";
try {
berkasMasukan = new FileReader
(this.namaFile);
BufferedReader streamMasukan = new
BufferedReader (berkasMasukan);
while (true) {
String barisData =
streamMasukan.readLine();
if (barisData == null)
break;
data = data + barisData +
"\n";
}
berkasMasukan.close();
}
catch (IOException i) {
System.err.println (i.getMessage());
}
tArea.setText(data);
}
public void simpan() {
try {
FileWriter berkasKeluaran = new
FileWriter (this.namaFile);
String isiTextArea = tArea.getText();
berkasKeluaran.write (isiTextArea);
berkasKeluaran.write (isiTextArea);
berkasKeluaran.flush();
berkasKeluaran.close();
}
catch (IOException i) {
System.err.println(i.getMessage());
}
}
class PenanganItemMenu implements
ActionListener {
public void actionPerformed (ActionEvent
e) {
String perintah =
e.getActionCommand();
if
(perintah.equals("Buka..."))
buka();
else if
(perintah.equals("Simpan..."))
simpan();
else if
(perintah.equals("Keluar"))
dispose();
else if
(perintah.equals("Putih"))
tArea.setBackground(Color.WHITE);
else if (perintah.equals("Abu-abu"))
tArea.setBackground(Color.GRAY);
else if
(perintah.equals("Pink"))
tArea.setBackground(Color.PINK);
else if
(perintah.equals("Hitam"))
tArea.setForeground(Color.BLACK);
else if (perintah.equals("Biru"))
tArea.setForeground(Color.BLUE);
else if
(perintah.equals("Cyan"))
tArea.setForeground(Color.CYAN);
else if
(perintah.equals("Hijau"))
tArea.setForeground(Color.GREEN);
else if
(perintah.equals("Kuning"))
tArea.setForeground(Color.YELLOW);
}
}
}
Di
Compile dengan ctrl-1
Lalu
ctrl-2 untuk run
Maka
akan tampil
Pilih
file, untuk submenu
Buka,Simpan,
dan Keluar
Jika
kita Klik Buka maka, akan terbuka jendela baru untuk memilih file yang akan
dibuka :
Jika
kita Klik Warna ada sub menu warna latar belakang dan warna teks
Warna
latar belakang kita pilih pink, lalu pilih warna teks
Warna
teks kita pilih biru :
Dan
tombol Perbesar,Perkecil dan Selesai dapat di eksekusi
TesFlowLayout.java (Pemrograman Berorientasi Objek)
TesFlowLayout.java
import java.awt.*;
import java.awt.event.*;
public class TesFlowLayout extends Frame {
public static void main(String[]args) {
TesFlowLayout apl = new TesFlowLayout();
}
public TesFlowLayout() {
super("Tes FlowLayout");
setSize(300,200);
Panel p1 = new Panel ();
p1.setLayout(new FlowLayout());
p1.add(new Button("Tombol A"));
p1.add(new Button("Tombol B"));
p1.add(new Button("Tombol C"));
Panel p2 = new Panel();
p2.setLayout(new FlowLayout ());
p2.add(new Button("Tombol X"));
p2.add(new Button("Tombol Y"));
p2.add(new Button("Tombol Z"));
Panel p3 = new Panel();
Button exit = new Button ("Exit");
p3.setLayout(new FlowLayout
(FlowLayout.CENTER, 35, 35));
p3.add (exit);
exit.addActionListener(new
TesFlowLayout.PenangananTombol());
add("North", p1);
add("South", p2);
add("Center",p3);
addWindowListener(
new
TesFlowLayout.PenangananKejadian());
show();
}
class PenangananKejadian extends
WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class PenangananTombol implements
ActionListener {
public void actionPerformed(ActionEvent e) {
String status =
e.getActionCommand();
if
(status.equals("Exit")) {
System.exit(0);
}
}
}
}
Jika di run maka hasilnya:
TesGridLayout.java (Pemrograman Berorientasi Objek)
TesGridLayout.java
import java.awt.*;
import java.awt.event.*;
public class TesGridLayout extends Frame {
public static void main (String[]args) {
TesGridLayout apl = new TesGridLayout();
}
public TesGridLayout() {
super("Tes FlowLayout");
setSize(400,250);
Panel p1 = new Panel();
p1.setLayout(new GridLayout(2,3));
p1.add(new Button("Tombol A"));
p1.add(new Button("Tombol B"));
p1.add(new Button("Tombol C"));
p1.add(new Button("Tombol D"));
p1.add(new Button("Tombol B"));
add("North", p1);
Panel p2 = new Panel();
p2.setLayout(new GridLayout(3,2 ,10, 5));
p2.add(new Button("Tombol A"));
p2.add(new Button("Tombol B"));
p2.add(new Button("Tombol C"));
p2.add(new Button("Tombol D"));
p2.add(new Button("Tombol E"));
add("South", p2);
Panel p3 = new Panel();
Button exit = new Button
("Exit");
p3.setLayout(new GridLayout(2,2));
p3.add(exit);
add("East",p3);
exit.addActionListener(new
TesGridLayout.PenangananTombol());
addWindowListener(
new
TesGridLayout.PenangananKejadian());
this.setVisible(true);
}
class PenangananKejadian extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class PenangananTombol implements
ActionListener {
public void actionPerformed(ActionEvent e) {
String status =
e.getActionCommand();
if
(status.equals("Exit")) {
System.exit(0);
}
}
}
}
Jika di run maka hasilnya:
Langganan:
Postingan
(
Atom
)
Blog Archive
Diberdayakan oleh Blogger.