2014-01-01_143308

Menu, Toolbar, and Dialog Component


Menu menentukan pilihan lebih mudah dan secara luas digunakan dalam aplikasi window.

Java menyediakan lima kelas yang mengimplementasikan menu:

JMenuBar

 JMenu

JMenuItem

JCheckBoxMenuItem

JRadioButtonMenuItem.

Berikut contoh codingan di pertemuan ini :

package pert11;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Pert11 extends JFrame {

public Pert11(){

setSize(300, 200);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);

JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);//attach a menu bar to a frame

JMenu file = new JMenu(“File”);
JMenu help = new JMenu(“Help”);
jmb.add(file);
jmb.add(help);
JMenuItem exit = new JMenuItem(“Exit”);
//JMenuItem tidak bisa dibuat menu lain jadi tak ada turunan
JMenuItem open = new JMenuItem(“Open”);

file.add(open);
file.addSeparator(); //pemisah antar isi menu berupa garis
file.add(exit);
file.setMnemonic(‘F’); //setMnemonic -> ketika tekan alt + char
JMenu s = new JMenu(“Software”);  //JMenu masih bisa dibuat menu dalam menu

JMenuItem l = new JMenuItem(“LINUX”);
JMenuItem w = new JMenuItem(“Windows7”);
help.add(s);
s.add(l);s.add(w);

open.setIcon(new ImageIcon(“image/open.gif”)); //folder image ada di dalam project
help.add(new JCheckBoxMenuItem(“Check it”));
JMenu c = new JMenu(“Color”);
help.add(c);
JRadioButtonMenuItem b = new JRadioButtonMenuItem(“Blue”);
JRadioButtonMenuItem y = new JRadioButtonMenuItem(“Yellow”);
c.add(b);c.add(y);
ButtonGroup bg = new ButtonGroup();
bg.add(b); bg.add(y);

JToolBar jtb = new JToolBar();
JButton time = new JButton(new ImageIcon(“image/gnome_set_time.png”));
jtb.add(time);
add(jtb, BorderLayout.NORTH);

//Add action interface
KeyStroke exitKey = KeyStroke.getKeyStroke(KeyEvent.VK_E,KeyEvent.CTRL_MASK);
Action act = new AbstractAction() {
//private Object ans;-> buat option dialog

@Override
public void actionPerformed(ActionEvent ae) {

//Cara membuat Option dialog

/*int jwb = JOptionPane.showOptionDialog(null,
“asal kota anda?”,
“tanya”,
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
new Object[]{“Jakarta”,”Bandung”}
,”Jakarta”);
JOptionPane.showMessageDialog(null, “”+jwb);
JOptionPane.showInputDialog(null, “nama anda?”);
JOptionPane.showMessageDialog(null, ans);*/

//Cara membuat open file dialog box

/*JFileChooser fc = new JFileChooser();
FileNameExtensionFilter ff = new FileNameExtensionFilter(“gambar”, “jpg”,”png”);
fc.setFileFilter(ff);
int resp = fc.showOpenDialog(null);
File f = fc.getSelectedFile();
if(resp == JFileChooser.APPROVE_OPTION)
{

JOptionPane.showMessageDialog(null, f.getAbsolutePath().toString().substring(f.getAbsolutePath().toString().length()-4));

}*/

}

};

act.putValue(Action.ACCELERATOR_KEY, exitKey);
exit.setAction(act);
exit.setText(“Exit”);

}

public static void main(String[] args) {
new Pert11();
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s