الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Menu...Submenu?!!?

مغلق
بدأه رحيق في 3 أبريل 2003 · 9 رد · 1,170 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم...

اريد أن اعرف كيف اصمم قائمة وقائمة متفرعة

Menu and Submenu

هذا مثال الكتاب بس ما عرفت االاشياء البيسك

يعني الاساسيات

ارجو مساعدتي لأن لغة الجافا جديده علي...

هذا مثال الكتاب

بالطبع لا اقصد شرحه لكن حتى تعرفون قصدي في نوع القائمة

// Fig. 13.10: MenuTest.java

// Demonstrating menus

// Java core packages

import java.awt.*;

import java.awt.event.*;

import com.sun.java.swing.*;

// Java extension packages

public class MenuTest extends JFrame {

private Color colorValues[] =

{ Color.black, Color.blue, Color.red, Color.green };

private JRadioButtonMenuItem colorItems[], fonts[];

private JCheckBoxMenuItem styleItems[];

private JLabel displayLabel;

private ButtonGroup fontGroup, colorGroup;

private int style;

// set up GUI

public MenuTest()

{

super( "Using JMenus" );

// set up File menu and its menu items

JMenu fileMenu = new JMenu( "File" );

fileMenu.setMnemonic( 'F' );

//set up Edit Menu

JMenu editMenu = new JMenu( "Edit" );

fileMenu.setMnemonic( 'E' );

//Set up Package Menu

// JMenu fileMenu = new JMenu( "Package" );

// fileMenu.setMnemonic( 'P' );

// set up About... menu item

JMenuItem aboutItem = new JMenuItem( "About..." );

aboutItem.setMnemonic( 'A' );

aboutItem.addActionListener(

// anonymous inner class to handle menu item event

new ActionListener() {

// display message dialog when user selects About...

public void actionPerformed( ActionEvent event )

{

JOptionPane.showMessageDialog( MenuTest.this,

"This is an examplenof using menus",

"About", JOptionPane.PLAIN_MESSAGE );

}

} // end anonymous inner class

); // end call to addActionListener

fileMenu.add( aboutItem );

// set up Exit menu item

JMenuItem exitItem = new JMenuItem( "Exit" );

exitItem.setMnemonic( 'x' );

exitItem.addActionListener(

// anonymous inner class to handle exitItem event

new ActionListener() {

// terminate application when user clicks exitItem

public void actionPerformed( ActionEvent event )

{

System.exit( 0 );

}

} // end anonymous inner class

); // end call to addActionListener

fileMenu.add( exitItem );

// create menu bar and attach it to MenuTest window

JMenuBar bar = new JMenuBar();

setJMenuBar( bar );

bar.add( fileMenu );

// create Format menu, its submenus and menu items

JMenu formatMenu = new JMenu( "Format" );

formatMenu.setMnemonic( 'r' );

// create Color submenu

String colors[] = { "Black", "Blue", "Red", "Green" };

JMenu colorMenu = new JMenu( "Color" );

colorMenu.setMnemonic( 'C' );

colorItems = new JRadioButtonMenuItem[ colors.length ];

colorGroup = new ButtonGroup();

ItemHandler itemHandler = new ItemHandler();

// create color radio button menu items

for ( int count = 0; count < colors.length; count++ ) {

colorItems[ count ] =

new JRadioButtonMenuItem( colors[ count ] );

colorMenu.add( colorItems[ count ] );

colorGroup.add( colorItems[ count ] );

colorItems[ count ].addActionListener( itemHandler );

}

// select first Color menu item

colorItems[ 0 ].setSelected( true );

// add format menu to menu bar

formatMenu.add( colorMenu );

formatMenu.addSeparator();

// create Font submenu

String fontNames[] = { "Serif", "Monospaced", "SansSerif" };

JMenu fontMenu = new JMenu( "Font" );

fontMenu.setMnemonic( 'n' );

fonts = new JRadioButtonMenuItem[ fontNames.length ];

fontGroup = new ButtonGroup();

// create Font radio button menu items

for ( int count = 0; count < fonts.length; count++ ) {

fonts[ count ] =

new JRadioButtonMenuItem( fontNames[ count ] );

fontMenu.add( fonts[ count ] );

fontGroup.add( fonts[ count ] );

fonts[ count ].addActionListener( itemHandler );

}

// select first Font menu item

fonts[ 0 ].setSelected( true );

fontMenu.addSeparator();

// set up style menu items

String styleNames[] = { "Bold", "Italic" };

styleItems = new JCheckBoxMenuItem[ styleNames.length ];

StyleHandler styleHandler = new StyleHandler();

// create style checkbox menu items

for ( int count = 0; count < styleNames.length; count++ ) {

styleItems[ count ] =

new JCheckBoxMenuItem( styleNames[ count ] );

fontMenu.add( styleItems[ count ] );

styleItems[ count ].addItemListener( styleHandler );

}

// put Font menu in Format menu

formatMenu.add( fontMenu );

// add Format menu to menu bar

bar.add( formatMenu );

// set up label to display text

displayLabel = new JLabel(

"Sample Text", SwingConstants.CENTER );

displayLabel.setForeground( colorValues[ 0 ] );

displayLabel.setFont(

new Font( "TimesRoman", Font.PLAIN, 72 ) );

getContentPane().setBackground( Color.cyan );

getContentPane().add( displayLabel, BorderLayout.CENTER );

setSize( 500, 200 );

setVisible( true );

} // end constructor

// execute application

public static void main( String args[] )

{

MenuTest application = new MenuTest();

}

// inner class to handle action events from menu items

private class ItemHandler implements ActionListener {

// process color and font selections

public void actionPerformed( ActionEvent event )

{

// process color selection

for ( int count = 0; count < colorItems.length; count++ )

if ( colorItems[ count ].isSelected() ) {

displayLabel.setForeground( colorValues[ count ] );

break;

}

// process font selection

for ( int count = 0; count < fonts.length; count++ )

if ( event.getSource() == fonts[ count ] ) {

displayLabel.setFont( new Font(

fonts[ count ].getText(), style, 72 ) );

break;

}

repaint();

}

} // end class ItemHandler

// inner class to handle item events from check box menu items

private class StyleHandler implements ItemListener {

// process font style selections

public void itemStateChanged( ItemEvent e )

{

style = 0;

// check for bold selection

if ( styleItems[ 0 ].isSelected() )

style += Font.BOLD;

// check for italic selection

if ( styleItems[ 1 ].isSelected() )

style += Font.ITALIC;

displayLabel.setFont( new Font(

displayLabel.getFont().getName(), style, 72 ) );

repaint();

}

} // end class StyleHandler

} // end class MenuTest

رحيق

#2

:(

طيب أنا عرفت للmenu

لكن submenu

???????????????????

شكرا

رحيـــــــ(f)(f)(f)(f)ــــــق

#3
public class Notepad extends JFrame{
	//for using the images in the JAR files
	private URL url;

	//declaration of the private variables used in the program
	//create the text area
	private JTextArea textArea;
	//create the Menu Bar that contains the JMenu "filE, ediT, vieW, formaT, helP"
	private JMenuBar Menubar;
	//Create the menu that contains the items
	private JMenu filE, ediT, vieW, formaT, helP;
	//Create the menu items
	private JMenuItem neW, opeN, savE, saveAS, prinT, exiT, fonT, abouT,
	cuT, copY, pastE, selectALL, timEdatE, finD, findNexT;
	private JCheckBoxMenuItem lineWraP;
	//Create the Tool Bar that contains the JButton
	private JToolBar toolBar;
	//Create the buttons
	private JButton newButton, openButton, saveButton, saveAsButton, printButton, 
	undoButton, redoButton, cutButton, copyButton, pasteButton, findButton,
	fontButton, aboutButton;
	//Create Scroll pane (JScrollPane) for the JTextArea
	private JScrollPane scrollpane;

	//for using lineWrap & textArea @Actions.java
	public JCheckBoxMenuItem getLineWrap(){
		return lineWraP;
	}
	public JTextArea getTextArea(){
		return textArea;
	}
	//Constructor of Notepad
	public Notepad(){
		//set the title for Notepad and set the size for it.
		setTitle("Untitled - JAVA™ Notepad");
		setSize(800,600);

		//get the graphical user interface components display area
		Container cp = getContentPane();
		/**
		 *adding the text area,
		 *adding the tool bar &
		 *adding the scroll pane to the container
		 */
		cp.add(textArea = new JTextArea());
		cp.add("North", toolBar = new JToolBar("Tool Bar"));
		cp.add(scrollpane = new JScrollPane(textArea)); 

		//for setting the menu bar
		setJMenuBar(Menubar= new JMenuBar());
		//adding file, edit, view, format, help to the menu bar
		Menubar.add(filE   = new JMenu("File"));
		Menubar.add(ediT   = new JMenu("Edit"));
		Menubar.add(vieW   = new JMenu("View"));
		Menubar.add(formaT = new JMenu("Format"));
		Menubar.add(helP   = new JMenu("Help"));

		/**
		 *adding neW, opeN, savE, saveAS, prinT & exiT to the filE Menu,
		 *adding a small image icon to the menu item &
		 *adding separator between the menu item
		 */
		filE.add(neW   = new JMenuItem("New",new ImageIcon(ClassLoader.getSystemResource("images/new.gif"))));
		filE.add(opeN  = new JMenuItem("Open...",new ImageIcon(ClassLoader.getSystemResource("images/open.gif"))));
		filE.add(savE  = new JMenuItem("Save",new ImageIcon(ClassLoader.getSystemResource("images/save.gif"))));
		filE.add(saveAS= new JMenuItem("Save As...",new ImageIcon(ClassLoader.getSystemResource("images/saveAs.gif"))));
		filE.add(prinT = new JMenuItem("Print...",new ImageIcon(ClassLoader.getSystemResource("images/print.gif"))));
		filE.add(exiT  = new JMenuItem("Exit"));
		filE.insertSeparator(4);
		filE.insertSeparator(6);

هذا جزء من برنامج لي .. و فيه الطريقه المطلوبه :)

#4

على فكره ... انا فهمت السؤال خطأ .. لكن مو مشكله

اذا تبغين تستخدمين SubMenu ...

بدل ما تضيفين للـ Menu الـ MenuItem .. اضيفي للـ Menu .. Menu جديد

يعني

Menu.add(MenuItem); // for adding menu item
Menu.add(Menu); // for adding new menu

ان شاء الله الشرح مفهوم :)

#5

(اهلا بهذا الرد الطويل والجميل..

الحقيقة انا سأعيد تنفيذ Menu

والاستفادة من ملفك الذي ارفقته...

وشكرا كثيرا يا أستاذ صلاح على مساعدتي..ساتفحص هذا الكود

واستفيد منه في تنفيذ الmenu

وساخبركم في حال وجدت مشاكل..

..بس صراحة

:o:o:o:o:o:o:o:o:o:o:o:o

ودي خدمة بسيطه

أبي كود

للundo,redo

لان القائمة اللي عندي استطعت ان اجد اوامر

copy,paste,cut,save,save as,new

وغيرها

(الله يخليك.ويوفقك

.(f)..)ابي منك undo,redo

اذا سمحت,,:)(f)

اختك

رحيق

#8

اخوي صلاح ..:).انا اليوم نزلت الكود الخاص بالnotepad

التي صنعتموها بنفسكم

http://www.planet-source-code.com

فقط بحث في undo

وظهرت امامي النوت باد كاملة في ملف مضغوط :D:D:D

لم استطع الانتظار;);)...لكن لكم جزيل وخالص ووافر وطويل وعريض الشكر

اهلا بطالبة علم...شكرا على الموقع المهم جدا يا عزيزتي سأتفحص الاكواد

واخذ الافضل..

بالنسبة line width

للاسف ...مازلنا على قائمة الانتظار..

رحيـــــــــ(f)(f)ــــق

#9

على فكره .. الـ undo اللي بالكود ،،، هذي مثال من الامثله الموجوده مع الجافا :) ... اذا تبغين تسويين Undo خاص بك .. تقدرين تستخدمين Link List مع الـ Node ...

واحد من اصحابي مسويها بالطريقه هذي و كانت ممتازه :)

#10

الاخ والاستاذ صلاح شكرا لكم كثيرا على افادتي فيما يتعلق باMenu

الحمد لله اشعر بأني استفدت كثيرا من برنامجكم..وكذلك بالنسبة للعمليات الاخرى مثل undo,redo

لكن مازلت أجد بعض المشاكل في التعامل مع 2jbuilder

بالنسبة لاقتراحكم لعمل undo خاص بي:cool::cool:

فكرتك ممتازة لكن للاسف لدي اشياء أهم الان من undo

على فكرة أتمنى ان أستطيع عمل undo,redo

لكن ليس لاستخدمها في برامجي بل في حياتي ,هنالك اشياء ليتها ان تعود وهناك اشياء ليتها لم تحصل....

(ياشين الفلسفة)

:o:o:o:o:o

شكرا كثيرا كثيرا كثيرا

رحيق(f)(f)

هذا الموضوع مغلق.

مواضيع مشابهة