JMenu jmF=new JMenu("File")
JMenuItem jmi=new JMenuItem("Exit")//子菜单
看我这个吧:import java.awt.*
import java.awt.event.*
import java.io.*
public class TestWindow extends Frame implements ActionListener
{
TextArea ta
Button b1,b2
Dialog md
MenuItem open,save,myexit,open1,save1
MenuItem copy,palse
CheckboxMenuItem bold,ita
MenuBar mb
PopupMenu popm
public void createmenu()
{
mb=new MenuBar()
Menu m1=new Menu("文件")
Menu m2=new Menu("编辑")
Menu m3=new Menu("字体")
mb.add(m1)mb.add(m2)m2.add(m3)
open=new MenuItem("打开")
open.addActionListener(this)
save=new MenuItem("保存")
myexit=new MenuItem("退出")
m1.add(open)m1.add(save)m1.add(myexit)
copy=new MenuItem("复制")
palse=new MenuItem("粘贴")
bold=new CheckboxMenuItem("加粗")
ita=new CheckboxMenuItem("倾斜")
m2.add(copy)m2.add(palse)
m3.add(bold)m3.add(ita)
popm=new PopupMenu ()
open1=new MenuItem("打开")
open1.addActionListener(this)
save1=new MenuItem("保存")
popm.add(open1)popm.add(save1)
ta.add(popm)
ta.addMouseListener(new MyMouseEvent(this))
}
class MyMouseEvent extends MouseAdapter
{ TestWindow myparentFrame
MyMouseEvent ( TestWindow m)
{myparentFrame=m }
public void mouseReleased(MouseEvent e)
{if(e.isPopupTrigger())
myparentFrame.popm.show((Component)e.getSource(),e.getX(),e.getY()) }
}
public TestWindow ()
{
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dispose()
System.exit(0)
}
})
ta=new TextArea(10,30)
b1=new Button("d出对话框")
b2=new Button("隐藏对话框")
b1.addActionListener(this)
b2.addActionListener(this)
add(ta,BorderLayout.CENTER)
Panel p1=new Panel()
p1.add(b1)
p1.add(b2)
add(p1,BorderLayout.SOUTH)
setVisible(true)
setSize(400,300)
md=new Dialog(this,"对话框")
md.add(new Label("这是一个对话框"))
md.setSize(100,60)
md.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
md.setVisible(false)
}
})
pack()
createmenu()
setMenuBar(mb)
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
md.setVisible(true)
if(e.getSource()==b2)
md.setVisible(false)
if(e.getActionCommand()=="打开")
try{ openfile()}catch(IOException ex){}
}
public void openfile() throws IOException
{
FileDialog fd=new FileDialog(this,"aaa",FileDialog.LOAD)
fd.setVisible(true)
FileInputStream fis=new FileInputStream( fd.getDirectory()+fd.getFile())
String s=""
int n=0
while((n=fis.read())!=-1)
ta.append(""+(char)n)
fis.close()
}
public static void main(String []args)
{
TestWindow tw=new TestWindow()
}
}
import java.awt.*import javax.swing.*
public class ErjiMenu extends JFrame
{
JPanel jp=new JPanel()
JMenuBar menubar=new JMenuBar()
JMenu m1=new JMenu("菜单")
JMenu m2=new JMenu("菜单")
JMenuItem item1=new JMenuItem("菜单项")
JMenuItem item2=new JMenuItem("菜单项1")
JMenuItem item3=new JMenuItem("菜单项2")
public ErjiMenu()
{
jp.setLayout(new BorderLayout())
m2.add(item2) //m2添加菜单项。
m2.add(item3)
m1.add(item1)
m1.add(m2) //m1把m2添加进去,作为二级菜单.
menubar.add(m1)
jp.add(menubar,BorderLayout.NORTH)
this.add(jp)
this.setBounds(100,100,380,200)
this.setVisible(true)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
public static void main(String[] args)
{
new ErjiMenu()
}
}
不用JPopupMenu,更简单。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)