Home | Looking for something? Sign In | New here? Sign Up | Log out

Selasa, 08 Januari 2013

Contoh Program Mengatur Tampilan Frame di Java

Selasa, 08 Januari 2013

Tampilan frame atau lebih dikenal dengan “Look and Feel” di java dapat diatur sesuai keinginan kita. Java memiliki 3 Look and Feel dasar yaitu Metal, Motif dan Windows.
Dan berikut ini contoh tampilannya:


Berikut ini program lengkapnya:
001import java.awt.*;
002
003import java.awt.event.*;
004
005import javax.swing.*;
006
007
008
009public class LookAndFeelDemo extends JFrame {
010
011    private final String lookName[] = {"Metal", "Motif", "Windows"};
012
013    private UIManager.LookAndFeelInfo lookr[];
014
015    private JRadioButton rdLook[];
016
017    private ButtonGroup btnGroup;
018
019    private JButton btn;
020
021    private JTextField txt;
022
023    private JPanel panel1, panel2;
024
025     
026
027    public LookAndFeelDemo () {
028
029        super ("Look and Feel Demo : Tampilan Frame");
030
031        setSize (400,300);
032
033         
034
035        Container container = getContentPane();
036
037        ItemHandler handler = new ItemHandler ();
038
039        panel1 = new JPanel();
040
041        panel2 = new JPanel();
042
043         
044
045        btnGroup = new ButtonGroup();
046
047         
048
049        rdLook = new JRadioButton [lookName.length];
050
051        for (int i = 0; i < rdLook.length; i++) {
052
053            rdLook[i] = new JRadioButton (lookName[i]);
054
055            rdLook[i].addItemListener(handler);
056
057            btnGroup.add(rdLook[i]);
058
059            panel2.add(rdLook[i]);
060
061        }
062
063         
064
065        btn = new JButton ("Tombol");
066
067        txt = new JTextField (20);
068
069         
070
071        panel1.add(btn);
072
073        panel1.add(txt);
074
075         
076
077        container.add(panel1, BorderLayout.NORTH);
078
079        container.add(panel2, BorderLayout.SOUTH);
080
081         
082
083        looks = UIManager.getInstalledLookAndFeels();
084
085         
086
087        setLocationRelativeTo (null);
088
089        setVisible (true);
090
091    }
092
093     
094
095    private void changeLookAndFeel(int val) {
096
097        try {
098
099            UIManager.setLookAndFeel(looks[val].getClassName());
100
101            SwingUtilities.updateComponentTreeUI(this);
102
103        
}
104
105        catch (Exception ex) {
106
107            ex.printStackTrace();
108
109        }
110
111    }
112
113     
114
115    public static void main (String args[]) {
116
117        LookAndFeelDemo test = new LookAndFeelDemo();
118
119        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
120
121    }
122
123     
124
125    private class ItemHandler implements ItemListener {
126
127        public void itemStateChanged (ItemEvent e) {
128
129            //
130
131            for (int i = 0; i < rdLook.length; i++) {
132
133                if (rdLook[i].isSelected()) {
134
135                    changeLookAndFeel (i);
136
137                }
138
139            }
140
141        }
142
143    }
144
145}
Semoga bermanfaat.

0 :

Posting Komentar

Related Posts Plugin for WordPress, Blogger...
 

Contoh Pilihan

SMS GRATIS

Like Box