简体   繁体   English

在 web 浏览器中获取一个小程序

[英]get an applet into the web browser

I have designed an applet that is shown in a separate java window (and a blank web browser window also appears) but i'd like it to be displayed in the web browser. I have designed an applet that is shown in a separate java window (and a blank web browser window also appears) but i'd like it to be displayed in the web browser. I have no clue about it.我对此一无所知。 Should I change the JFrame or is it different stuff?我应该更改 JFrame 还是不同的东西? My code is as follows:我的代码如下:

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();
 }
});
}

}

Screen Shot截屏

AppletViewer 中的 myApplet

Code代码

//<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();
            }
        });
    }
}

To Run跑步

prompt>appetviewer myApplet.java
Applet initializing
1
prompt>

You should extend JApplet and put your controls directly in the JApplet instance ( this ).您应该扩展JApplet并将您的控件直接放在JApplet实例 ( this ) 中。

Click here to get sample source on how to show an applet in web browser.单击此处获取有关如何在 web 浏览器中显示小程序的示例源。

Thanks Deepak谢谢迪帕克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM