Creating Menus in Java

Senin, 19 Desember 2011

Examples The following program demonstrates how to create a menu in java. Menus created with two main classes JMenu and JMenuItem.

Here is an example looks:


contoh-program-menu-java 

Berikut ini contoh program lengkapnya:
001import java.awt.*;
002 
003import java.awt.event.*;
004 
005import javax.swing.*;
006 
007public class MenuTest extends JFrame {
008 
009    private JMenuBar bar;
010 
011    private JMenu mnuFile, mnuHelp, submnuNew;
012 
013    private JMenuItem itemNew, itemOpen, itemExit, itemAbout, itemHelp,
014 
015        subitemFile, subitemProject;
016 
017    public MenuTest() {
018 
019        super ("Membuat menu");
020 
021        bar = new JMenuBar();
022 
023        setJMenuBar (bar);
024 
025        mnuFile = new JMenu ("File");
026 
027        mnuFile.setMnemonic('F');
028 
029        mnuHelp = new JMenu ("Help");
030 
031        mnuHelp.setMnemonic('H');
032 
033        submnuNew = new JMenu ("New");
034 
035        submnuNew.setMnemonic('N');
036 
037        mnuFile.add(submnuNew);
038 
039        subitemFile = new JMenuItem ("File...");
040 
041        subitemFile.setIcon(new ImageIcon("images/new.gif"));
042 
043        subitemFile.setAccelerator(KeyStroke.getKeyStroke(
044 
045            KeyEvent.VK_N, ActionEvent.CTRL_MASK));
046 
047        subitemFile.addActionListener(
048 
049            new ActionListener() {
050 
051                public void actionPerformed(ActionEvent e) {
052 
053                    JOptionPane.showMessageDialog(null, "New File");
054 
055                }
056 
057            }
058 
059        );
060 
061        submnuNew.add(subitemFile);
062 
063        subitemProject = new JMenuItem ("Project...");
064 
065        subitemProject.setMnemonic ('P');
066 
067        submnuNew.add(subitemProject);
068 
069        itemOpen = new JMenuItem ("Open");
070 
071        itemOpen.setMnemonic('O');
072 
073        mnuFile.add(itemOpen);
074 
075        mnuFile.addSeparator();
076 
077        itemExit = new JMenuItem ("Exit");
078 
079        itemExit.setMnemonic('E');
080 
081        itemExit.addActionListener(
082 
083            new ActionListener() {
084 
085                public void actionPerformed(ActionEvent e) {
086 
087                    if (JOptionPane.showConfirmDialog(null,"Apakah Anda yakin akan keluar?",
088 
089                        "Konfirmasi",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)
090 
091                        == JOptionPane.OK_OPTION) {
092 
093                        System.exit(0);
094 
095                    }
096 
097                }
098 
099            }
100 
101        );
102 
103        mnuFile.add(itemExit);
104 
105        itemHelp = new JMenuItem ("Help");
106 
107        itemHelp.setIcon(new ImageIcon("images/help.gif"));
108 
109        itemHelp.setMnemonic('e');
110 
111        mnuHelp.add(itemHelp);
112 
113        itemAbout = new JMenuItem ("About");
114 
115        itemAbout.setMnemonic('A');
116 
117        itemAbout.setIcon(new ImageIcon("images/info.gif"));
118 
119        mnuHelp.add(itemAbout);
120 
121        bar.add(mnuFile);
122 
123        bar.add(mnuHelp);
124 
125        setSize (400,200);
126 
127        setLocationRelativeTo(null);
128 
129        setVisible (true);
130 
131    }
132 
133    public static void main (String args[]) {
134 
135        JFrame.setDefaultLookAndFeelDecorated(true);
136 
137        MenuTest test = new MenuTest();
138 
139        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
140 
141    }
142 
143}

0 komentar:

 
© Copyright 2010-2011 materials informatics All Rights Reserved.
Template Design by Moch Ramdhan | Published by Ramdhan Templates | Powered by Blogger.com.