import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyMenu1 extends Frame implements ActionListener
{
String[] strColor={"紅色", "紛紅色", "綠色", "黃色", "藍色", "黑色"};
Color[] c=new Color[]{Color.red, Color.pink, Color.green,
Color.yellow, Color.blue, Color.black};
MenuBar menubar;
Menu mnuColor, mnuFontColor, mnuBackColor, mnuHelp;
MenuItem mnuCopyRightItem, mnuExitItem;
MenuItem[] mnuFontColorItem = new MenuItem[strColor.length];
MenuItem[] mnuBackColorItem = new MenuItem[strColor.length];
Label lbl;
Font f=new Font("細明體", Font.BOLD, 20);
MyMenu1(String title)
{
setLayout(null);
setTitle(title);
setVisible(true);
setSize(460, 200);
lbl = new Label("http://www.wretch.cc/blog/JavaNeo");
lbl.setBounds(50, 100, 350, 40);
lbl.setFont(f);
menubar = new MenuBar();
mnuColor = new Menu("設定顏色");
mnuFontColor = new Menu("文字顏色");
mnuBackColor = new Menu("背景色");
for (int i=0; i {
mnuFontColorItem[i] = new MenuItem(strColor[i]);
mnuFontColor.add(mnuFontColorItem[i]);
mnuFontColorItem[i].addActionListener(this);
mnuBackColorItem[i] = new MenuItem(strColor[i]);
mnuBackColor.add(mnuBackColorItem[i]);
mnuBackColorItem[i].addActionListener(this);
}
mnuHelp = new Menu("說明");
mnuCopyRightItem = new MenuItem("版權");
mnuCopyRightItem.addActionListener(this);
mnuExitItem = new MenuItem("結束");
mnuExitItem.addActionListener(this);
mnuHelp.add(mnuCopyRightItem);
mnuHelp.add(mnuExitItem);
mnuColor.add(mnuFontColor);
mnuColor.add(mnuBackColor);
menubar.add(mnuColor);
menubar.add(mnuHelp);
setMenuBar(menubar);
add(lbl);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent ae)
{
for (int i=0; i {
if (ae.getSource()==mnuFontColorItem[i])
lbl.setForeground(c[i]);
else if(ae.getSource()==mnuBackColorItem[i])
lbl.setBackground(c[i]);
}
if (ae.getSource()==mnuCopyRightItem)
JOptionPane.showMessageDialog(null, "\t By Neo

\nhttp://www.wretch.cc/blog/JavaNeo");
else if(ae.getSource()==mnuExitItem)
System.exit(0);
}
public static void main(String[] args)
{
new MyMenu1("功能表範例");
}
}
arrow
arrow
    全站熱搜

    Neo Chao 發表在 痞客邦 留言(0) 人氣()