📄 menuexample11128.java
字号:
import java.awt.*;
import java.awt.event.*;
public class MenuExample1 extends Frame implements ItemListener,ActionListener,MouseMotionListener,MouseListener,WindowListener
{
TextField tf;
MenuBar mb;
Menu m1,m2,ms,h;
MenuItem mi1,mi2;
CheckboxMenuItem cm;
FileDialog fd1,fd2;
Dialog d;
Panel p;
Choice ch;
Label l;
CheckboxGroup cbg;
Checkbox black,blue,red,yellow;
int x1,x2,y1,y2;
Color i;
PopupMenu pm;
public static void main(String args[])
{
MenuExample me=new MenuExample();
me.go();
}
public void go()
{
setTitle("Menu Example");
setSize(600,500);
addMouseListener(this);
addMouseMotionListener(this);
addWindowListener(this);
mb=new MenuBar();
setMenuBar(mb);
m1=new Menu("File");
mb.add(m1);
m1.add("Open...");
m1.add("Save");
m1.addSeparator();
m1.add("Exit");
m1.addActionListener(this);
m2=new Menu("Option");
mb.add(m2);
m2.add("Font...");
ms=new Menu("Color...");
ms.add("Foreground");
ms.add("Background");
ms.addActionListener(this);
m2.add(ms);
m2.addSeparator();
cm=new CheckboxMenuItem("Always On Top");
cm.addItemListener(this);
m2.add(cm);
m2.addActionListener(this);
h=new Menu("Help");
h.add("about...");
h.addActionListener(this);
mb.setHelpMenu(h);
tf=new TextField();
add(tf,"South");
fd1=new FileDialog(this,"Open Files",FileDialog.LOAD);
fd2=new FileDialog(this,"Save Files",FileDialog.SAVE);
d=new Dialog(this,"about",true);
d.add(new Label("小李画笔 版本:1.0"));
d.pack();
d.addWindowListener(this);
p=new Panel();
p.setLayout(new GridLayout(10,2));
l=new Label("Select Background");
ch=new Choice();
ch.addItem("White");
ch.addItem("Blue");
ch.addItem("Red");
ch.addItem("Yellow");
ch.addItem("Black");
ch.addItemListener(this);
p.add(l);
p.add(ch);
p.setBackground(Color.green);
add(p,"West");
cbg=new CheckboxGroup();
black=new Checkbox("black",cbg,true);
blue=new Checkbox("blue",cbg,false);
red=new Checkbox("red",cbg,false);
yellow=new Checkbox("yellow",cbg,false);
p.add(black);
p.add(blue);
p.add(red);
p.add(yellow);
black.addItemListener(this);
blue.addItemListener(this);
red.addItemListener(this);
yellow.addItemListener(this);
pm=new PopupMenu ("Popup");
pm.add("New");
pm.add("Load");
pm.add("Save");
pm.add("Save as..");
pm.addActionListener(this);
add(pm);
setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
tf.setText("ItemStateChanged");
if (e.getItem()=="Black")
{
setBackground(Color.black);
}
if (e.getItem()=="Blue")
{
setBackground(Color.blue);
}
if (e.getItem()=="Red")
{
setBackground(Color.red);
}
if (e.getItem()=="Yellow")
{
setBackground(Color.yellow);
}
if (e.getItem()=="White")
{
setBackground(Color.white);
}
if (e.getItem()=="black")
{
i=Color.black;
}
if (e.getItem()=="blue")
{
i=Color.blue;
}
if (e.getItem()=="red")
{
i=Color.red;
}
if (e.getItem()=="yellow")
{
i=Color.yellow;
}
}
public void actionPerformed(ActionEvent e)
{
tf.setText(e.getActionCommand());
if ((e.getActionCommand()=="Open...")||(e.getActionCommand()=="Load"))
{
fd1.setVisible(true);
tf.setText("Open files from: "+fd1.getDirectory());
}
if (e.getActionCommand()=="Save")
{
fd2.setVisible(true);
tf.setText("Save files to: "+fd2.getDirectory());
}
if (e.getActionCommand()=="about...")
{
d.setVisible(true);
}
if (e.getActionCommand()=="Exit")
System.exit(0);
}
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseDragged(MouseEvent e)
{
Graphics g=getGraphics();
g.setColor(i);
x2=e.getX();
y2=e.getY();
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
public void mouseClicked(MouseEvent e)
{
pm.show(this,x1,y1);
}
public void mouseMoved(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void windowClosing(WindowEvent e)
{
if (d.isVisible()==true)
d.setVisible(false);
else
System.exit(0);
}
public void windowActivated(WindowEvent e)
{}
public void windowClosed(WindowEvent e)
{}
public void windowDeactivated(WindowEvent e)
{}
public void windowDeiconified(WindowEvent e)
{}
public void windowIconified(WindowEvent e)
{}
public void windowOpened(WindowEvent e)
{}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -