1) import javax.swing.*;
import java.awt.*;
public class cthIcon {
public static void main(String[] args) {
JFrame myFrame = new JFrame("Contoh program AWT");
Icon ikon = new ImageIcon("ulos.jpg");
JLabel myLabel = new JLabel("Selamat belajar JAVA", ikon, SwingConstants.CENTER);
JButton myButton = new JButton("SELESAI", ikon);
myButton.setToolTipText("Ini adalah Button");
myLabel.setToolTipText("Ini adalah Label");
myFrame.setLayout(new BorderLayout());
myFrame.add(myLabel, "South");
myFrame.add(myButton, "North");
myFrame.setSize(500,500);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
outputnya :
import java.awt.*;
public class cthIcon {
public static void main(String[] args) {
JFrame myFrame = new JFrame("Contoh program AWT");
Icon ikon = new ImageIcon("ulos.jpg");
JLabel myLabel = new JLabel("Selamat belajar JAVA", ikon, SwingConstants.CENTER);
JButton myButton = new JButton("SELESAI", ikon);
myButton.setToolTipText("Ini adalah Button");
myLabel.setToolTipText("Ini adalah Label");
myFrame.setLayout(new BorderLayout());
myFrame.add(myLabel, "South");
myFrame.add(myButton, "North");
myFrame.setSize(500,500);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
outputnya :
2) import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.File;
public class ContohJFileChooser
extends JFrame {
public static void
main(String[] args) {
ContohJFileChooser apl = new ContohJFileChooser();
}
public
ContohJFileChooser() {
setSize(400,150);
JFileChooser jfc = new JFileChooser();
FileFilter
filterDotJava = new Akhiran(".java", "Berkas*.java");
jfc.setFileFilter(filterDotJava);
int hasil
= jfc.showOpenDialog(this);
if (hasil
== JFileChooser.APPROVE_OPTION)
setTitle("Tes JFileChooser.File: " +
jfc.getSelectedFile().getName());
else
setTitle("Tes JFileChooser. Batal memilih");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
class Akhiran extends FileFilter {
private String
ekstensi;
private String
keterangan;
public Akhiran(String
suffix, String deskripsi) {
this.ekstensi =
suffix;
this.keterangan
= deskripsi;
}
public boolean
accept(File f) {
if
(f.isDirectory())
return true;
return
f.getName().endsWith(ekstensi);
}
public String
getDescription() {
return keterangan;
}
}
outputnya :
3)
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
class cthList extends JFrame {
JList daftar;
JTextArea tArea1,
tArea2;
JPanel Panel1, Panel2,
Panel3, Panel4;
JSpinner Spin;
String daftarBunga[] =
{"Melati","Mawar","Krisan","Kenanga","Cempaka","Tulip","Kamboja
Jepang"};
public cthList() {
super("Test
JList");
setSize(350,300);
Spin = new
JSpinner();
Spin.setValue(new Integer(10));
Panel1 = new
JPanel();
Panel2 = new
JPanel();
Panel3 = new
JPanel();
Panel4 = new
JPanel();
daftar = new
JList(daftarBunga);
daftar.setSelectedIndex(0);
tArea1 = new
JTextArea("",3,15);
tArea1.setEditable(false);
tArea2 = new JTextArea("",3,15);
tArea2.setEditable(false);
setLayout(new
BorderLayout());
add(Panel1,
"North");
add(Panel2,
"West");
add(Panel3,
"East");
add(Panel4,
"South");
Panel1.add(daftar);
Panel2.add(tArea1);
Panel3.add(tArea2);
Panel4.add(Spin);
setDefaultCloseOperation(3);
setVisible(true);
daftar.addListSelectionListener(new ListSelectionListener() {
public void
valueChanged(ListSelectionEvent e) {
tArea1.setText("Yang terpilih : \n"+daftar.getSelectedValue());
}
}
);
Spin.addChangeListener(new ChangeListener() {
public
void stateChanged(ChangeEvent e) {
tArea2.setText("Nilai :\n"+Spin.getValue());
}
}
);
}
}
public
class ContohJList {
public
static void main(String[] args) {
cthList
apl = new cthList();
}
}
outputnya :
4)
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CthJProgressBar extends JFrame
implements ActionListener {
public final static int
SELANG_WAKTU_TUNGGU = 1;
public final static int
MAKS = 1000;
JProgressBar pb;
JButton b;
Timer timer;
int posisi = 0;
public CthJProgressBar()
{
super("Tes
JProgressBar");
setSize(300,120);
pb = new
JProgressBar(0, MAKS);
b = new
JButton("Mulai");
JPanel p = new
JPanel();
p.add(b);
setLayout(new
BorderLayout());
add("North", p);
add("South", pb);
//-- Set Timer
timer = new Timer
(SELANG_WAKTU_TUNGGU, new ActionListener()
{
public void
actionPerformed(ActionEvent evt) {
posisi++;
pb.setValue(posisi);
if(posisi==MAKS)
{
timer.stop();
b.setText("Mulai");
}
}
});
b.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void
actionPerformed(ActionEvent e) {
String s =
e.getActionCommand();
if(s.equals("Mulai")){
b.setText("Stop");
posisi=0;
pb.setValue(posisi);
timer.start();
}
else {
b.setText("Mulai");
timer.stop();
}
}
}
public class
ContohJProgressBar {
public
static void main(String[] args) {
CthJProgressBar apl = new CthJProgressBar();
}
}
"serif"i � s - �& ��% ont-family:"Times New Roman"'> public
class ContohJList {
public
static void main(String[] args) {
cthList
apl = new cthList();
}
}
outputnya :
5)
import javax.swing.*;
class CthJTabbedPane extends JFrame{
JLabel lTab1,lTab2,lTab3,lTab4;
JTabbedPane tab;
public CthJTabbedPane(){
super ("Tes JTabbedPane");
setSize(300,120);
tab= new JTabbedPane();
JPanel panel1 = new JPanel();
tab.addTab("Satu",panel1);
tab.setSelectedIndex(0);
lTab1= new JLabel ("Tab
satu");
panel1.add(lTab1);
JPanel panel2 = new JPanel();
tab.addTab("Dua",panel2);
lTab2= new JLabel ("Tab
dua");
panel2.add(lTab2);
JPanel panel3 = new JPanel();
tab.addTab("Tiga",panel3);
lTab3= new JLabel ("Tab
tiga");
panel3.add(lTab3);
JPanel panel4 = new JPanel();
tab.addTab("empat",panel4);
lTab4= new JLabel ("Tab
empat");
panel4.add(lTab4);
add(tab);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
public class ContohJTabbedPane{
public static void
main(String[]args){
CthJTabbedPane apl= new
CthJTabbedPane();
}
}
outputnya :
6)
import javax.swing.*;
import java.awt.*;
public class ContohJTable extends
JFrame{
public static void main
(String[]args){
ContohJTable apl = new
ContohJTable();
}
public ContohJTable(){
super ("Tes
JTable");
setSize(500,150);
Object[][] data= {
{"11234",
"Citra Dewi","Akunting",new Long(2200000)},
{"11235",
"Novika Anggraini","Pemasaran",new Long(1200000)},
{"11236",
"Satria","Personalia",new Long(1200000)},
{"11237", "Tri
Styowati","EDP",new Long(1500000)},
{"11238",
"Suadi","Akunting",new Long(1250000)},
{"11239", "Rani
Astri","Akunting",new Long(1000000)},
{"11240",
"Fahmi Udri","Pemasaran",new Long(1100000)},
{"11241", "Listya","Pemasaran",new
Long(1100000)},
};
String [] judulKolom=
{"NIP","Nama","Bagian","Gaji"};
JTable tabel = new
JTable(data, judulKolom);
JScrollPane scrollPane= new
JScrollPane(tabel);
add(scrollPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
outputnya :
7)
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class ContohJTree extends
JFrame{
public static void main (String[]args){
ContohJTree apl= new ContohJTree();
}
public ContohJTree(){
super("Tes JTable");
setSize(500,500);
// ---Buat Akar - Level 0
DefaultMutableTreeNode akar = new
DefaultMutableTreeNode ("Benua");
// ---Level 1
DefaultMutableTreeNode afrika = new
DefaultMutableTreeNode ("Afrika");
DefaultMutableTreeNode asia = new
DefaultMutableTreeNode ("Asia");
DefaultMutableTreeNode amerika = new
DefaultMutableTreeNode ("Amerika");
DefaultMutableTreeNode eropa = new
DefaultMutableTreeNode ("Eropa");
DefaultMutableTreeNode australia =
new DefaultMutableTreeNode ("Australia");
// ---Level 2
DefaultMutableTreeNode mesir = new
DefaultMutableTreeNode ("Mesir");
DefaultMutableTreeNode libya = new
DefaultMutableTreeNode ("Libya");
DefaultMutableTreeNode indonesia =
new DefaultMutableTreeNode ("Indonesia");
DefaultMutableTreeNode jepang = new
DefaultMutableTreeNode ("Jepang");
DefaultMutableTreeNode malaysia =
new DefaultMutableTreeNode ("Malaysia");
DefaultMutableTreeNode kanada = new
DefaultMutableTreeNode ("Kanada");
DefaultMutableTreeNode peru= new
DefaultMutableTreeNode ("Peru");
DefaultMutableTreeNode bolivia = new
DefaultMutableTreeNode ("Bolovia");
DefaultMutableTreeNode brazilia= new
DefaultMutableTreeNode ("Brazilia");
DefaultMutableTreeNode austria = new
DefaultMutableTreeNode ("Austria");
DefaultMutableTreeNode jerman= new
DefaultMutableTreeNode ("Jerman");
DefaultMutableTreeNode perth= new
DefaultMutableTreeNode ("Perth");
DefaultMutableTreeNode canberra= new
DefaultMutableTreeNode ("Canberra");
DefaultMutableTreeNode darwin= new
DefaultMutableTreeNode ("Darwin");
//--- Membuat JTree
JTree struktur = new JTree(akar);
add(struktur);
akar.add(afrika);
akar.add(asia);
akar.add(amerika);
akar.add(eropa);
akar.add(australia);
afrika.add(mesir);
afrika.add(libya);
asia.add(indonesia);
asia.add(jepang);
asia.add(malaysia);
amerika.add(kanada);
amerika.add(peru);
amerika.add(bolivia);
amerika.add(brazilia);
eropa.add(austria);
eropa.add(jerman);
australia.add(perth);
australia.add(canberra);
australia.add(darwin);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// --- Tampilkan frame
setVisible(true);
}
}
Outputnya :
Tidak ada komentar:
Posting Komentar