3总结:
本工程的全部代码粘贴如下:
package textmenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class textmenu extends JFrame implements ActionListener{
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=300;
private JLabel label=new JLabel();
private JMenuItem openItem =new JMenuItem("打开");
private JMenuItem exitItem =new JMenuItem("关闭");
public textmenu(){
setTitle("看图片");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
add(label);
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
JMenu menu=new JMenu("文件");
menuBar.add(menu);
menu.add(openItem);
openItem.addActionListener(this);
menu.add(exitItem);
exitItem.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==openItem){
}
if(e.getSource()==exitItem){
System.exit(0);
}
}
public static void main(String[] args) {
new textmenu();
}
}