Results 1 to 3 of 3
- 11-28-2008, 12:20 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 1
- Rep Power
- 0
Rewrite as a function so it can call from main.
I need help to rewrite this code into a function so that I can call it from the main program. Please guide me how, I have tried in various ways and it could not work. Thanks.
This one also has a little problem that I want user not be able to delete or copy the text when he open a file to read. Thanks In advances.
===
public static void editor()
{
The below program should be in here, so the main can call. Thanks.
}
===
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Editor
extends JFrame
implements ActionListener {
public static void main(String[] s) { new Editor( ); }
private JEditorPane textPane = new JEditorPane( );
public Editor( ) {
super("Editor v1.0");
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
Container content = getContentPane( );
content.add(new JScrollPane(textPane), BorderLayout.CENTER);
JMenu menu = new JMenu("File");
menu.add(makeMenuItem("Open"));
menu.add(makeMenuItem("Save"));
menu.add(makeMenuItem("Quit"));
JMenuBar menuBar = new JMenuBar( );
menuBar.add(menu);
setJMenuBar(menuBar);
setSize(300, 300);
setLocation(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand( );
if (command.equals("Quit")) System.exit(0);
else if (command.equals("Open")) loadFile( );
//else if (command.equals("Save")) saveFile( );
}
private void loadFile ( ) {
JFileChooser chooser = new JFileChooser( );
int result = chooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION) return;
try {
File file = chooser.getSelectedFile( );
java.net.URL url = file.toURL( );
textPane.setPage(url);
}
catch (Exception e) {
textPane.setText("Could not load file: " + e);
}
}
private void saveFile( ) {
JFileChooser chooser = new JFileChooser( );
chooser.showSaveDialog(this);
// Save file data...
}
private JMenuItem makeMenuItem( String name ) {
JMenuItem m = new JMenuItem( name );
m.addActionListener( this );
return m;
}
}
===Last edited by thangli; 11-28-2008 at 12:38 AM. Reason: Modify
- 11-30-2008, 06:03 AM #2
Member
- Join Date
- Nov 2008
- Posts
- 11
- Rep Power
- 0
you should repost this using the "code" "/code" just replace the "" with [] tags so it will be easier to read....
Last edited by jralexander; 11-30-2008 at 06:06 AM.
- 11-30-2008, 06:26 AM #3
Similar Threads
-
rewrite mousedown code
By brahms666 in forum New To JavaReplies: 0Last Post: 11-26-2008, 01:15 AM -
how to add main function
By spratana in forum New To JavaReplies: 5Last Post: 04-16-2008, 01:33 PM -
Call a main method from within a running program
By zoe in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:16 AM -
help with System.exit (1) function call
By katie in forum Advanced JavaReplies: 2Last Post: 08-06-2007, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks