java子菜单的事件怎么写啊?
1个回答
2013-02-17 · 知道合伙人数码行家
关注
展开全部
看我这个吧:
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("弹出对话框");
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 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("弹出对话框");
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询