⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example0702_menuandtool.java

📁 /* 本程序用于说明如何建立和使用菜单 */
💻 JAVA
字号:
/* 本程序用于说明如何建立和使用菜单 */

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Example0702_MenuAndTool extends JFrame implements ActionListener,MenuListener,FocusListener,MouseMotionListener
{
	JMenuItem fNew;
	JMenuItem fOpen;
	JMenuItem fClose;
	JMenuItem fSaveAs;
	JMenuItem fCtrlOthers;
	JMenuItem fExit;
	
	boolean othersExist = true;
	
	JRadioButtonMenuItem rgb1;
	JRadioButtonMenuItem rgb2;
	JRadioButtonMenuItem rgb3;

	JCheckBoxMenuItem ctrlExit;
	boolean exitEnable = true;
	
	JMenu others;
	
	JMenuBar menuBar;
	
	JMenuItem popupItem1;
	JMenuItem popupItem2;
	JMenuItem popupItem3;
	JMenuItem popupExit;
	
	JPopupMenu popupMenu;
	
	JButton ctrlOthersButt;
	JButton exitButt;
	JToolBar toolBar;
	
	JButton test;
			
	public Example0702_MenuAndTool()
	{
		super("A JFrame with menus");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		buildMenu();
		
		buildTool();
		
		test = new JButton("Test");
		test.addFocusListener(this);
		getContentPane().add(test, "South");
		
		addMouseMotionListener(this);
		
		setSize(800,600);
		setVisible(true);
	}
	
	public void buildMenu()
	{
		fNew = new JMenuItem("New", 'n');
		
		fOpen = new JMenuItem("Open", 'o');
		
		fClose = new JMenuItem("Close", 'c');
		
		fSaveAs = new JMenuItem("Save As ...", 'A');
		fSaveAs.setDisplayedMnemonicIndex(5);
		
		fCtrlOthers = new JMenuItem(othersExist ? "Remove the Others Menu" : "Add the Others Menu");
		fCtrlOthers.addActionListener(this);
		
		fExit = new JMenuItem("Exit", 'x');
		fExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK));
		fExit.addActionListener(this);
		
		JMenu file = new JMenu("File(F)");
		file.setMnemonic('f');
		file.setDisplayedMnemonicIndex(5);
		file.addMenuListener(this);
		
		file.add(fNew);
		file.add(fOpen);
		file.add(fClose);
		file.addSeparator();
		file.add(fSaveAs);
		file.addSeparator();
		file.add(fCtrlOthers);
		file.addSeparator();
		file.add(fExit);
		
		rgb1 = new JRadioButtonMenuItem("Red");
		rgb2 = new JRadioButtonMenuItem("Green");
		rgb3 = new JRadioButtonMenuItem("Blue");
		ButtonGroup group = new ButtonGroup();
		group.add(rgb1);
		group.add(rgb2);
		group.add(rgb3);
		
		ImageIcon ii = new ImageIcon("java.gif");
		ctrlExit = new JCheckBoxMenuItem("Exit item Enabled", ii, exitEnable);
		ctrlExit.addActionListener(this);
		
		JMenuItem subItem1 = new JMenuItem("Sub Item1");
		JMenuItem subItem2 = new JMenuItem("Sub Item2");
		JMenuItem subItem3 = new JMenuItem("Sub Item3");
		
		JMenu subMenu = new JMenu("Sub Menu");
		subMenu.add(subItem1);
		subMenu.add(subItem2);
		subMenu.add(subItem3);
				
		others = new JMenu("Other");
		others.add(rgb1);
		others.add(rgb2);
		others.add(rgb3);
		others.addSeparator();
		others.add(ctrlExit);
		others.addSeparator();
		others.add(subMenu);
		
		menuBar = new JMenuBar();
		menuBar.add(file);
		menuBar.add(others);
		
		setJMenuBar(menuBar);
		
		popupItem1 = new JMenuItem("popupItem1");
		popupItem2 = new JMenuItem("popupItem2");
		popupItem3 = new JMenuItem("popupItem3");
		popupExit = new JMenuItem("popupExit");
		popupExit.addActionListener(this);
		
		popupMenu = new JPopupMenu();
		popupMenu.add(popupItem1);
		popupMenu.add(popupItem2);
		popupMenu.add(popupItem3);
		popupMenu.addSeparator();
		popupMenu.add(popupExit);
		
		addMouseListener(new MouseAdapter()
		{
			public void mouseReleased(MouseEvent evt)
			{
				if (evt.isPopupTrigger())
				{
					popupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
				}
			}
		});
	}
	
	public void buildTool()
	{
		ctrlOthersButt = new JButton(othersExist ? "Remove the Others Menu" : "Add the Others Menu");
		ctrlOthersButt.addActionListener(this);
		ctrlOthersButt.setToolTipText("Add or Remove the Others Menu");
		
		exitButt = new JButton("Exit", new ImageIcon("exit.gif"));
		exitButt.setToolTipText("Exit this program");
		exitButt.addActionListener(this);
		
		toolBar = new JToolBar();
		toolBar.add(ctrlOthersButt);
		toolBar.addSeparator();
		toolBar.add(exitButt);
		
		getContentPane().add(toolBar, "North");
	}
	
	public void focusGained(FocusEvent evt)
	{
		test.setText("Focus Gained!");
	}
	
	public void focusLost(FocusEvent evt)
	{
		test.setText("Focus Lost!");
	}
	
	public void mouseMoved(MouseEvent evt)
	{
		setTitle("x = " + evt.getX() + " , y = " + evt.getY());
	}
	
	public void mouseDragged(MouseEvent evt)
	{
		setTitle("Dragged: x = " + evt.getX() + " , y = " + evt.getY());
	}
	
	public void actionPerformed(ActionEvent evt)
	{
		if (evt.getSource() == fCtrlOthers || 
		    evt.getSource() == ctrlOthersButt)
		{
			othersExist = !othersExist;
			fCtrlOthers.setText(othersExist ? "Remove the Others Menu" : "Add the Others Menu");
			ctrlOthersButt.setText(othersExist ? "Remove the Others Menu" : "Add the Others Menu");
			
			if (othersExist)
			{
				menuBar.add(others);
			}
			else
			{
				menuBar.remove(others);
			}
			
			repaint();
		}
		
		if (evt.getSource() == ctrlExit)
		{
			exitEnable = !exitEnable;
		}

		if (evt.getSource() == fExit || 
		    evt.getSource() == popupExit || 
		    evt.getSource() == exitButt)
		{
			if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this, "你确认要退出吗?", "确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE))
			{
				System.exit(0);
			}
		}
	}
	
	public void menuSelected(MenuEvent evt)
	{
		fExit.setEnabled(exitEnable);
	}
	
	public void menuDeselected(MenuEvent evt)
	{
	}
	
	public void menuCanceled(MenuEvent evt)
	{
	}
	
	public static void main(String[] args)
	{
		new Example0702_MenuAndTool();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -