hafidh

JAVA GUI - Membuat menubar

Feb 19th, 2012
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. import java.awt.event.*;
  2. import javax.swing.*;
  3. import javax.swing.JOptionPane;
  4.  
  5. class CobaMenuBar extends JFrame{
  6.     // attribute
  7.     // buat objek menubar untuk menampung menu
  8.     JMenuBar menubar    = new JMenuBar();
  9.     // buat menu untuk menampung aksi / item yang
  10.     // diinginkan
  11.     JMenu menu1         = new JMenu("File");
  12.     JMenu menu2         = new JMenu("help");
  13.     JMenu submenu       = new JMenu("Submenu");
  14.     // membuat item dalam menu
  15.     JMenuItem item1     = new JMenuItem("new");
  16.     JMenuItem item2     = new JMenuItem("open");
  17.     JMenuItem item3     = new JMenuItem("save");
  18.     JMenuItem item4     = new JMenuItem("close");
  19.     JMenuItem item5     = new JMenuItem("help me");
  20.     // bisa juga masukan cekbox atau radio button
  21.     JCheckBox cekbox    = new JCheckBox("Chekbox");
  22.     JRadioButton radiob1= new JRadioButton("Radio button 1");
  23.     JRadioButton radiob2= new JRadioButton("Radio button 2");
  24.     ButtonGroup group   = new ButtonGroup();
  25.     // untuk framenya
  26.     JTextArea texta     = new JTextArea("history\n");
  27.     JScrollPane scrl;
  28.     public CobaMenuBar(){
  29.         //frame seting---------------------------------------------------------------------
  30.         //memanggil konstruktor kelas induk (JFrame)
  31.         super("coba menubar");
  32.         //seting besar frame LEBAR x TINGGI
  33.         this.setSize(300,350);
  34.         //seting agar bisa ditutup
  35.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  36.         //seting kemunculan frame di tengah window
  37.         this.setLocationRelativeTo(null);
  38.         // pada contoh ini tidak menggunakan layout
  39.         //frame seting---------------------------------------------------------------------
  40.     }
  41.     public void konfig(){
  42.         // masukan menu item ke menu
  43.         group.add(radiob1);
  44.         group.add(radiob2);
  45.         submenu.add(cekbox);
  46.         submenu.add(radiob1);
  47.         submenu.add(radiob2);
  48.         menu1.add(item1);
  49.         menu1.add(item2);
  50.         menu1.add(item3);
  51.         menu1.add(item4);
  52.         menu2.add(item5);
  53.         // membuat separator (garis)
  54.         menu1.addSeparator();
  55.         menu1.add(submenu);
  56.         // masukan menu ke menu bar
  57.         menubar.add(menu1);
  58.         menubar.add(menu2);
  59.         // masukan objek ke frame
  60.         scrl = new JScrollPane(texta);
  61.         this.add(scrl);
  62.         // masukan menubar ke frame
  63.         this.setJMenuBar(menubar);
  64.         // optional : kalau mau buat shortcut pake sintaks ini
  65.         // .setMnemonic(KeyEvent.<key-nya>)
  66.         // java.awt.*; harus di import
  67.         item1.setMnemonic(KeyEvent.VK_N);
  68.         menu1.setMnemonic(KeyEvent.VK_F);
  69.         submenu.setMnemonic(KeyEvent.VK_2);
  70.     }
  71.     public void setAksi(){
  72.         cekbox.addActionListener(new ActionListener( ) {
  73.             public void actionPerformed(ActionEvent e){
  74.                 // kode disini; cek keadaan check box
  75.                 if(cekbox.isSelected()){
  76.                     texta.append("-cekbox dicentang\n");
  77.                 }else{
  78.                     texta.append("-cekbox tidak dicentang\n");
  79.                 }
  80.                
  81.             }
  82.         });
  83.         radiob1.addActionListener(new ActionListener( ) {
  84.             public void actionPerformed(ActionEvent e){
  85.                 // kode disini; cek keadaan check box
  86.                 texta.append("-anda pilih radio button 1\n");
  87.                
  88.             }
  89.         });
  90.         radiob2.addActionListener(new ActionListener( ) {
  91.             public void actionPerformed(ActionEvent e){
  92.                 // kode disini; cek keadaan check box
  93.                 texta.append("-anda pilih radio button 2\n");
  94.                
  95.             }
  96.         });
  97.         item1.addActionListener(new ActionListener( ) {
  98.             public void actionPerformed(ActionEvent e){
  99.                 // kode disini; cek keadaan check box
  100.                 texta.append("-anda klik item 1 (new)\n");
  101.                
  102.             }
  103.         });
  104.         item5.addActionListener(new ActionListener( ) {
  105.             public void actionPerformed(ActionEvent e){
  106.                 // kode disini; cek keadaan check box
  107.                 JOptionPane.showMessageDialog(null,"Selamat anda sudah berhasil membuat menu");
  108.                
  109.             }
  110.         });
  111.     }
  112.     public static void main(String[] args){
  113.         CobaMenuBar bar = new CobaMenuBar();
  114.         bar.konfig();
  115.         bar.setAksi();
  116.         bar.show();
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment