|
import java.awt. *;
import java.awt.event. *;
import java.io. *;
import javax.swing. *;
import javax.swing.text. *;
// Simple text editor implements ActionListener
public class edit extends JFrame implements ActionListener {
JTextPane textPane = new JTextPane (); // Text pane, edit window
JFileChooser filechooser = new JFileChooser (); // File selector
JMenuBar menubar = new JMenuBar ();
JMenu menuFile = new JMenu ("File"); // Instantiate the menu
JMenu menuEdit = new JMenu ("Edit");
JMenu menuAbout = new JMenu ("Help");
JMenuItem menu1 = new JMenuItem ("New");
JMenuItem menu2 = new JMenuItem ("Open");
JMenuItem menu3 = new JMenuItem ("closed");
JMenuItem menu4 = new JMenuItem ("Copy");
JMenuItem menu5 = new JMenuItem ("Cut");
JMenuItem menu6 = new JMenuItem ("Paste");
JMenuItem menu7 = new JMenuItem ("Help");
public edit () {// Constructor
super ("Simple text editor");
Ranch
Container container = getContentPane (); // Get the container
Ranch
menu1.addActionListener (this);
menuFile.add (menu1);
Ranch
Ranch
menu2.addActionListener (this);
menuFile.add (menu2);
Ranch
Ranch
menu3.addActionListener (this);
menuFile.add (menu3);
Ranch
Ranch
menu4.addActionListener (this);
menuEdit.add (menu4);
Ranch
Ranch
menu5.addActionListener (this);
menuEdit.add (menu5);
Ranch
Ranch
menu6.addActionListener (this);
menuEdit.add (menu6);
Ranch
menu7.addActionListener (this);
menuAbout.add (menu7);
Ranch
menubar.add (menuFile);
menubar.add (menuEdit);
menubar.add (menuAbout);
Ranch
container.add (textPane, BorderLayout.CENTER); // Add text pane
container.add (menubar, BorderLayout.NORTH); // Add status bar
setSize (330, 200); // Set window size
setVisible (true); // Set window visible
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Exit the program when the window is closed
Ranch
Ranch
Ranch
Ranch
Ranch
Ranch
}
Ranch
Ranch
Ranch
Ranch
Ranch
Ranch
Ranch
Ranch
public void actionPerformed (ActionEvent e) {
if (e.getSource () == menu1) {
Ranch
textPane.setDocument (new DefaultStyledDocument ());} // Empty the document
Ranch
if (e.getSource () == menu2) {
int i = filechooser.showOpenDialog (edit.this); // Show open file dialog
Ranch
Ranch
if (i == JFileChooser.APPROVE_OPTION) {// Click the dialog box to open the option
File f = filechooser.getSelectedFile (); // Get the selected file
try {
InputStream is = new FileInputStream (f); // Get file input stream
textPane.read (is, "d"); // Read the file into the text pane
} catch (Exception ex) {
ex.printStackTrace (); // Output error information
}
}
}
Ranch
Ranch
if (e.getSource () == menu3) {
Ranch
System.exit (0);} // Exit the program
if (e.getSource () == menu4) {
textPane.copy ();} // Call the copy command of the text pane
Ranch
if (e.getSource () == menu5) {
textPane.cut ();} // Call the cut command of the text pane
if (e.getSource () == menu5) {
textPane.paste ();} // Invoke the paste command of the text pane
if (e.getSource () == menu7) {
JOptionPane.showMessageDialog (edit.this, "Simple text editor demo");} // Show software information
}
Ranch
Ranch
Ranch
public static void main (String [] args) {
new edit ();}
Ranch
Ranch
}
The problem was solved just now Thank you !!!!!!!!!!!!!!!
However, copying and pasting is still not possible. Every time I select the text and then select the copy and paste command on the menu, the selected text immediately becomes unselected. What is going on? |
|