Java 将小程序放入web浏览器

Java 将小程序放入web浏览器,java,applet,browser,japplet,Java,Applet,Browser,Japplet,我设计了一个小程序,它显示在一个单独的java窗口中(也会显示一个空白的web浏览器窗口),但我希望它显示在web浏览器中。我对此一无所知。我应该改变JFrame还是不同的东西? 我的代码如下: Public class myApplet extends Applet implements ActionListener{ public JPanel createContentPane (){ System.out.println("1"); // We create a b

我设计了一个小程序,它显示在一个单独的java窗口中(也会显示一个空白的web浏览器窗口),但我希望它显示在web浏览器中。我对此一无所知。我应该改变JFrame还是不同的东西? 我的代码如下:

Public class myApplet extends Applet implements  ActionListener{

public JPanel createContentPane (){

    System.out.println("1");
    // We create a bottom JPanel to place everything on.
    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    titleLabel = new JLabel("Login");
    totalGUI.add(titleLabel);

    // Creation of a Panel to contain the JLabels
    textPanel = new JPanel();
    textPanel.setLayout(null);
    totalGUI.add(textPanel);

    // Usuario Label
    usuarioLabel = new JLabel("User");
    textPanel.add(usuarioLabel);

    // Password nuevo Label
    passwordLabel = new JLabel("Password");
    passwordLabel.setHorizontalAlignment(4);
    textPanel.add(passwordLabel);


    // TextFields Panel Container
    panelForTextFields = new JPanel();
    panelForTextFields.setLayout(null);
    totalGUI.add(panelForTextFields);

    // Password viejo Textfield
    usuarioField = new JTextField(8);
    panelForTextFields.add(usuarioField);

    // Password nuevo Textfield
    passwordField = new JPasswordField(8);
    panelForTextFields.add(passwordField);

    // Button for Logging in
    loginButton = new JButton("Restore");
    loginButton.addActionListener(this);
    totalGUI.add(loginButton);
    totalGUI.setOpaque(true);

    return totalGUI;
}


public void actionPerformed(ActionEvent e) {
    //restores password

    }

private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Change password");
    myApplet demo = new myApplet ();
    frame.setContentPane(demo.createContentPane());

    frame.setSize(310, 400);
    frame.setVisible(true);

}

public void init (){
System.out.println("Applet initializing");
final myApplet rp = new myApplet ();
 SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        rp.createAndShowGUI();
 }
});
}

}

您应该扩展
JApplet
,并将控件直接放在
JApplet
实例(
this
)中。

获取如何在web浏览器中显示小程序的示例源代码

谢谢 Deepak

屏幕截图

密码
您可以使用
标记来嵌入它,就像您嵌入Flash或其他“外部”项目一样。@sutil:“我设计了一个小程序,它显示在单独的java窗口中”显然,这段代码不是它,因为<代码>公共类myApplet甚至无法编译。请复制/粘贴代码,而不是将您的时间和带宽浪费在正在使用的代码上。@sutil:BTW 1)为了更快地获得更好的帮助,请发布一篇文章。2) 你是在编写一个
java.applet.applet
还是一个
javax.swing.JApplet
?我强烈建议你阅读Sun关于applet的教程,否则你会对堆栈溢出提出10-15个问题,问一些显而易见的问题:谢谢,朋友!你真快!:)Slaks,恐怕我不理解“将控件直接放在JApplet实例中(这个)”。什么意思?对不起,我是小程序的新手:S@sutil:使用
JApplet
实例(即
this
)而不是创建
JFrame
@sutil:很可能您希望在
JOptionPane
中弹出此登录面板,或将其添加到
CardLayout
的一张卡中。要按预期布局构件,请执行以下操作:,对于标签/字段对,我建议在
边界布局的
中心
组布局
SpringLayout
组成嵌套布局,按钮位于
边界布局
南部
的中心
//<applet code='myApplet' width=220 height=100></applet>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** This was terrible code.  You should take it back to whoever gave
it to you, and throw it at them.  Never get code from them again. */
public class myApplet extends JApplet implements  ActionListener{

    private JLabel titleLabel;
    private JLabel usuarioLabel;
    private JLabel passwordLabel;
    private JPanel textPanel;
    private JPanel panelForTextFields;
    private JTextField usuarioField;
    private JPasswordField passwordField;
    private JButton loginButton;

    public JPanel createContentPane (){
        System.out.println("1");
        // We create a bottom JPanel to place everything on.
        JPanel totalGUI = new JPanel();
        // Use LAYOUTS!
        totalGUI.setLayout(new FlowLayout());

        titleLabel = new JLabel("Login");
        totalGUI.add(titleLabel);

        // Creation of a Panel to contain the JLabels
        textPanel = new JPanel();
        totalGUI.add(textPanel);

        // Usuario Label
        usuarioLabel = new JLabel("User");
        textPanel.add(usuarioLabel);

        // Password nuevo Label
        passwordLabel = new JLabel("Password");
        passwordLabel.setHorizontalAlignment(4);
        textPanel.add(passwordLabel);

        // TextFields Panel Container
        panelForTextFields = new JPanel();
        totalGUI.add(panelForTextFields);

        // Password viejo Textfield
        usuarioField = new JTextField(8);
        panelForTextFields.add(usuarioField);

        // Password nuevo Textfield
        passwordField = new JPasswordField(8);
        panelForTextFields.add(passwordField);

        // Button for Logging in
        loginButton = new JButton("Restore");
        loginButton.addActionListener(this);
        totalGUI.add(loginButton);
        totalGUI.setOpaque(true);

        return totalGUI;
    }

    public void actionPerformed(ActionEvent e) {
        //restores password
    }

    private void createAndShowGUI() {
        add( createContentPane() );
        validate();
    }

    public void init (){
        System.out.println("Applet initializing");
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
prompt>appetviewer myApplet.java
Applet initializing
1
prompt>