Daisypath Friendship tickers
Tampilkan postingan dengan label programming. Tampilkan semua postingan
Tampilkan postingan dengan label programming. Tampilkan semua postingan

Minggu, 26 Februari 2012

Contoh Program Message Dialog di Java

Contoh program berikut ini menampilkan jenis-jenis window pesan di Java. Window pesan (message dialog) antara lain bertipe warning message, information message, confirmation message, dan juga input message. Class yang digunakan adalah class JOptionPane.
Berikut ini tampilannya:
contoh-program-message-dialog-java

Berikut ini contoh programnya:
001import java.awt.*;
002 
003import java.awt.event.*;
004 
005import javax.swing.*;
006 
007public class MessageDialog extends JFrame {
008 
009    private JButton tombol, btn2, btn3, btn4, btn5;
010 
011    public MessageDialog() {
012 
013        super ("Event Handling");
014 
015        Container container = getContentPane();
016 
017        container.setLayout(new FlowLayout());
018 
019        tombol = new JButton ("Message Dialog");
020 
021        tombol.addActionListener(
022 
023            new ActionListener() {
024 
025                public void actionPerformed (ActionEvent e) {
026 
027                    JOptionPane.showMessageDialog (null,"Contoh Message Dialog");
028 
029                }
030 
031            }
032 
033        );
034 
035        container.add(tombol);
036 
037        btn2 = new JButton ("Warning Message");
038 
039        btn2.addActionListener(
040 
041            new ActionListener() {
042 
043                public void actionPerformed (ActionEvent e) {
044 
045                    JOptionPane.showConfirmDialog(null, "Contoh Warning Message","Peringatan",
046 
047                        JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
048 
049                }
050 
051            }
052 
053        );
054 
055        container.add(btn2);
056 
057        btn3 = new JButton ("Question Message");
058 
059        btn3.addActionListener(
060 
061            new ActionListener() {
062 
063                public void actionPerformed (ActionEvent e) {
064 
065                    JOptionPane.showConfirmDialog(null, "Contoh Question Message","Pertanyaan",
066 
067                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
068 
069                }
070 
071            }
072 
073        );
074 
075        container.add(btn3);
076 
077        btn4 = new JButton ("Information Message");
078 
079        btn4.addActionListener(
080 
081            new ActionListener() {
082 
083                public void actionPerformed (ActionEvent e) {
084 
085                    JOptionPane.showConfirmDialog(null, "Contoh Information Message","Informasi",
086 
087                        JOptionPane.NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
088 
089                }
090 
091            }
092 
093        );
094 
095        container.add(btn4);
096 
097        btn5 = new JButton ("Input Dialog");
098 
099        btn5.addActionListener(
100 
101            new ActionListener() {
102 
103                public void actionPerformed (ActionEvent e) {
104 
105                    String a = JOptionPane.showInputDialog("Input Nama : ");
106 
107                    JOptionPane.showMessageDialog(null, a);
108 
109                }
110 
111            }
112 
113        );
114 
115        container.add(btn5);
116 
117        setSize (200,300);
118 
119        setLocationRelativeTo(null);
120 
121        setVisible (true);
122 
123    }
124 
125    public static void main (String arg[]) {
126 
127        MessageDialog test = new MessageDialog();
128 
129        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
130 
131    }
132 
133}

Contoh Program Multiple Selection List di Java

COntoh program java berikut ini merupakan variasi dari contoh program list sebelumnya. Di dalam program ini, list dibuat agar bisa dipilih lebih dari satu pilihan.
Berikut ini tampilan programnya:
contoh-program-multiple-list-java

Berikut ini programnya:
01import java.awt.*;
02import java.awt.event.*;
03import javax.swing.*;
04 
05public class MultipleSelectionTest extends JFrame {
06    private JList lstColor, lstCopy;
07    private JButton btnCopy;
08    private final String arrColorName[] =
09        { "Black","Blue","Cyan","Dark Gray","Gray","Green","Light Gray",
10          "Magenta","Orange","Pink","Red","Yellow","White"
11        };
12 
13    public MultipleSelectionTest() {
14        super ("Multiple Selection List");     
15 
16        Container container = getContentPane();
17        container.setLayout(new FlowLayout());
18 
19        lstColor = new JList (arrColorName);
20        lstColor.setVisibleRowCount(5);
21        lstColor.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
22        container.add(new JScrollPane (lstColor));
23 
24        btnCopy = new JButton ("Copy >>>");       
25 
26        btnCopy.addActionListener(
27            new ActionListener() {
28                public void actionPerformed (ActionEvent e) {
29                    lstCopy.setListData(lstColor.getSelectedValues());
30                }
31            } //end of class
32        );
33 
34        container.add(btnCopy);    
35 
36        lstCopy = new JList();
37        lstCopy.setVisibleRowCount(5);
38        lstCopy.setFixedCellHeight(15);
39        lstCopy.setFixedCellWidth(100);
40        lstCopy.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
41        container.add(new JScrollPane(lstCopy))    
42 
43        setSize (400,300);
44        setLocationRelativeTo(null);
45        setVisible(true);
46    }
47 
48    public static void main (String args[]) {
49        MultipleSelectionTest test = new MultipleSelectionTest();
50        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51    }
52 
53}
maju terus ilmu pengetahuan Indonesia!

Contoh Program List di Java

List atau List box pada dasarnya sama seperti combobox, hanya saja pada list box pilihan dapat diatur agar dapat ditampilkan beberapa baris sekaligus. Berikut ini contoh program java sederhana untuk membuat dan menerapkan listener terhadap ListBox. Listbox dapat dibuat dengan class JList.
Berikut ini tampilannya:
contoh-program-list-java

Dan berikut ini contoh programnya:
01import java.awt.*;
02import javax.swing.event.*;
03import javax.swing.*;
04 
05public class ListTest extends JFrame {
06    private JList lstColor;
07    private final String arrColorName[] =
08        { "Black","Blue","Cyan","Dark Gray","Gray","Green","Light Gray",
09          "Magenta","Orange","Pink","Red","Yellow","White"
10        };
11    private final Color arrColor[] =
12        { Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GRAY,
13          Color.GREEN,Color.LIGHT_GRAY,Color.MAGENTA,Color.ORANGE,Color.PINK,
14          Color.RED,Color.YELLOW,Color.WHITE
15        };
16 
17    private Container container;
18 
19    public ListTest() {
20 
21        super ("Mencoba JList");
22        container = getContentPane();
23        container.setLayout(new FlowLayout());
24 
25        lstColor = new JList (arrColorName);
26        lstColor.setVisibleRowCount(5);
27 
28        //hanya boleh pilih satu
29        lstColor.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
30        container.add(new JScrollPane (lstColor));
31        //container.add(lstColor);
32        lstColor.addListSelectionListener(
33            new ListSelectionListener() { //adanya di package javax.swing.event
34                public void valueChanged (ListSelectionEvent e) {
35                    container.setBackground(arrColor[lstColor.getSelectedIndex()]);
36                }
37 
38            } //end of class
39 
40        );
41 
42        setSize (400,300);
43        setLocationRelativeTo(null);
44        setVisible(true);
45    }
46 
47    public static void main (String args[]) {
48 
49           ListTest test = new ListTest();
50           test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51    }
52 
53}
Semoga bermanfaat

Membuat Slider Sederhana di Java

Slider merupakan jenis inputan dimana nilai akan berubah dengan menggeser posisi penunjuk pada slider. Contoh program java berikut ini akan membuat slider sederhana dengan Java. Class yang digunakan adalah JSlider.
Berikut ini tampilan contoh program slider:
contoh-program-slider-java

Berikut ini contoh programnya:
01import java.awt.*;
02 
03import java.awt.event.*;
04 
05import javax.swing.*;
06 
07import javax.swing.event.*;
08 
09public class SimpleSliderDemo extends JFrame {
10 
11    private JSlider slider;
12 
13    private JTextField txtValue;
14 
15    public SimpleSliderDemo() {
16 
17        super ("JSlider Sederhana");
18 
19        Container container = getContentPane();
20 
21        container.setLayout(new FlowLayout());
22 
23        txtValue = new JTextField (20);
24 
25        txtValue.setBackground(Color.WHITE);
26 
27        txtValue.setEditable(false);
28 
29        slider = new JSlider(SwingConstants.HORIZONTAL,0,100,50);
30 
31        txtValue.setText(String.valueOf(slider.getValue()));
32 
33        slider.setMinorTickSpacing(5);
34 
35        slider.setMajorTickSpacing(20);
36 
37        slider.setPaintTicks(true);
38 
39        slider.setLabelTable(slider.getLabelTable());
40 
41        slider.setPaintLabels(true);
42 
43        slider.addChangeListener (
44 
45            new ChangeListener() {
46 
47                public void stateChanged (ChangeEvent e) {
48 
49                    txtValue.setText ( String.valueOf(slider.getValue()) );
50 
51                }
52 
53            }
54 
55        );
56 
57        container.add(txtValue);
58 
59        container.add(slider);
60 
61        setSize (280,200);
62 
63        setVisible (true);
64 
65    }
66 
67    public static void main (String args[]) {
68 
69        JFrame.setDefaultLookAndFeelDecorated(true);
70 
71        SimpleSliderDemo test = new SimpleSliderDemo();
72 
73        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
74 
75    }
76 
77}
Semoga bermanfaat

Membuat Popup Menu di Java

Popup menu merupakan menu yang akan ditampilkan saat kita melakukan klik kanan di window. Program berikut ini merupakan contoh program membuat popup menu di Java. Class yang digunakan adalah JPopupMenu.
Berikut ini tampilan programnya:
contoh-program-menu-popup-java

Berikut ini source code programnya:
01import java.awt.*;
02import java.awt.event.*;
03import javax.swing.*;
04 
05public class PopupMenuTest extends JFrame {
06    private JRadioButtonMenuItem items[];
07    private final Color colorValues[] = {Color.BLUE, Color.YELLOW, Color.RED};
08    private JPopupMenu popMenu;
09 
10    public PopupMenuTest() {
11        super ("Menu Popup");
12 
13        ItemHandler handler = new ItemHandler();
14        String colorNames[] = {"Biru", "Kuning", "Merah"};
15 
16        ButtonGroup colorGroup = new ButtonGroup();
17        popMenu = new JPopupMenu();
18        items = new JRadioButtonMenuItem [colorValues.length];
19 
20        for (int i = 0; i < items.length; i++) {
21            items[i] = new JRadioButtonMenuItem (colorNames[i]);
22            popMenu.add (items[i]);
23            colorGroup.add (items[i]);
24            items[i].addActionListener(handler);
25           }
26 
27        getContentPane().setBackground(Color.WHITE);
28 
29        addMouseListener(
30            new MouseAdapter() {
31                public void mousePressed (MouseEvent e) {
32                    showPopupMenu (e);
33                }
34 
35                public void mouseReleased (MouseEvent e) {
36                    showPopupMenu (e);
37                }
38 
39                private void showPopupMenu(MouseEvent e) {
40                    if (e.isPopupTrigger())
41                        popMenu.show(e.getComponent(), e.getX(), e.getY());
42                }
43            } //end of anonymous class
44        ); //end of addMouseListener
45 
46        setSize (400,300);
47        setLocationRelativeTo (null);
48        setVisible (true);
49    }
50 
51    public static void main (String args[]) {
52        JFrame.setDefaultLookAndFeelDecorated(true);
53        PopupMenuTest test = new PopupMenuTest();
54        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
55    }  
56 
57    private class ItemHandler implements ActionListener {
58        public void actionPerformed (ActionEvent e) {
59            //
60            for (int i = 0; i < items.length; i++) {
61                if (e.getSource() == items[i]) {
62                    getContentPane().setBackground (colorValues[i]);
63                    return;
64                }
65            }
66        }
67    }
68}
Semoga bermanfaat. [OOT] Kunjungi situs ini untuk mendapatkan buku gratis PHP dan MySQL.