Hi all
I copied the source code for a simple text editor from http://www.leepoint.net/notes-java/examples/components/editor/nutpad.html
I tried to modify it slightly by adding a help column to the menu bar so you would click Help> then a drop down list would appear with About and you click that and a frame would appear saying made by blah
Date: blah blah etc....
But i modifyed it and tried to compile it, i realised that there would be bugs but i had an error message Incompatible types which i dont understand.
I have placed
/********************me******************/
comments above most of the lines if code i added so you know what ive changed and the error was appearing on line 18
Heres the code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
///////////////////////////////////////////////////////////////////////// NutPad
public class NutPad extends JFrame {
//... Components
private JTextArea _editArea;
private JFileChooser _fileChooser = new JFileChooser();
//... Create actions for menu items, buttons, ...
private Action _openAction = new OpenAction();
private Action _saveAction = new SaveAction();
private Action _exitAction = new ExitAction();
/*********me*******/
private Action _aboutAction = new AboutAction(); /******************* error message: Incompatible types**************/
//===================================================================== main
public static void main(String[] args) {
new NutPad();
}
//============================================================== constructor
public NutPad() {
//... Create scrollable text area.
_editArea = new JTextArea(15, 80);
_editArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
_editArea.setFont(new Font("monospaced", Font.PLAIN, 14));
JScrollPane scrollingText = new JScrollPane(_editArea);
//-- Create a content pane, set layout, add component.
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollingText, BorderLayout.CENTER);
//... Create menubar
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = menuBar.add(new JMenu("File"));
fileMenu.setMnemonic('F');
fileMenu.add(_openAction); // Note use of actions, not text.
fileMenu.add(_saveAction);
fileMenu.addSeparator();
fileMenu.add(_exitAction);
/*******me*******/
//Create help button on menu bar
JMenu helpMenu = menuBar.add(new JMenu("Help"));
helpMenu.setMnemonic('H');
helpMenu.add(_aboutAction);
//... Set window content and menu.
setContentPane(content);
setJMenuBar(menuBar);
//... Set other window characteristics.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("NutPad");
pack();
setLocationRelativeTo(null);
setVisible(true);
}
////////////////////////////////////////////////// inner class OpenAction
class OpenAction extends AbstractAction {
//============================================= constructor
public OpenAction() {
super("Open...");
putValue(MNEMONIC_KEY, new Integer('O'));
}
//========================================= actionPerformed
public void actionPerformed(ActionEvent e) {
int retval = _fileChooser.showOpenDialog(NutPad.this);
if (retval == JFileChooser.APPROVE_OPTION) {
File f = _fileChooser.getSelectedFile();
try {
FileReader reader = new FileReader(f);
_editArea.read(reader, ""); // Use TextComponent read
} catch (IOException ioex) {
System.out.println(e);
System.exit(1);
}
}
}
}
//////////////////////////////////////////////////// inner class SaveAction
class SaveAction extends AbstractAction {
//============================================= constructor
SaveAction() {
super("Save...");
putValue(MNEMONIC_KEY, new Integer('S'));
}
//========================================= actionPerformed
public void actionPerformed(ActionEvent e) {
int retval = _fileChooser.showSaveDialog(NutPad.this);
if (retval == JFileChooser.APPROVE_OPTION) {
File f = _fileChooser.getSelectedFile();
try {
FileWriter writer = new FileWriter(f);
_editArea.write(writer); // Use TextComponent write
} catch (IOException ioex) {
JOptionPane.showMessageDialog(NutPad.this, ioex);
System.exit(1);
}
}
}
}
/*********me with ym help button but it doesnt work**********************/
class AboutAction
{
public AboutAction()
{
JFrame About = new JFrame();
JLabel label = new JLabel("Hello World");
About.getContentPane().add(label);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("About");
setVisible(true);
}
}
///////////////////////////////////////////////////// inner class ExitAction
class ExitAction extends AbstractAction {
//============================================= constructor
public ExitAction() {
super("Exit");
putValue(MNEMONIC_KEY, new Integer('X'));
}
//========================================= actionPerformed
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
}
Can someone please tell me what i ahve done wrong, i am a noob to java and this was my biggest venture yet, to edit this text editor, so please keep the explanations as simple as possible otherwise i might come back with "I dont understand what you said".
Many Thanks
HLA91