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

📄 ch9_54.java

📁 java图形用户界面设计 包括如何创建窗体 设计界面 管理布局 绘制图形 使用组件 事件编程等
💻 JAVA
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ch9_54 extends JFrame
{
	public ch9_54()
	{
		super("菜单演示");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(20,20,500,300);
		
	}
	public static void main(String [] args)
	{
		ch9_54 f=new ch9_54();
		
		JMenuBar mbar=new JMenuBar();
		JMenu mfile=f.buildFileMenu();
		mbar.add(mfile);
		mbar.setOpaque(true);
		
		JToolBar theBar=f.buildToolBar();

		f.getContentPane().add(theBar,BorderLayout.NORTH);
		f.setJMenuBar(mbar);
		
		
		f.setVisible(true);
	}
	public JMenu buildFileMenu()
	{
		JMenu theedit=new JMenu("文件");
		theedit.setIcon(new ImageIcon("cut.gif"));
		
		JRadioButtonMenuItem newf=new JRadioButtonMenuItem("新建(N)",new ImageIcon("new.gif"),false);
		JRadioButtonMenuItem open=new JRadioButtonMenuItem("打开(O)",new ImageIcon("close.gif"),false);
	
		//添加事件处理
		newf.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.out.print("fasdf");
			}
			
		});		
		
		//两个中只能选择一个
		ButtonGroup bg=new ButtonGroup();
		bg.add(newf);
		bg.add(open);
		
		theedit.add(newf);
		theedit.add(open);
		

		
		return theedit;
	}
	
	public JToolBar buildToolBar()
	{
		JToolBar toolbar=new JToolBar();
		
		//设置可浮动
		toolbar.setFloatable(true);
		
		//分别构造三个ToolBarAction的类组件,准备将这三个组件放置到JToolBar中
		//JToolBarAction是我们自己编写的
		JToolBarAction tba_new= new JToolBarAction("new",new ImageIcon("close.gif"));
		
		
		JToolBarAction tba_open= new JToolBarAction("open",new ImageIcon("open.gif"));
		
		JToolBarAction tba_close= new JToolBarAction("close",new ImageIcon("paste.gif"));
		
		//放置到JToolBar中
		JButton jb;
		jb=toolbar.add(tba_new);
		
		jb=toolbar.add(tba_open);
		jb=toolbar.add(tba_close);
		
		
		return toolbar;
	}
}
class JToolBarAction extends AbstractAction
{
	String thename;
	public JToolBarAction(String name,Icon icon)
	{
		super(name,icon);
		thename=name;
	}
	public void actionPerformed(ActionEvent e)
	{
		try
		{
			System.out.print("点击");
			//这里可以根基不同的thename判断当前点击的按钮,然后编写事件
			System.out.print(thename);
			
		}
		catch(Exception ex)
		{
			
		}
	}
}
		

⌨️ 快捷键说明

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