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;
}
}
hasil program :
2. ContohJList
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("Tes 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();
}
}
Hasil Program
3. ContohJProgressBar
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CthProgressBar extends JFrame implements ActionListener {
public final static int SELANG_WAKTU_TUNGGU = 1;
public static int MAKS =1000;
JProgressBar pb;
JButton b;
Timer timer;
int posisi = 0;
public CthProgressBar() {
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) {
ContohJProgressBar apl = new ContohJProgressBar();
}
}
4. CthJTabbedPane extends JFrame
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();
}
}
hasil program
5. ContohJTable extends JFrame
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,500);
Object[][] data = {
{"11234", "Asti Damayanti", "Akunting", new Long (2200000)},
{"11235", "Erittria", "Pemasaran", new Long(1200000)},
{"11236", "Satria", "Personalia", new Long(1200000)},
{"11237", "Tri Setyowati", "EDP", new Integer(1500000)},
{"11238", "Fahmi Basya", "Akunting", new Long(1250000)},
{"11239", "Suadi Marwan", "Akunting", new Long(1000000)},
{"11240", "Rani Ardiani", "Pemasaran", new Long(110000)},
{"11238", "Listya", "Pemasaran", new Long(110000)},
};
String[] judulKolom = {"NIP", "Nama", "Bagian", "Gaji"};
JTable table= new JTable (data, judulKolom);
JScrollPane scrollPane= new JScrollPane(table);
add(scrollPane);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
hasil program
6. ContohJTree extends JFrame
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class ContohJTree extends JFrame {
public static void main (String [] args){
ContohJTree ap1 = new ContohJTree();
}
public ContohJTree(){
super ("Tes JTable");
setSize (400,400);
//---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");
//---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("Bolivia");
DefaultMutableTreeNode brazilia = new DefaultMutableTreeNode("Brazilia");
DefaultMutableTreeNode australia = new DefaultMutableTreeNode("australia");
DefaultMutableTreeNode jerman= new DefaultMutableTreeNode("Jerman");
//--- Membuat JTree
JTree struktur = new JTree (akar);
add(struktur);
akar.add(afrika);
akar.add(asia);
akar.add(amerika);
akar.add(eropa);
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(australia);
eropa.add(jerman);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// --- Tampilkan frame
setVisible(true);
}
}
hasil program
7. cthIcon
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("1.GIF");
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);
}
}
hasil program
Tidak ada komentar:
Posting Komentar