How can I write jsp code called with buttons?

Source: Internet
Author: User

// Import the JAVA class package
Import java. awt .*;
Import java. awt. event .*;
Import javax. swing .*;
Import java. io .*;

Class EditFile
{
// Default constructor
EditFile (){}
// Save the file Function
Void SaveFile (String name, String str)
{
Try
{
// Declare the file output object
FileOutputStream fos = new FileOutputStream (name );
// Output and save the file
Fos. write (str. getBytes ());
}
Catch (IOException e)
{
// Print the error message when an error occurs
System. out. println (e. toString ());
}
}
// Open the file Function
String OpenFile (String name)
{
// Define the str string to save the file content
String str = null;
// Define the counter
Int ch = 0;
Try
{
// Declare the input object of the file
FileInputStream FCM = new FileInputStream (name );
// Traverse the input object and store the memory in str
While (ch = FCM. read ())! =-1)
{
Str = str + (char) ch;
}
// Close the object
FCM. close ();
}
Catch (IOException e)
{
// Print the error message when an error occurs
System. out. println (e. toString ());
}
// Return str
Return str;
}
}

Class MyMainFrame extends JFrame implements ActionListener
{
// File name
String filename = null;
// Search for strings
String findtxt = null;
// Replace the string
String replacetxt = null;
// Search Start bit
Int findtxton =-1;
// Find the digit
Int findtxtoff =-1;
// Intermediate plane
Container c = getContentPane ();
// Menu
JMenuBar mainMb = new JMenuBar ();
// Menu button
JMenu fileMenu = new JMenu ("file ");
JMenu editMenu = new JMenu ("edit ");
JMenu typeMenu = new JMenu ("format ");
JMenuItem newItem = new JMenuItem ("new ");
JMenuItem openItem = new JMenuItem ("open ");
JMenuItem saveItem = new JMenuItem ("save ");
JMenuItem saveasItem = new JMenuItem ("Save ");
JMenuItem exitItem = new JMenuItem ("quit ");
JMenuItem findItem = new JMenuItem ("Search ");
JMenuItem findnextItem = new JMenuItem ("find next ");
JMenuItem changeItem = new JMenuItem ("replace ");
JCheckBoxMenuItem changelineItem = new JCheckBoxMenuItem ("", false );
// Text field
JTextArea txt = new JTextArea ();
JTextArea txt2 = new JTextArea ();
// Scroll bar
JScrollPane jsp (preferred for SUN Enterprise applications );
// File operation object
EditFile ef = new EditFile ();
// Constructor
MyMainFrame ()
{
// Draw the interface
C. setLayout (new BorderLayout ());
Jsp (preferred for SUN Enterprise Applications) = new JScrollPane (txt );
Jsp (preferred for SUN Enterprise Applications). setHorizontalScrollBarPolicy (preferred for SUN Enterprise Applications). HORIZONTAL_SCROLLBAR_AS_NEEDED );
C. add (jsp (preferred for SUN Enterprise applications), BorderLayout. CENTER );
MainMb. add (fileMenu );
FileMenu. add (newItem );
FileMenu. add (openItem );
FileMenu. add (saveItem );
FileMenu. add (saveasItem );
FileMenu. addSeparator ();
FileMenu. add (exitItem );
MainMb. add (editMenu );
EditMenu. add (findItem );
EditMenu. add (findnextItem );
EditMenu. add (changeItem );
MainMb. add (typeMenu );
TypeMenu. add (changelineItem );
SetJMenuBar (mainMb );
// Add event listening for the menu
NewItem. addActionListener (this );
OpenItem. addActionListener (this );
SaveItem. addActionListener (this );
SaveasItem. addActionListener (this );
ExitItem. addActionListener (this );
FindItem. addActionListener (this );
FindnextItem. addActionListener (this );
ChangeItem. addActionListener (this );
ChangelineItem. addActionListener (this );
}
// Event processing functions
Public void actionreceivmed (ActionEvent e)
{
// Create a file operation object
EditFile ef = new EditFile ();
// If you click the button to create
If (e. getActionCommand () = "new ")
{
// Leave the text field empty
Txt. setText (null );
// The file name is empty.
Filename = null;
}
// If you click the button to open
If (e. getActionCommand () = "open ")
{
// Define a file form
FileDialog openDialog = new FileDialog (this, "open file...", FileDialog. LOAD );
// Display form
OpenDialog. show ();
// If the returned file is not empty
If (openDialog. getFile ()! = Null)
{
// Get the file name
Filename = openDialog. getFile ();
// Obtain the File Content
Txt. setText (ef. OpenFile (openDialog. getFile ()));
}
}
// If you click the Save button
If (e. getActionCommand () = "save ")
{
// If no file name exists
If (filename = null)
{
// Define a file form
FileDialog saveDialog = new FileDialog (this, "SAVE as...", FileDialog. SAVE );
// Display form
SaveDialog. show ();
// If the returned file is not empty
If (saveDialog. getFile ()! = Null)
{
// Get the file name
Filename = saveDialog. getFile ();
// Save the file
Ef. SaveFile (filename, txt. getText ());
}
}
Else
{
// Directly save the file
Ef. SaveFile (filename, txt. getText ());
}
}
// If you click the Save
If (e. getActionCommand () = "Save ")
{
// Define a file form
FileDialog saveDialog = new FileDialog (this, "SAVE as...", FileDialog. SAVE );
// Display form
SaveDialog. show ();
// If the returned file is not empty
If (saveDialog. getFile ()! = Null)
{
// Get the file name
Filename = saveDialog. getFile ();
// Save the file
Ef. SaveFile (filename, txt. getText ());
}
}
// If the button is pushed
If (e. getActionCommand () = "exit ")
{
// Exit
System. exit (0 );
}
// If you click the search button
If (e. getActionCommand () = "Search ")
{
// Define a dialog box
JOptionPane inputdia = new JOptionPane ();
// Get the search text in the definition dialog box
Findtxt = inputdia. showInputDialog ("Enter the text to search", null );
// Define the actual coordinates
Findtxton =-1;
Findtxtoff =-1;
// Call the lookup Function
Find ();
}
// If you click the button to find the next
If (e. getActionCommand () = "find next ")
{
// Call the lookup Function
Find ();
}
// If you click the button to replace
If (e. getActionCommand () = "replace ")
{
// Define a dialog box
JOptionPane inputdia = new JOptionPane ();
// Obtain the search and replace text
Findtxt = inputdia. showInputDialog ("Enter the text to be replaced", null );
Replacetxt = inputdia. showInputDialog ("Enter the replaced text", null );
// Backup content
Txt2.setText (replacetxt );
Txt2.selectAll ();
Txt2.copy ();
// Call the lookup Function
Find ();
// If not found
If (findtxton =-1 & findtxtoff =-1)
{
}
// If you find
Else
{
// Replace
Txt. paste ();
}
Findtxton =-1;
Findtxtoff =-1;
}
// If you click the button to wrap the line automatically
If (e. getActionCommand () = "")
{
// Judge the current status
If (changelineItem. isSelected () = true)
{
// Set the scroll bar status
Jsp (preferred for SUN Enterprise Applications). setHorizontalScrollBarPolicy (preferred for SUN Enterprise Applications). HORIZONTAL_SCROLLBAR_NEVER );
// Set whether to wrap the line.
Txt. setLineWrap (true );
}
Else
{
Jsp (preferred for SUN Enterprise Applications). setHorizontalScrollBarPolicy (preferred for SUN Enterprise Applications). HORIZONTAL_SCROLLBAR_AS_NEEDED );
Txt. setLineWrap (false );
}
}
}
// Query Functions
Public void find ()
{
// Define a temporary string
String tempstr;
// If the search text is not empty
If (findtxt! = Null)
{
Try
{
// Search
Tempstr = txt. getText (findtxton + 1, (txt. getText (). length ()-findtxton-1 ));
Findtxton = findtxton + 1 + tempstr. indexOf (findtxt );
}
Catch (Exception e)
{

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.