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

Registered : 109,027

HOME > Java Programming > Java GUI สอน Java GUI เขียนโปรแกรม GUI ด้วย Java(Swing/AWT/JFC) > Java Menu Item/RadioButton (JRadioButtonMenuItem) - Swing Example



Clound SSD Virtual Server

Java Menu Item/RadioButton (JRadioButtonMenuItem) - Swing Example

Java Menu Item/RadioButton (JRadioButtonMenuItem) - Swing Example สำหรับ Menu Item/RadioButton หรือ JRadioButtonMenuItem (javax.swing.JRadioButtonMenuItem) จัดอยู่ในกลุ่มของ Swing Menu ใช้สร้าง Menu Item แบบ Radio Button ซึ่งการใช้งาน JRadioButtonMenuItem จะต้องใช้ควบคู่กับ ButtonGroup ซึ่งจะทำให้การจัดกลุ่มของ Radio สามารถเลือกได้เฉพาะเพียงหนึ่งรายการเท่านั้น และ Menu Item แบบ Radio Button มี Property ที่สำคุณคือ JRadioButtonMenuItem.isVisible() คือใช้ตรวจสอบว่า Radio Button มีการเลือก Item หรือ Option นั้น ๆ หรือไม่

Java Menu Item/RadioButton  (JRadioButtonMenuItem)

Java Menu Item/RadioButton (JRadioButtonMenuItem) - Swing Example


Syntax
ButtonGroup group = new ButtonGroup();
final JRadioButtonMenuItem rdo1 = new JRadioButtonMenuItem("Option 1");
final JRadioButtonMenuItem rdo2 = new JRadioButtonMenuItem("Option 2");
group.add(rdo1);
group.add(rdo2);


Controls Icon Tools

Java Menu Item/RadioButton  (JRadioButtonMenuItem)

Example 1 การสร้าง Menu Item แบบ Radio Button ด้วย JRadioButtonMenuItem แบบง่าย ๆ

MyForm.java
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.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.JRadioButtonMenuItem;

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();
        
        JMenu menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        
        // Open
        ImageIcon openExit = new ImageIcon(getClass().getResource("open.gif"));
        JMenuItem menuOpen = new JMenuItem("Open",openExit);
        menuOpen.setMnemonic(KeyEvent.VK_O);
        menuOpen.setToolTipText("Open Application");
        menuOpen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	JOptionPane.showMessageDialog(null,
            			"Open Command");
            }
        });
        
        // Radio
        zButtonGroup group = new ButtonGroup();
        final JRadioButtonMenuItem rdo1 = new JRadioButtonMenuItem("Option 1");
        final JRadioButtonMenuItem rdo2 = new JRadioButtonMenuItem("Option 2");
        group.add(rdo1);
        group.add(rdo2);
        
        // Exit
        ImageIcon iconExit = new ImageIcon(getClass().getResource("exit.gif"));
        JMenuItem menuExit = new JMenuItem("Exit",iconExit);
        menuExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
        	    ActionEvent.CTRL_MASK));
        menuExit.setToolTipText("Exit Application");
        menuExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	System.exit(0);
            }
        });
        
        // Create menu
        menu.add(menuOpen); // Open
        menu.addSeparator();
        menu.add(rdo1);  // Radio
        menu.add(rdo2);  // Radio
        menu.addSeparator();
        menu.add(menuExit); // Exit
       
        menuBar.add(menu);


        setJMenuBar(menuBar);
	}
}

Output

Java Menu Item/RadioButton  (JRadioButtonMenuItem)

แสดง Menu Item แบบ Radio Button








Example 2 การสร้าง Menu Item แบบ Radio Button และการสร้าง Event Handler

Event Handler
// Radio 1
JRadioButtonMenuItem rdo1 = new JRadioButtonMenuItem("Option 1");
rdo1.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent event) {
	  if (rdo1.isVisible()) {
		  JOptionPane.showMessageDialog(null,
				"Option 1 is Checked");
	  }
	}

});

// Radio 2
JRadioButtonMenuItem rdo2 = new JRadioButtonMenuItem("Option 2");
rdo2.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent event) {
	  if (rdo2.isVisible()) {
		  JOptionPane.showMessageDialog(null,
				"Option 2 is Checked");
	  }
	}

});


MyForm.java
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.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.JRadioButtonMenuItem;

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();
        
        JMenu menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        
        // Open
        ImageIcon openExit = new ImageIcon(getClass().getResource("open.gif"));
        JMenuItem menuOpen = new JMenuItem("Open",openExit);
        menuOpen.setMnemonic(KeyEvent.VK_O);
        menuOpen.setToolTipText("Open Application");
        menuOpen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	JOptionPane.showMessageDialog(null,
            			"Open Command");
            }
        });
        
        // Group
        ButtonGroup group = new ButtonGroup();
        
        // Radio 1
        final JRadioButtonMenuItem rdo1 = new JRadioButtonMenuItem("Option 1");
        rdo1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
              if (rdo1.isVisible()) {
            	  JOptionPane.showMessageDialog(null,
              			"Option 1 is Checked");
              }
            }

        });
        
        // Radio 2
        final JRadioButtonMenuItem rdo2 = new JRadioButtonMenuItem("Option 2");
        rdo2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
              if (rdo2.isVisible()) {
            	  JOptionPane.showMessageDialog(null,
              			"Option 2 is Checked");
              }
            }

        });
        
        group.add(rdo1);
        group.add(rdo2);
        
        // Exit
        ImageIcon iconExit = new ImageIcon(getClass().getResource("exit.gif"));
        JMenuItem menuExit = new JMenuItem("Exit",iconExit);
        menuExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
        	    ActionEvent.CTRL_MASK));
        menuExit.setToolTipText("Exit Application");
        menuExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	System.exit(0);
            }
        });
        
        // Create menu
        menu.add(menuOpen); // Open
        menu.addSeparator();
        menu.add(rdo1);  // Radio
        menu.add(rdo2);  // Radio
        menu.addSeparator();
        menu.add(menuExit); // Exit
       
        menuBar.add(menu);


        setJMenuBar(menuBar);
	}
}

Output

Java Menu Item/RadioButton  (JRadioButtonMenuItem)

แสดง Menu Item แบบ Radio Button

Java Menu Item/RadioButton  (JRadioButtonMenuItem)

เมื่อคลิกเลือกที่รายการของ Radio Button จะแสดง Event Handler ที่กำหนดขึ้น








แนะนำให้อ่านบทความนี้เพิ่มเติม เกี่ยวกับการสร้าง JMenu และการใช้งาน jMenu


   
Share

Property & Method (Others Related)

Java GUI Swing Menus
Java MenuBar (JMenuBar) - Swing Example
Java Menu (JMenu) - Swing Example
Java Menu Item (JMenuItem) - Swing Example
Java Menu Item/Checkbox (JCheckBoxMenuItem) - Swing Example
Java Popup Menu (JPopupMenu) - Swing Example

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


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


   


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

 
Java GUI Swing Menus
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 02
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 อัตราราคา คลิกที่นี่