// MenuBar MenuBar mbar = new MenuBar(); // Menu 1 Menu menu1 = new Menu("Menu 1"); MenuItem m1 = new MenuItem("Menu Item 1"); MenuItem m2 = new MenuItem("Menu Item 2"); MenuItem m3 = new MenuItem("Menu Item 3"); // Menu 2 Menu menu2 = new Menu("Menu 2"); MenuItem m4 = new MenuItem("Menu Item 4"); MenuItem m5 = new MenuItem("Menu Item 5"); MenuItem m6 = new MenuItem("Menu Item 6");
package com.java.myapp; import java.awt.EventQueue; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.WindowEvent; public class MyForm extends Frame { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MyForm form = new MyForm(); form.setVisible(true); } }); } public MyForm() { // Create Form Frame super("ThaiCreate.Com Java GUI Tutorial"); setSize(434, 285); setLocation(500, 280); setLayout(null); // MenuBar MenuBar mbar = new MenuBar(); // Menu 1 Menu menu1 = new Menu("Menu 1"); MenuItem m1 = new MenuItem("Menu Item 1"); MenuItem m2 = new MenuItem("Menu Item 2"); MenuItem m3 = new MenuItem("Menu Item 3"); menu1.add(m1); menu1.add(m2); menu1.add(m3); mbar.add(menu1); // Menu 2 Menu menu2 = new Menu("Menu 2"); MenuItem m4 = new MenuItem("Menu Item 4"); MenuItem m5 = new MenuItem("Menu Item 5"); MenuItem m6 = new MenuItem("Menu Item 6"); menu2.add(m4); menu2.add(m5); menu2.add(m6); mbar.add(menu2); setMenuBar(mbar); // Close addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } }
MenuItem m1 = new MenuItem("Menu Item 1"); m1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(null, "Command 1"); } });
package com.java.myapp; import java.awt.EventQueue; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import javax.swing.JOptionPane; public class MyForm extends Frame { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MyForm form = new MyForm(); form.setVisible(true); } }); } public MyForm() { // Create Form Frame super("ThaiCreate.Com Java GUI Tutorial"); setSize(434, 285); setLocation(500, 280); setLayout(null); // MenuBar MenuBar mbar = new MenuBar(); // Menu Menu menu = new Menu("Menu"); // Item 1 MenuItem m1 = new MenuItem("Menu Item 1"); m1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(null, "Command 1"); } }); // Item 2 MenuItem m2 = new MenuItem("Menu Item 2"); m2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(null, "Command 2"); } }); // Item 3 MenuItem m3 = new MenuItem("Menu Item 3"); m3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(null, "Command 3"); } }); menu.add(m1); menu.add(m2); menu.add(m3); mbar.add(menu); setMenuBar(mbar); // Close addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท