前几天,老师讲了一个记事本小程序,但是功能不够完善。这几天晚上自习自己写了一个。可是运行,文本输入框,不能输入。源代码如下,求大神指出哪里存在bug。
代码有点多,除去布局等,也没多少。
JFrame frame;
BorderLayout layout;
JMenuBar bar;
JMenu menuFile;
JMenuItem fileNew;
JMenuItem fileOpen;
JMenuItem fileSave;
JMenuItem fileOtherSave;
JMenuItem filePageSet;
JMenuItem filePrint;
JMenuItem fileExit;
JMenu menuEdit;
JMenuItem editCancel;
JMenuItem editCut;
JMenuItem editCopy;
JMenuItem editPaste;
JMenuItem editDelete;
JMenuItem editSearch;
JMenuItem editReplace;
JMenuItem editGItem;
JMenuItem editAItem;
JMenuItem editDItem;
JCheckBoxMenuItem autoWrap;
JMenu menuForruat;
JMenuItem forruatFont;
JMenu menuView;
JMenuItem viewSJMenu;
JMenu menuHelp;
JMenuItem helpHItem;
JMenuItem helpAboutNtepad;
JTextArea area;
JScrollPane scroll;
public void init()
{
// 创建属性
autoWrap = new JCheckBoxMenuItem("自动换行");
frame = new JFrame("记事本");
layout = new BorderLayout();
bar = new JMenuBar();
menuFile = new JMenu("文件");
fileNew = new JMenuItem("新建");
fileOpen = new JMenuItem("打开");
fileSave = new JMenuItem("保存");
fileOtherSave = new JMenuItem("另存为");
filePageSet = new JMenuItem("页面设置");
filePrint = new JMenuItem("打印");
fileExit = new JMenuItem("退出");
menuEdit = new JMenu("编辑");
editCancel = new JMenuItem("撤销");
editCut = new JMenuItem("剪切");
editCopy = new JMenuItem("复制");
editPaste = new JMenuItem("粘贴");
editDelete = new JMenuItem("删除");
editSearch = new JMenuItem("查找");
editReplace = new JMenuItem("替换");
editGItem = new JMenuItem("转到");
editAItem = new JMenuItem("全选");
editDItem = new JMenuItem("时间/日期");
menuForruat = new JMenu("格式");
forruatFont = new JMenuItem("字体");
menuView = new JMenu("查看");
viewSJMenu = new JMenuItem("状态栏");
menuHelp = new JMenu("帮助");
helpHItem = new JMenuItem("查看帮助");
helpAboutNtepad = new JMenuItem("关于记事本");
area = new JTextArea();
scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JFileChooser chooser;
FileReader read;
FileWriter writer;
// 放置属性
frame.add(bar, BorderLayout.NORTH);
bar.add(menuFile);
menuFile.add(fileNew);
menuFile.add(fileOpen);
menuFile.add(fileSave);
menuFile.add(fileOtherSave);
menuFile.addSeparator();// 分割线
menuFile.add(filePageSet);
menuFile.add(filePrint);
menuFile.addSeparator();
menuFile.add(fileExit);
bar.add(menuEdit);
menuEdit.add(editCancel);
menuEdit.addSeparator();
menuEdit.add(editCut);
menuEdit.add(editPaste);
menuEdit.add(editDelete);
menuEdit.addSeparator();
menuEdit.add(editSearch);
menuEdit.add(editReplace);
menuEdit.add(editGItem);
menuEdit.addSeparator();
menuEdit.add(editAItem);
menuEdit.add(editDItem);
bar.add(menuForruat);
menuForruat.add(autoWrap);
menuForruat.add(forruatFont);
bar.add(menuView);
menuView.add(viewSJMenu);
bar.add(menuHelp);
menuHelp.add(helpHItem);
menuHelp.addSeparator();
menuHelp.add(helpAboutNtepad);
frame.add(area);
frame.add(scroll, BorderLayout.CENTER);
// 4.对frame进行自身的设置
frame.setJMenuBar(bar);
frame.setLayout(layout);
fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK));
fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK));
fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK));
frame.setBounds(100, 100, 700, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 实现新建功能 加监听
fileNew.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
area.setText("");
}
});
fileOpen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(area);
File f = chooser.getSelectedFile();
if (f != null)
{
try
{
FileReader read = new FileReader(f);
int i = 0;
area.setText("");
while ((i = read.read()) != -1)
{
char c = (char) i;
area.setText(area.getText() + c + "");
}
read.close();
} catch (Exception e1)
{
e1.printStackTrace();
}
}
}
});
//
fileSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(area);
File f = chooser.getSelectedFile();
if (f != null)
{
// 我们将文本域中的内容写入到文件夹中
try
{
FileWriter writer = new FileWriter(f);
writer.write(area.getText().replace("\n", "\r\n"));
writer.close();
} catch (IOException e1)
{
e1.printStackTrace();
}
}
}
});
fileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
autoWrap.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// 表示自动换行是否选中,选中则返回true 否则false
boolean b = autoWrap.isSelected();
area.setLineWrap(b);
}
});
}
public static void main(String[] args)
{
new Notepad().init();
}
|