Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

Java AWT and Popup Menu (PopupMenu) - Example

Java AWT and Popup Menu (PopupMenu) - Example สำหรับ Popup Menu (java.awt.PopupMenu) จัดอยู่ในกลุ่มของ AWT Menu ใช้สำหรับสร้าง Menu Item แบบ Popup คล้าย ๆ กับการคลิกขวาที่ Folder แล้วมี Menu ต่าง ๆ ที่อยู่บน Windows OS หรือ OS อื่น ๆ โดยการสร้าง PopupMenu ก็เหมือนกับการสร้าง Menu Item แบบปกติ เพียงแต่เปลี่ยนจาก Menu มาเป็น PopupMenu ส่วนรายละเอียดการสร้าง Event Handler ก็เหมือนกันทุกประการ

Java AWT and Popup Menu (PopupMenu)

Java AWT and Popup Menu (PopupMenu) - Example


Syntax
CreatePopupMenu popMenu = new CreatePopupMenu(this);

Controls Icon Tools

Java AWT and Popup Menu (PopupMenu)

Example ตัวอย่างการสร้าง PopupMenu บน AWT ด้วยการคลิกที่ปุ่ม Button หรือจะคลิกขวาที่ Frame

MyForm.java
package com.java.myapp;

import java.awt.Button;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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);

		// Button
		final Button btn = new Button("Pop-up Menu");
		btn.setBounds(140, 120, 150, 20);
		final CreatePopupMenu popMenu = new CreatePopupMenu(this);
		add(popMenu);
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				popMenu.show(btn, 20, 20);
			}
		});	
		add(btn);
		
		// Mouse Event
		addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger())
					popMenu.show(e.getComponent(), e.getX(), e.getY());
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger())
					popMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		});

		// Close
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				System.exit(0);
			}
		});
	}

	class CreatePopupMenu extends PopupMenu implements ActionListener {
		MyForm ref;

		public CreatePopupMenu(MyForm ref) {
			super("File");
			this.ref = ref;
			MenuItem mnItem;
			add(mnItem = new MenuItem("Menu Item 1"));
			mnItem.addActionListener(this);
			add(mnItem = new MenuItem("Menu Item 2"));
			mnItem.addActionListener(this);
			add(mnItem = new MenuItem("Menu Item 3"));
			mnItem.addActionListener(this);
			add(mnItem = new MenuItem("Menu Item 4"));
			mnItem.addActionListener(this);
			add(mnItem = new MenuItem("Menu Item 5"));
			mnItem.addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String item = e.getActionCommand(); 
			JOptionPane.showMessageDialog(null,
        			"Your Selected : " + item.toString());
		}

	}
}

Output

Java AWT and Popup Menu (PopupMenu)

แสดงปุ่ม Button

Java AWT and Popup Menu (PopupMenu)

คลิกที่ Button หรือคลิกขวาที่ Frame จะแสดง Popup Menu

Java AWT and Popup Menu (PopupMenu)

แสดงรายการที่เลือก






   
Share

Property & Method (Others Related)

Java GUI AWT (Abstract Window Toolkit)
Java AWT and Frame (java.awt.Frame) - Example
Java AWT and Label (Label) - Example
Java AWT and Button (Button) - Example
Java AWT and Text Field (TextField) - Example
Java AWT and Text Area (TextArea) - Example
Java AWT and Checkbox (Checkbox) - Example
Java AWT and Choice (Choice) - Example
Java AWT and List (List) - Example
Java AWT and Scrollbar (Scrollbar) - Example
Java AWT and Scroll Pane (ScrollPane) - Example
Java AWT and Panel (Panel) - Example
Java AWT and Canvas (Canvas) - Example
Java AWT and Menu/Menu Bar (Menu/MenuBar) - Example

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-09-06 08:59:17 / 2013-09-06 11:44:03
  Download : No files
 Sponsored Links / Related

 
Java GUI AWT (Abstract Window Toolkit)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่