⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 texteditframe.java~2~

📁 java GUI用户界面程序
💻 JAVA~2~
📖 第 1 页 / 共 2 页
字号:
package chapter7.edit;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.borland.dbswing.*;
import java.io.*;
import javax.swing.text.Document;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class TextEditFrame
    extends JFrame
{
    IntlSwingSupport intlSwingSupport1 = new IntlSwingSupport();
    JPanel contentPane;
    BorderLayout borderLayout1 = new BorderLayout();
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenuFile = new JMenu();
    JMenuItem jMenuFileExit = new JMenuItem();
    JMenu jMenuHelp = new JMenu();
    JMenuItem jMenuHelpAbout = new JMenuItem();
    JToolBar jToolBar = new JToolBar();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JButton jButton3 = new JButton();
    ImageIcon image1 = new ImageIcon(chapter7.TextEditFrame.class.getResource(
        "openFile.png"));
    ImageIcon image2 = new ImageIcon(chapter7.TextEditFrame.class.getResource(
        "closeFile.png"));
    ImageIcon image3 = new ImageIcon(chapter7.TextEditFrame.class.getResource(
        "help.png"));
    JLabel statusBar = new JLabel();
    JScrollPane jScrollPane1 = new JScrollPane();
    JTextArea jTextArea1 = new JTextArea();
    JMenuItem jMenuItem1 = new JMenuItem();
    JMenuItem jMenuItem2 = new JMenuItem();
    JMenuItem jMenuItem3 = new JMenuItem();
    JMenuItem jMenuItem4 = new JMenuItem();
    JMenu jMenu1 = new JMenu();
    JMenuItem jMenuItem5 = new JMenuItem();
    JMenuItem jMenuItem6 = new JMenuItem();
    JMenuItem jMenuItem7 = new JMenuItem();
    FontChooser fontChooser1 = new FontChooser();
    JFileChooser jFileChooser1 = new JFileChooser();
    String currFileName = null; // Full path and filename. null means new/untitled.
    boolean dirty = false; // false means the file was not modified initially.

    Document document1 = jTextArea1.getDocument();
    DBTextDataBinder dBTextDataBinder1 = new DBTextDataBinder();
    public TextEditFrame()
    {
        try
        {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit()
        throws Exception
    {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(borderLayout1);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setSize(new Dimension(400, 300));
        setTitle("Text Edit");
        this.addWindowListener(new TextEditFrame_this_windowAdapter(this));
        statusBar.setText(" ");
        jMenuFile.setText("File");
        jMenuFileExit.setText("Exit");
        jMenuFileExit.addActionListener(new
                                        TextEditFrame_jMenuFileExit_ActionAdapter(this));
        jMenuHelp.setText("Help");
        jMenuHelpAbout.setText("About");
        jMenuHelpAbout.addActionListener(new
                                         TextEditFrame_jMenuHelpAbout_ActionAdapter(this));
        jTextArea1.setLineWrap(true);
        jTextArea1.setWrapStyleWord(true);
        jMenuItem1.setText("New");
        jMenuItem1.addActionListener(new TextEditFrame_jMenuItem1_actionAdapter(this));
        jMenuItem2.setText("Open");
        jMenuItem2.addActionListener(new TextEditFrame_jMenuItem2_actionAdapter(this));
        jMenuItem3.setText("Save");
        jMenuItem3.addActionListener(new TextEditFrame_jMenuItem3_actionAdapter(this));
        jMenuItem4.setText("Save As");
        jMenuItem4.addActionListener(new TextEditFrame_jMenuItem4_actionAdapter(this));
        jMenu1.setText("Font");
        jMenuItem6.setText("Font ");
        jMenuItem6.addActionListener(new TextEditFrame_jMenuItem6_actionAdapter(this));
        jMenuItem5.setText("Foreground Color");
        jMenuItem5.addActionListener(new TextEditFrame_jMenuItem5_actionAdapter(this));
        jMenuItem7.setText("Background Color");
        jMenuItem7.addActionListener(new TextEditFrame_jMenuItem7_actionAdapter(this));
        fontChooser1.setFrame(this);
        fontChooser1.setTitle("Font");
        jButton1.addActionListener(new TextEditFrame_jButton1_actionAdapter(this));
        jButton3.addActionListener(new TextEditFrame_jButton3_actionAdapter(this));
        jButton2.addActionListener(new TextEditFrame_jButton2_actionAdapter(this));
        document1.addDocumentListener(new
                                      TextEditFrame_document1_documentAdapter(this));
        dBTextDataBinder1.setJTextComponent(jTextArea1);
        jMenuBar1.add(jMenuFile);
        jMenuBar1.add(jMenu1);
        jMenuFile.add(jMenuItem1);
        jMenuFile.add(jMenuItem2);
        jMenuFile.add(jMenuItem3);
        jMenuFile.add(jMenuItem4);
        jMenuFile.add(jMenuFileExit);
        jMenuBar1.add(jMenuHelp);
        jMenuHelp.add(jMenuHelpAbout);
        setJMenuBar(jMenuBar1);
        jButton1.setIcon(image1);
        jButton1.setToolTipText("Open File");
        jButton2.setIcon(image2);
        jButton2.setToolTipText("Close File");
        jButton3.setIcon(image3);
        jButton3.setToolTipText("Help");
        jToolBar.add(jButton1);
        jToolBar.add(jButton2);
        jToolBar.add(jButton3);
        contentPane.add(statusBar, BorderLayout.SOUTH);
        contentPane.add(jScrollPane1, java.awt.BorderLayout.CENTER);
        jScrollPane1.getViewport().add(jTextArea1);
        contentPane.add(jToolBar, java.awt.BorderLayout.NORTH);
        jMenu1.add(jMenuItem6);
        jMenu1.add(jMenuItem5);
        jMenu1.add(jMenuItem7);
    }

    /**
     * File | Exit action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuFileExit_actionPerformed(ActionEvent actionEvent)
    {
        if (okToAbandon())
        {
            System.exit(0);
        }
    }

    /**
     * Help | About action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent)
    {
        helpAbout();
    }

    public void jMenuItem6_actionPerformed(ActionEvent e)
    {
        fontChooser1.setSelectedFont(jTextArea1.getFont());
        if (fontChooser1.showDialog())
        {

            jTextArea1.setFont(fontChooser1.getSelectedFont());
        }
    }

    public void jMenuItem5_actionPerformed(ActionEvent e)
    {
        Color color = javax.swing.JColorChooser.showDialog(this,
            "Foreground Color",
            jTextArea1.getForeground());
        if (color != null)
        {
            jTextArea1.setForeground(color);
        }
    }

    public void jMenuItem7_actionPerformed(ActionEvent e)
    {
        Color color = javax.swing.JColorChooser.showDialog(this,
            "Background Color",
            jTextArea1.getBackground());
        if (color != null)
        {
            jTextArea1.setBackground(color);
        }
    }

    public void jMenuItem1_actionPerformed(ActionEvent e)
    {

        if (okToAbandon())
        {
            jTextArea1.setText("");
            currFileName = null;
            dirty = false;
        }

    }

    public void jMenuItem2_actionPerformed(ActionEvent e)
    {
        fileOpen();
    }

    void openFile(String fileName)
    {
        try
        {
            // Open a file of the given name.
            File file = new File(fileName);

            // Get the size of the opened file.
            int size = (int) file.length();

            // Set to zero a counter for counting the number of
            // characters that have been read from the file.
            int chars_read = 0;

            // Create an input reader based on the file, so we can read its data.
            // FileReader handles international character encoding conversions.
            FileReader in = new FileReader(file);

            // Create a character array of the size of the file,
            // to use as a data buffer, into which we will read
            // the text data.
            char[] data = new char[size];

            // Read all available characters into the buffer.
            while (in.ready())
            {
                // Increment the count for each character read,
                // and accumulate them in the data buffer.
                chars_read += in.read(data, chars_read, size - chars_read);
            }

            in.close();

            // Create a temporary string containing the data,
            // and set the string into the JTextArea.
            jTextArea1.setText(new String(data, 0, chars_read));
            this.currFileName = fileName;
            // ...and mark the edit session as being clean
            this.dirty = false;
            // Display the name of the opened directory+file in the statusBar.
            statusBar.setText("Opened " + fileName);
        }

        catch (IOException e)
        {
            statusBar.setText("Error opening " + fileName);
        }
    }

    // Save current file; handle not yet having a filename; report to statusBar.
    boolean saveFile()
    {

        // Handle the case where we don't have a file name yet.
        if (currFileName == null)
        {
            return saveAsFile();
        }

        try
        {
            // Open a file of the current name.
            File file = new File(currFileName);

            // Create an output writer that will write to that file.
            // FileWriter handles international characters encoding conversions.
            FileWriter out = new FileWriter(file);
            String text = jTextArea1.getText();
            out.write(text);
            out.close();
            this.dirty = false;

            // Display the name of the saved directory+file in the statusBar.
            statusBar.setText("Saved to " + currFileName);
            return true;
        }
        catch (IOException e)
        {
            statusBar.setText("Error saving " + currFileName);
        }
        return false;
    }

    // Save current file, asking user for new destination name.
// Report to statusBar.
    boolean saveAsFile()
    {
        // Use the SAVE version of the dialog, test return for Approve/Cancel
        if (JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(this))
        {
            // Set the current file name to the user's selection,
            // then do a regular saveFile
            currFileName = jFileChooser1.getSelectedFile().getPath();
            //repaints menu after item is selected
            this.repaint();
            return saveFile();
        }
        else
        {
            this.repaint();
            return false;
        }
    }

    // Check if file is dirty.
// If so, prompt for save/don't save/cancel save decision.
    boolean okToAbandon()
    {
        if (!dirty)
        {
            return true;
        }
        int value = JOptionPane.showConfirmDialog(this, "Save changes?",
                                                  "Text Edit",

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -