Berikut contoh aplikasi GUI pada pemrograman Java dengan menggunakan class SWING dan AWT. Dengan contoh ini akan dipahamkan tentang penggunaan komponen dasar SWING (GUI) pada pemrograman java yang meliputi :
- Top Level Container: JFrame, JDialog ,
- Intermediate Container : JPanel,
- Atomic Component : JButton, JLabel, JTextField, JTextArea,
- Layout Manager: Flowlayout, BorderLayout, BoxLayout,
- Event Handling : penekanan tombol, mouse over dsb.
Contoh-contoh program jawa tersebut seperti berikut :
- JForm
import javax.swing.JFrame;
public class gui1{
public static void main(String[] args) {
JFrame a=new JFrame(“Latihan Frame”);
a.setSize(400,150);
a.setVisible(true);
}
} - JButton – FlowLayout
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.FlowLayout;
public class gui2 {
public static void main(String[] args){
JFrame a=new JFrame(“Mencoba Frame”);
FlowLayout tampilan=new FlowLayout(FlowLayout.CENTER);
JButton b1=new JButton(“Satu”);
JButton b2=new JButton(“Dua”);
JButton b3=new JButton(“Tiga”);
JButton b4=new JButton(“Empat”);
JButton b5=new JButton(“Lima”);
a.setLayout(tampilan);
a.add(b1);
a.add(b2);
a.add(b3);
a.add(b4);
a.add(b5);
a.pack();
a.setSize(600,100);
a.setVisible(true);
}
} - JButton – BorderLayout
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
public class gui3 {
public static void main(String[] args){
JFrame a=new JFrame(“Mencoba Frame”);
BorderLayout tampilan=new BorderLayout();
JButton b1=new JButton(“Satu”);
JButton b2=new JButton(“Dua”);
JButton b3=new JButton(“Tiga”);
JButton b4=new JButton(“Empat”);
JButton b5=new JButton(“Lima”); a.add(b1,tampilan.NORTH);
a.add(b2,tampilan.EAST);
a.add(b3,tampilan.WEST);
a.add(b4,tampilan.SOUTH);
a.add(b5,tampilan.CENTER);
a.pack();
a.setSize(400,500);
a.setVisible(true);
}
} - JCheckBox
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
public class gui4{
public static void main(String[] args){
JFrame a=new JFrame(“Coba Frame”);
JLabel label=new JLabel(“Menu makan siang : “);
JCheckBox cb1=new JCheckBox(“Nasi Goreng”);
JCheckBox cb2=new JCheckBox(“Nasi Soto Ayam”);
JCheckBox cb3=new JCheckBox(“Nasi Pecel”);
JCheckBox cb4=new JCheckBox(“Nasi Rawon”);
Box b=new Box(BoxLayout.Y_AXIS);
b.add(label);
b.add(cb1);
b.add(cb2);
b.add(cb3);
b.add(cb4);
a.add(b);
a.pack();
a.setSize(400,500);
a.setVisible(true);
}
} - JRadioButton
import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
public class gui5{
public static void main(String[] args){
JFrame a=new JFrame(“Coba Frame”);
JRadioButton rb1=new JRadioButton(“Es Jeruk”);
JRadioButton rb2=new JRadioButton(“Es Tea”);
JRadioButton rb3=new JRadioButton(“Es Teler”);
JRadioButton rb4=new JRadioButton(“Es Degan”);
ButtonGroup gr=new ButtonGroup();
gr.add(rb1);
gr.add(rb2);
gr.add(rb3);
gr.add(rb4);
Box b=new Box(BoxLayout.Y_AXIS);
b.add(rb1);
b.add(rb2);
b.add(rb3);
b.add(rb4);
a.add(b);
a.pack();
a.setSize(400,500);
a.setVisible(true);
}
}
You can leave a response, or trackback from your own site.