?? menu1.java
字號:
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class menu1 extends Frame implements ActionListener,ItemListener
{
MenuBar mnub;
Menu mnu1,mnu2,mnu3,mnu31;
MenuItem itm1,itm2,itm3,itm4,itm5,itm6,itm71,itm72;
CheckboxMenuItem itm8;
MenuShortcut stcte=new MenuShortcut(KeyEvent.VK_E);
MenuShortcut stctn=new MenuShortcut(KeyEvent.VK_N);
Clipboard clp=null;
TextArea txt1,txt2;
menu1()
{
super("一個簡單菜單");
setVisible(true);
setBackground(Color.cyan);
setResizable(true);
setLayout(new FlowLayout());
setBounds(10,10,100,60);
txt1=new TextArea(12,30);
txt2=new TextArea(12,30);
mnub=new MenuBar();
setMenuBar(mnub);
mnu1=new Menu("文件");
mnu2=new Menu("編輯");
mnu3=new Menu("插入");
mnu31=new Menu("文本框");
itm1=new MenuItem("新文件",stctn);
itm2=new MenuItem("打開",new MenuShortcut(KeyEvent.VK_O));
itm3=new MenuItem("退出");
itm3.setShortcut(stcte);
itm4=new MenuItem("復制");
itm5=new MenuItem("剪切");
itm6=new MenuItem("粘貼");
itm71=new MenuItem("橫排");
itm72=new MenuItem("豎排");
itm8=new CheckboxMenuItem("圖片");
mnub.add(mnu1);
mnub.add(mnu2);
mnub.add(mnu3);
mnu3.add(mnu31);
mnu1.add(itm1);
mnu1.addSeparator();
mnu1.add(itm2);
mnu1.addSeparator();
mnu1.add(itm3);
mnu2.add(itm4);
mnu2.addSeparator();
mnu2.add(itm5);
mnu2.addSeparator();
mnu2.add(itm6);
mnu31.add(itm71);
mnu31.addSeparator();
mnu31.add(itm72);
mnu3.addSeparator();
mnu3.add(itm8);
add(txt1);
add(txt2);
pack();
clp=getToolkit().getSystemClipboard();
itm1.addActionListener(this);
itm2.addActionListener(this);
itm3.addActionListener(this);
itm4.addActionListener(this);
itm5.addActionListener(this);
itm6.addActionListener(this);
itm71.addActionListener(this);
itm72.addActionListener(this);
itm8.addItemListener(this);
addWindowListener(new cls());
}
class cls extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==itm1)
{
txt1.setText("單擊"+itm1.getLabel());
}
else if (e.getSource()==itm2)
{
txt1.setText("單擊"+itm2.getLabel());
}
else if (e.getSource()==itm3)
{
setVisible(false);
System.exit(0);
}
else if (e.getSource()==itm4)
{
String str=txt1.getSelectedText();
StringSelection txt=new StringSelection(str);
clp.setContents(txt,null);
}
else if (e.getSource()==itm5)
{
String str=txt1.getSelectedText();
StringSelection txt=new StringSelection(str);
clp.setContents(txt,null);
int start=txt1.getSelectionStart();
int end=txt1.getSelectionEnd();
txt1.replaceRange("",start,end);
}
else if (e.getSource()==itm6)
{
Transferable cnt=clp.getContents(this);
DataFlavor flv=DataFlavor.stringFlavor;
if(cnt.isDataFlavorSupported(flv))
try
{
String str;
str=(String)cnt.getTransferData(flv);
txt2.append(str);
}
catch(Exception ee)
{
}
}
else if (e.getSource()==itm71)
{
txt1.setText("單擊"+itm71.getLabel());
}
else if (e.getSource()==itm72)
{
txt1.setText("單擊"+itm72.getLabel());
}
}
public void itemStateChanged(ItemEvent e)
{
if(itm8.getState()==true)
{
txt2.setText(itm8.getLabel()+" checked ");
}
else
{
txt2.setText(itm8.getLabel()+" unchecked");
}
}
public static void main(String args[])
{
menu1 frm=new menu1();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -