我如何将运行东西作为小程序和应用程序?

How do I run something as an applet and application?

我正在处理这个项目,我需要它 运行 作为小程序和应用程序。这就是我所拥有的,但我坚持要去哪里,因为我在互联网上找不到任何东西。是否有任何资源或有人可以给我一些快速建议?

public class Project extends JApplet {

public void init() {

    try {
        URL pictureURL = new URL(getDocumentBase(), "sample.jpg");
        myPicture = ImageIO.read(pictureURL);
        myIcon = new ImageIcon(myPicture);
        myLabel = new JLabel(myIcon);
    } catch (Exception e) {
        e.printStackTrace();
    }
    add(label, BorderLayout.NORTH);

    add(bio);
    add(bio, BorderLayout.CENTER);

    pane.add(play);
    getContentPane().add(pane, BorderLayout.SOUTH);
    play.addActionListener(new  ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{
                FileInputStream FIS = new FileInputStream("sample.mp3");
                player = new Player (FIS);
                player.play();
            } catch (Exception e1) {
                e1.printStackTrace();
            }}});
}

public static void main(String args[]) {
    JFrame frame = new JFrame("");
    frame.getContentPane().add();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.show();
}

private JPanel pane = new JPanel();
private TextArea bio = new TextArea("Bio");
private JButton play = new JButton("Play");
private Image myPicture;
private ImageIcon icon;
private JLabel label;
private Player player;

}

当尝试 运行 作为小程序和应用程序时,有几个注意事项。

小程序有一定的life cycle必须遵守。可以将小程序添加到 JFrame 的内容窗格并手动调用 init(),但一般来说,如果小程序希望调用其 start()stop() 方法,事情可能会变得棘手...

更重要的是:applet 和应用程序处理资源的方式不同。

在小程序中处理文件(例如使用 FileInputStream)可能会带来安全隐患,并且在某些情况下显然不起作用 - 例如当小程序嵌入网站时。 (另见 What Applets Can and Cannot Do)。

相反,当 运行将此作为应用程序时,调用 getDocumentBase() 没有意义。根本就没有"document base"申请。


不过,可以编写一个可以显示为小程序或应用程序的程序。主要区别在于主 JPanel 是放入 JApplet 还是放入 JFrame,以及如何读取数据。

一种读取适用于小程序和应用程序的数据的方法是通过 getClass().getResourceAsStream("file.txt"),前提是相应的文件位于 class 路径中。

我犹豫了一会儿,我是否应该 post 针对主要问题的示例,或者我是否应该修改您的代码以使其工作。两者我都会:

这是一个可以作为小程序或应用程序执行的示例。它将读取并显示 "sample.jpg"。 (这个文件目前基本上预计是"in the same directory as the .class-file"。关于资源处理的更多细节,class路径和流处理超出了这个答案的范围)

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class AppletOrApplicationExample extends JApplet
{
    @Override
    public void init()
    {
        add(new AppletOrApplicationMainComponent());
    }

    public static void main(String args[])
    {
        JFrame frame = new JFrame("");
        frame.getContentPane().add(new AppletOrApplicationMainComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

class AppletOrApplicationMainComponent extends JPanel
{
    public AppletOrApplicationMainComponent()
    {
        super(new BorderLayout());

        InputStream stream = getClass().getResourceAsStream("sample.jpg");
        if (stream == null)
        {
            add(new JLabel("Resource not found"), BorderLayout.NORTH);
        }
        else
        {
            try
            {
                BufferedImage image = ImageIO.read(stream);
                add(new JLabel(new ImageIcon(image)), BorderLayout.NORTH);
            }
            catch (IOException e1)
            {
                add(new JLabel("Could not load image"), BorderLayout.NORTH);
            }
        }

        JTextArea textArea = new JTextArea("Text...");
        add(textArea, BorderLayout.CENTER);

        JButton button = new JButton("Button");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                doSomething();
            }
        });
        add(button, BorderLayout.SOUTH);
    }

    private void doSomething()
    {
        System.out.println("Button was clicked");
    }
}


这里仍然有点接近您的原始代码。但是,我 强烈 建议尽可能分解出实际的应用程序逻辑。例如,您的主要 GUI 组件应该 而不是 小程序本身,而是 JPanel。不应通过 FileInputStreams 或文档库中的 URL 直接读取资源,而只能从 InputStreams 读取资源。这基本上是您 post 编写的代码,只需 最少的修改即可将其 运行 作为小程序或应用程序

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Module5Assignment2 extends JApplet
{
    public void init()
    {
        try
        {
            InputStream stream = getClass().getResourceAsStream("sample.jpg");
            if (stream == null)
            {
                System.out.println("Resource not found");
            }
            else
            {
                myPicture = ImageIO.read(stream);
                icon = new ImageIcon(myPicture);
                label = new JLabel(icon);
                add(label, BorderLayout.NORTH);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        add(bio);
        add(bio, BorderLayout.CENTER);

        pane.add(play);
        getContentPane().add(pane, BorderLayout.SOUTH);
        play.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileInputStream FIS = new FileInputStream("sample.mp3");
                    // player = new Player (FIS);
                    // player.play();
                }
                catch (Exception e1)
                {
                    e1.printStackTrace();
                }
            }
        });
    }

    public static void main(String args[])
    {
        JFrame frame = new JFrame("");
        // ******PRETTY SURE I NEED TO ADD SOMETHING HERE*************
        Module5Assignment2 contents = new Module5Assignment2();
        frame.getContentPane().add(contents);
        contents.init();
        // *************************************************************

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.show();
    }

    private JPanel pane = new JPanel();
    private TextArea bio = new TextArea(
        "This is the bio of Christian Sprague; he doesn't like typing things.");
    private JButton play = new JButton("Play");
    private Image myPicture;
    private ImageIcon icon;
    private JLabel label;
    // private Player player;

}