画图小程序 - 加载/保存图像时出现问题

发布于 2024-11-15 17:48:34 字数 4740 浏览 4 评论 0原文

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.lang.Math.*;
import java.text.*;
import java.io.*;
import java.awt.Scrollbar.*;
import javax.swing.*;
import java.awt.datatransfer.*;

public class DFunPaint extends JApplet implements ActionListener, AdjustmentListener, MouseListener, MouseMotionListener
{
    String filename;
    JTextArea tx = new JTextArea();
    JMenuBar mb =new JMenuBar();
    JMenu f = new JMenu("File");
    JMenuItem n = new JMenuItem("New");
    JMenuItem o = new JMenuItem("Open");
    JMenuItem s = new JMenuItem("Save");
    JMenuItem e = new JMenuItem("Exit");
    JMenu ed = new JMenu("Edit");
    JMenuItem cut = new JMenuItem("Cut");
    JMenuItem copy = new JMenuItem("Copy");
    JMenuItem paste = new JMenuItem("Paste");
    JMenu format = new JMenu("Format");
    JMenuItem  Word = new JMenuItem("Word Wrap");
    JMenuItem  Font = new JMenuItem("Font");
    JMenu view = new JMenu("View");
    JMenuItem  StatusBar= new JMenuItem("StatusBar");
    JMenu help = new JMenu("Help");
    JMenuItem  About = new JMenuItem("About");
    JMenuItem Topics = new JMenuItem("Topics");

    private JPanel p1 = new JPanel(new BorderLayout());

    public void init(){
        setLayout(new BorderLayout());
        f.add(n);
        f.add(o);
        f.add(s);
        f.add(e);
        mb.add(f);
        ed.add(cut);
        ed.add(copy);
        ed.add(paste);
        mb.add(ed);
        format.add(Word);
        format.add(Font);
        mb.add(format);
        mb.add(view);
        view.add(StatusBar);
        mb.add(help);
        help.add(About);
        JMenuItem Topics = new JMenuItem("Topics");
        help.add(Topics);

        p1.add(mb);

        add(p1,"North");

        n.addActionListener(this);
        o.addActionListener(this);
        s.addActionListener(this);
        e.addActionListener(this);
        cut.addActionListener(this);
        copy.addActionListener(this);
        paste.addActionListener(this);
        About.addActionListener(this);
        Topics.addActionListener(this);
    }

    public void ActionPerformed(ActionEvent e)
    {
        if (e.getSource() == n)
            tx.setText(" ");
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == o)
            JFileDialog fd = new JFileDialog( DFunPaint, "select File",JFileDialog.LOAD);
        fd.show();
        if (fd.getFile()!=null)
        {
            filename = fd.getDirectory() + fd.getFile();
            setTitle(filename);
            ReadFile();
        }
        tx.requestFocus();
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == s)
            FileDialog fd = new FileDialog(DFunPaint,"Save File",FileDialog.SAVE);
        fd.show();
        if (fd.getFile()!=null)
        {
            filename = fd.getDirectory() + fd.getFile();
            setTitle(filename);
            try
            {
                DataOutputStream d = new DataOutputStream(new FileOutputStream(filename));
                String line = tx.getText();
                BufferedReader br = new BufferedReader(new StringReader(line));
                while((line = br.readLine())!=null)
                {
                    d.writeBytes(line + "\r\n");
                    d.close();
                }
            }
            catch(Exception ex)
            {
                System.out.println("File not found");
            }
            tx.requestFocus();
        }
    }

    public void ctionPerformed(ActionEvent ae)
    {
        if (ae.getSource() == e)
            System.exit(0);
    }

    public void ActionPerformed(ActionEvent e)
    {
        if (e.getSource() == cut)
            String sel =
        StringSelection ss = new StringSelection(tx.getSelectedText());
        clip.setContents(ss,ss);
        tx.replaceRange(" ",tx.getSelectionStart(),tx.getSelectionEnd());
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == copy)
            String sel = tx.getSelectedText();
            StringSelection clipString = new StringSelection(sel);
            clip.setContents(clipString, clipString);
        }

    public void ActionPerformed(ActionEvent e)
    {
        if (e.getSource() == paste)
            Transferable cliptran = clip.getContents();

        try
        {
            String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor);
            tx.replaceRange(sel,tx.getSelectionStart(),tx.getSelectionEnd());
        }
        catch(Exception exc)
        {
            System.out.println("not string flavour");
        }
    }
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.lang.Math.*;
import java.text.*;
import java.io.*;
import java.awt.Scrollbar.*;
import javax.swing.*;
import java.awt.datatransfer.*;

public class DFunPaint extends JApplet implements ActionListener, AdjustmentListener, MouseListener, MouseMotionListener
{
    String filename;
    JTextArea tx = new JTextArea();
    JMenuBar mb =new JMenuBar();
    JMenu f = new JMenu("File");
    JMenuItem n = new JMenuItem("New");
    JMenuItem o = new JMenuItem("Open");
    JMenuItem s = new JMenuItem("Save");
    JMenuItem e = new JMenuItem("Exit");
    JMenu ed = new JMenu("Edit");
    JMenuItem cut = new JMenuItem("Cut");
    JMenuItem copy = new JMenuItem("Copy");
    JMenuItem paste = new JMenuItem("Paste");
    JMenu format = new JMenu("Format");
    JMenuItem  Word = new JMenuItem("Word Wrap");
    JMenuItem  Font = new JMenuItem("Font");
    JMenu view = new JMenu("View");
    JMenuItem  StatusBar= new JMenuItem("StatusBar");
    JMenu help = new JMenu("Help");
    JMenuItem  About = new JMenuItem("About");
    JMenuItem Topics = new JMenuItem("Topics");

    private JPanel p1 = new JPanel(new BorderLayout());

    public void init(){
        setLayout(new BorderLayout());
        f.add(n);
        f.add(o);
        f.add(s);
        f.add(e);
        mb.add(f);
        ed.add(cut);
        ed.add(copy);
        ed.add(paste);
        mb.add(ed);
        format.add(Word);
        format.add(Font);
        mb.add(format);
        mb.add(view);
        view.add(StatusBar);
        mb.add(help);
        help.add(About);
        JMenuItem Topics = new JMenuItem("Topics");
        help.add(Topics);

        p1.add(mb);

        add(p1,"North");

        n.addActionListener(this);
        o.addActionListener(this);
        s.addActionListener(this);
        e.addActionListener(this);
        cut.addActionListener(this);
        copy.addActionListener(this);
        paste.addActionListener(this);
        About.addActionListener(this);
        Topics.addActionListener(this);
    }

    public void ActionPerformed(ActionEvent e)
    {
        if (e.getSource() == n)
            tx.setText(" ");
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == o)
            JFileDialog fd = new JFileDialog( DFunPaint, "select File",JFileDialog.LOAD);
        fd.show();
        if (fd.getFile()!=null)
        {
            filename = fd.getDirectory() + fd.getFile();
            setTitle(filename);
            ReadFile();
        }
        tx.requestFocus();
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == s)
            FileDialog fd = new FileDialog(DFunPaint,"Save File",FileDialog.SAVE);
        fd.show();
        if (fd.getFile()!=null)
        {
            filename = fd.getDirectory() + fd.getFile();
            setTitle(filename);
            try
            {
                DataOutputStream d = new DataOutputStream(new FileOutputStream(filename));
                String line = tx.getText();
                BufferedReader br = new BufferedReader(new StringReader(line));
                while((line = br.readLine())!=null)
                {
                    d.writeBytes(line + "\r\n");
                    d.close();
                }
            }
            catch(Exception ex)
            {
                System.out.println("File not found");
            }
            tx.requestFocus();
        }
    }

    public void ctionPerformed(ActionEvent ae)
    {
        if (ae.getSource() == e)
            System.exit(0);
    }

    public void ActionPerformed(ActionEvent e)
    {
        if (e.getSource() == cut)
            String sel =
        StringSelection ss = new StringSelection(tx.getSelectedText());
        clip.setContents(ss,ss);
        tx.replaceRange(" ",tx.getSelectionStart(),tx.getSelectionEnd());
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == copy)
            String sel = tx.getSelectedText();
            StringSelection clipString = new StringSelection(sel);
            clip.setContents(clipString, clipString);
        }

    public void ActionPerformed(ActionEvent e)
    {
        if (e.getSource() == paste)
            Transferable cliptran = clip.getContents();

        try
        {
            String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor);
            tx.replaceRange(sel,tx.getSelectionStart(),tx.getSelectionEnd());
        }
        catch(Exception exc)
        {
            System.out.println("not string flavour");
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2024-11-22 17:48:34

默认情况下,Applet 部署在安全沙箱中。要访问本地文件系统,基本上有两种选择:

  1. 对代码进行数字签名并让用户在出现提示时确定信任对话框。如果小程序是“可信的”,那么它可以执行应用程序可以执行的大部分操作。当然,使用 JFileChooser 加载和保存文件。以下是使用自签名证书的可信小程序示例。该演示小程序可以浏览本地文件系统并在JEditorPane 中打开文件。
  2. 更新的 Sun/Oracle JRE 是 Plug-In2“下一代”架构。它们允许嵌入式小程序访问 JNLP API 的服务(为 JNLP API 提供)。 com/tags/java-web-start/info">Java Web Start)。使用 JNLP API 文件服务,沙盒小程序可以从本地文件系统加载资源并将资源保存到本地文件系统(同样在出现提示时需要用户批准)。这是我的 演示。 JNLP API 文件服务

请注意,我对问题的本质做了一些大胆的猜测。为了获得更好的建议,请准确说明问题所在。

拜托,以后不要写那么多代码了。 ;)

Applets are deployed in a security sand-box by default. To access the local file-system there are basically 2 options:

  1. Digitally sign the code and get the user to OK the trust dialog when prompted. If an applet is 'trusted', it can do most the things an application can do. Certainly load and save files using a JFileChooser. Here is an example of a trusted applet using a self signed certificate. The demo applet can browse the local file system and open files in the JEditorPane.
  2. More recent Sun/Oracle JREs are Plug-In2 'next generation' architecture. They allow embedded applets to access the services of the JNLP API (supplied for Java Web Start). Using the JNLP API file services, it is possible for a sand-boxed applet to load resources from, and save resources to, the local file system (again with user approval when prompted). Here is my demo. of the JNLP API file services.

Note that I made some wild guesses about the nature of the problem. For better advice, state exactly what the problem is.

And please, not so much code in future. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文