package com.java.myapp; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; public class MyForm extends JFrame { /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MyForm frame = new MyForm(); frame.setVisible(true); } }); } /** * Create the frame. */ public MyForm() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 362, 249); setTitle("ThaiCreate.Com Java GUI Tutorial"); getContentPane().setLayout(null); // Menu Bar JMenuBar menuBar=new JMenuBar(); // Menu 1 JMenu menu1 = new JMenu("Menu 1"); ImageIcon icon1 = new ImageIcon(getClass().getResource("open.gif")); JMenuItem menu1_1 = new JMenuItem("Sub Menu 1-1",icon1); menu1_1.setToolTipText("Open"); menu1_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(null, "Open Command"); } }); ImageIcon icon2 = new ImageIcon(getClass().getResource("save.gif")); JMenuItem menu1_2 = new JMenuItem("Sub Menu 1-2",icon2); menu1.add(menu1_1); menu1.add(menu1_2); menuBar.add(menu1); // Menu 2 JMenu menu2 = new JMenu("Menu 2"); ImageIcon icon3 = new ImageIcon(getClass().getResource("right.gif")); JMenuItem menu2_1 = new JMenuItem("Sub Menu 2-1",icon3); ImageIcon icon4 = new ImageIcon(getClass().getResource("left.gif")); JMenuItem menu2_2 = new JMenuItem("Sub Menu 2-2",icon4); menu2.add(menu2_1); menu2.add(menu2_2); menuBar.add(menu2); setJMenuBar(menuBar); } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท