package popmenudemo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
public class PopMenuDemo extends JFrame {
JTextArea theArea = null;
static final String ComboStr[] = { "Times New Roman", "Dialog", "宋体", "黑体",
"楷体" };
JPopupMenu Popup = null;// 声明一个全局(Global)并设置初始值为null的JPopupMenu组件.
public PopMenuDemo() {
super("JPopupMenu1");
theArea = new JTextArea();
theArea.setEditable(false);
this.getContentPane()
.add(new JScrollPane(theArea), BorderLayout.CENTER);
JMenuBar MBar = new JMenuBar();
MBar.setOpaque(true);
JMenu mfile = buildFileMenu();
JToolBar theBar = buildToolBar();
this.getContentPane().add(theBar, BorderLayout.NORTH);
PopupPanel pp = new PopupPanel();// new一个PopupPanel组件来放置JPopupMenu.
this.getContentPane().add(pp, BorderLayout.SOUTH); // 将PopupPanel组件放置到窗口中.
MBar.add(mfile);
setJMenuBar(MBar);
}// end of JPopupMenu1()
/*****************begin**********************
* 分别建立程序中菜单,工具栏和事件处理模式的部份,我们在此不再加以说明.
****************************************/
public JMenu buildFileMenu() {
JMenu thefile = new JMenu("File");
thefile.setMnemonic('F');
JMenuItem newf = new JMenuItem("New", new ImageIcon("icons/new.png"));
JMenuItem open = new JMenuItem("Open",
new ImageIcon("icons/open.png"));
JMenuItem close = new JMenuItem("Close", new ImageIcon(
"icons/close.png"));
JMenuItem quit = new JMenuItem("Exit",
new ImageIcon("icons/exit.png"));
newf.setMnemonic('N');
open.setMnemonic('O');
close.setMnemonic('L');
quit.setMnemonic('X');
newf.setAccelerator(KeyStroke.getKeyStroke('N',
java.awt.Event.CTRL_MASK, false));
open.setAccelerator(KeyStroke.getKeyStroke('O',
java.awt.Event.CTRL_MASK, false));
close.setAccelerator(KeyStroke.getKeyStroke('L',
java.awt.Event.CTRL_MASK, false));
quit.setAccelerator(KeyStroke.getKeyStroke('X',
java.awt.Event.CTRL_MASK, false));
newf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
theArea.append("- MenuItem New Performed -\n");
}
});
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
theArea.append("- MenuItem Open Performed -\n");
}
});
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
theArea.append("- MenuItem Close Performed -\n");
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();
thefile.add(quit);
return thefile;
}// end of buildFileMenu()
public JToolBar buildToolBar() {
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(true);
ToolBarAction tba_new = new ToolBarAction("new",new ImageIcon("icons/new.png"));
ToolBarAction tba_open = new ToolBarAction("open",new ImageIcon("icons/open.png"));
ToolBarAction tba_close = new ToolBarAction("close",new ImageIcon("icons/close.png"));
JButton JB;
JB = toolBar.add(tba_new);
JB.setActionCommand("#TooBar_NEW performed!");
JB.setToolTipText((String)tba_new.getValue(Action.NAME));
JB = toolBar.add(tba_open);
JB.setActionCommand("#ToolBar_OPEN performed!");
JB.setToolTipText((String)tba_open.getValue(Action.NAME));
JB = toolBar.add(tba_close);
JB.setActionCommand("#ToolBar_CLOSE performed!");
JB.setToolTipText((String)tba_close.getValue(Action.NAME));
toolBar.addSeparator();
ToolBarAction tba_B = new ToolBarAction("bold",new ImageIcon("icons/bold.png"));
ToolBarAction tba_I = new ToolBarAction("italic",new ImageIcon("icons/italic.png"));
ToolBarAction tba_U = new ToolBarAction("underline",new ImageIcon("icons/underline.png"));
JB = toolBar.add(tba_B);
JB.setActionCommand("#ToolBar_Bold performed!");
JB.setToolTipText((String)tba_B.getValue(Action.NAME));
JB = toolBar.add(tba_I);
JB.setActionCommand("#ToolBar_Italic performed!");
JB.setToolTipText((String)tba_I.getValue(Action.NAME));
JB = toolBar.add(tba_U);
JB.setActionCommand("#ToolBar_Underline performed!");
JB.setToolTipText((String)tba_U.getValue(Action.NAME));
toolBar.addSeparator();
JLabel JLfont = new JLabel("Font Type");
toolBar.add(JLfont);
toolBar.addSeparator();
JComboBox jcb = new JComboBox(ComboStr);
jcb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
theArea.append("*Combobox "+((JComboBox)e.getSource()).getSelectedItem()+" performed!\n");
}});
toolBar.add(jcb);
return toolBar;
}//end of buildToolBar()
/*****************
* End********************** 分别建立程序中菜单,工具栏和事件处理模式的部份,我们在此不再加以说明.
****************************************/
public static void main(String[] args) {
SwingUtil.setLookAndFeel();
JFrame F = new PopMenuDemo();
F.setSize(430, 200);
F.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});// end of addWindowListener
F.setVisible(true);
} // end of main
class ToolBarAction extends AbstractAction {
public ToolBarAction(String name, Icon icon) {
super(name, icon);
}
public void actionPerformed(ActionEvent e) {
try {
theArea.append(e.getActionCommand() + "\n");
} catch (Exception ex) {
}
}
}// end of inner class ToolBarAction
/*
* 本方法建立inner class
* PopupPanel.这个类继承JPanel并实作MouseListener,PopupMenuListener,ActionListener和
* ActionListener等3个Listener
* .MouseListener为处理鼠标事件的Listener,PopupMenuListener为Popup菜单事件的Listener,
* ActionListener则是大家最熟悉用来处理JMenuItem事件的Listener.
*/
class PopupPanel extends JPanel implements MouseListener,
PopupMenuListener, ActionListener {
public PopupPanel() {
Popup = new JPopupMenu();// new 一个新的JPopupMenu.
JMenuItem theItem;
// 分别建立四个JMenuItem和其事件处理模式将将JMenuItem加入JPopupMenu中.
Popup.add(theItem = new JMenuItem("Cut", new ImageIcon(
"icons/cut.png")));// 建立JMenuItem组件.
theItem.addActionListener(this);// 将JMenuItem组件加入事件处理模式.
Popup.add(theItem = new JMenuItem("Copy", new ImageIcon(
"icons/copy.png")));
theItem.addActionListener(this);
Popup.add(theItem = new JMenuItem("Paste", new ImageIcon(
"icons/paste.png")));
theItem.addActionListener(this);
Popup.addSeparator();// 在JPopupMenu中加入分隔线,我们在前面有提到JPopupMenu和Menu大致上的性质是一样的,所以
// 在JPopupMenu中一样是以JMenuItem来当作选项的组件.
Popup.add(theItem = new JMenuItem("Page Setup..."));
theItem.addActionListener(this);
// 利用setBorder()方法将BevelBorder类当作参数传入,使JPopupMenu产生立体边框浮起的效果.我们也可以将参数设置改
// 为BevelBorder.LOWERED使得JPopupMenu产生立体边框凹陷的效果.
Popup.setBorder(new BevelBorder(BevelBorder.RAISED));
Popup.addPopupMenuListener(this); // 将JPopupMenu组件加入PopupMenu事件处理模式中.
addMouseListener(this);// 将PopupPanel类加入Mouse事件处理模式中.
theArea.addMouseListener(this);// 后面有说明
}// end of PopupPanel()
/*
* 由于PopupPanel这个inner
* class中实作了MouseListener,因此需要覆写mouseClicked(),mousePressed(),
* mouseReleased
* (),mouseEntered()和mouseExited()等5个方法.在这个范例里当mouseClicked(
* ),mousePressed()
* 和mouseReleased()等3个方法被触发时会调用checkPopup()方法来�
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
popmenudemo.rar (21个子文件)
popmenudemo
.project 563B
icons
cut.png 524B
bold.png 553B
open.png 745B
underline.png 232B
copy.png 594B
1098416[1].gif 875B
close.png 117B
save.png 761B
exit.png 463B
1172527[1].gif 898B
new.png 356B
paste.png 769B
italic.png 428B
src
test
java
popmenudemo
AppTest.java 677B
main
java
popmenudemo
PopMenuDemo.java 10KB
popmenudemo.bat 45B
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 243B
pom.xml 1KB
.classpath 1022B
共 21 条
- 1
资源评论
s31366
- 粉丝: 0
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功