import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;public class JPopupMenu1 extends JFrame{
JTextArea theArea=null;
static final String ComboStr[]={"Times New Roman","Dialog","宋体","黑体","楷体"};
JPopupMenu Popup=null;//声明一个全局(Global)并设置初始值为null的JPopupMenu组件
public JPopupMenu1(){
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();
this.getContentPane().add(pp,BorderLayout.SOUTH);//将PopupPanel组件放置到窗口中
MBar.add(mfile);
setJMenuBar(MBar);
}//end of JPopupMenu1()
/*****************************************************************
分别建立程序中菜单,工具栏和事件处理模式的部分,我们在此不再加以说明
******************************************************************/
public JMenu buildFileMenu(){
JMenu thefile=new JMenu("File");
thefile.setMnemonic('F');
JMenuItem newf=new JMenuItem("New",new ImageIcon("open.jpg"));
JMenuItem open=new JMenuItem("Open",new ImageIcon("open.jpg"));
JMenuItem close=new JMenuItem("Close",new ImageIcon("open.jpg"));
JMenuItem quit=new JMenuItem("Exit",new ImageIcon("open.jpg"));
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("3.gif"));
ToolBarAction tba_open=new ToolBarAction("open",new ImageIcon("3.gif"));
ToolBarAction tba_close=new ToolBarAction("close",new ImageIcon("3.gif"));
JButton JB;
JB=toolBar.add(tba_new);
JB.setActionCommand("#ToolBar_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("3.gif"));
ToolBarAction tba_I=new ToolBarAction("italic",new ImageIcon("3.gif"));
ToolBarAction tba_U=new ToolBarAction("underline",new ImageIcon("3.gif"));
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()
/************************************************************************
分别建立程序中菜单,工具栏和事件处理模式的部分,我们在此不再加以说明。
************************************************************************/
public static void main(String[] args){
new JPopupMenu1();
JFrame F=new JPopupMenu1();
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;public class JPopupMenu1 extends JFrame{
JTextArea theArea=null;
static final String ComboStr[]={"Times New Roman","Dialog","宋体","黑体","楷体"};
JPopupMenu Popup=null;//声明一个全局(Global)并设置初始值为null的JPopupMenu组件
public JPopupMenu1(){
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();
this.getContentPane().add(pp,BorderLayout.SOUTH);//将PopupPanel组件放置到窗口中
MBar.add(mfile);
setJMenuBar(MBar);
}//end of JPopupMenu1()
/*****************************************************************
分别建立程序中菜单,工具栏和事件处理模式的部分,我们在此不再加以说明
******************************************************************/
public JMenu buildFileMenu(){
JMenu thefile=new JMenu("File");
thefile.setMnemonic('F');
JMenuItem newf=new JMenuItem("New",new ImageIcon("open.jpg"));
JMenuItem open=new JMenuItem("Open",new ImageIcon("open.jpg"));
JMenuItem close=new JMenuItem("Close",new ImageIcon("open.jpg"));
JMenuItem quit=new JMenuItem("Exit",new ImageIcon("open.jpg"));
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("3.gif"));
ToolBarAction tba_open=new ToolBarAction("open",new ImageIcon("3.gif"));
ToolBarAction tba_close=new ToolBarAction("close",new ImageIcon("3.gif"));
JButton JB;
JB=toolBar.add(tba_new);
JB.setActionCommand("#ToolBar_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("3.gif"));
ToolBarAction tba_I=new ToolBarAction("italic",new ImageIcon("3.gif"));
ToolBarAction tba_U=new ToolBarAction("underline",new ImageIcon("3.gif"));
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()
/************************************************************************
分别建立程序中菜单,工具栏和事件处理模式的部分,我们在此不再加以说明。
************************************************************************/
public static void main(String[] args){
new JPopupMenu1();
JFrame F=new JPopupMenu1();
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
解决方案 »
- JAVA 官方网站,找不到 JDK 6 的API文档下载地址?给我把地址发上来,谢谢、。要官方地址。
- 帮我看看什么错!编译通过,运行显示main():java.lang.NullPointException
- javaBean运行环境
- preferences怎么操作注册表?
- 如何在一个函数中掷出三个不同类型的异常
- 哪里能下载到jprobe或OPTIMIZEIT???
- java怎样实现树?
- Jbuider6's Register Code
- 初学javamail
- eclipse使用debug调试时,出现Source not found.错误
- 各位见过下面类型的帮忙给解释一下吧,万分感谢!!
- 急,在线等,怎么使用js在同一页面中实现点击不同的按扭实现不同的内容
你的代码中也没有