package caidan;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class JMenuDemo1 implements ActionListener {
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu;
JMenuItem newItem,openItem,saveItem,closeItem;
ImageIcon newIcon,openIcon,saveIcon,closeIcon;
JScrollPane scorll;
JTextArea area;
File file;
private JFileChooser chooser;
public JMenuDemo1(){
frame = new JFrame("记事本");
bar = new JMenuBar();
fileMenu = new JMenu("文件");
editMenu = new JMenu("编辑");
newItem = new JMenuItem("新建(N)",newIcon);
openItem = new JMenuItem("打开(O)",openIcon);
saveItem = new JMenuItem("另存为(A)",saveIcon);
closeItem = new JMenuItem("关闭(X)",closeIcon);
newIcon = new ImageIcon("d:"+File.separator+"新建.png");
openIcon = new ImageIcon("D:"+File.separator+"打开.png");
saveIcon = new ImageIcon("D:"+File.separator+"另存为.png");
closeIcon = new ImageIcon("D:"+File.separator+"关闭.png");
area = new JTextArea();
scorll = new JScrollPane(area);
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
saveItem.setMnemonic(KeyEvent.VK_A);
closeItem.setMnemonic(KeyEvent.VK_X);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
bar.add(fileMenu);
bar.add(editMenu);
frame.setJMenuBar(bar);
frame.add(scorll);
frame.setSize(400, 300);
frame.setVisible(true);
}
总结:老师让我们写一个记事本,我就晓得新建操作和部分按钮设置,监听器的部分老师虽然讲了,很详细,他讲的时候好像觉得不难,我自己操作才发现这,,,我能力还不够,凭我目前这水平,不看老师打的代码,我写不出来啊我。我不晓得打表格,我明天再去问问同学,学习学习。