【问题标题】:get an applet into the web browser在网络浏览器中获取一个小程序
【发布时间】:2011-05-31 14:42:41
【问题描述】:

我设计了一个小程序,它显示在一个单独的 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();
 }
});
}

}

【问题讨论】:

  • 您可以使用<object> 标记来嵌入它,就像您对 Flash 或其他“外部”项目所做的那样。
  • @sutil: "我设计了一个在单独的 java 窗口中显示的小程序" 显然这段代码不是它,因为..Public class myApplet 甚至无法编译。请复制/粘贴代码,而不是将您的时间和我们的带宽浪费在“类似于”正在使用的代码上。
  • @sutil: BTW 1) 为了尽快获得更好的帮助,请发布SSCCE。 2) 你编码的是java.applet.Applet 还是javax.swing.JApplet
  • 我强烈建议您阅读有关 Applets 的 Sun 教程,否则您将在 Stack Overflow 上提出 10-15 个问题,询问显而易见的事情:P

标签: java applet browser japplet


【解决方案1】:

屏幕截图

代码

//<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>

【讨论】:

  • @sutil:您很可能希望在JOptionPane 中弹出此登录面板,或者将其添加到CardLayout 的一张卡片中。要按照您期望的方式布局组件,我建议在标签/BorderLayoutCENTER 中包含 GroupLayoutSpringLayout 的嵌套布局/字段对 - 按钮位于 BorderLayoutSOUTH 中的居中 FlowLayout
【解决方案2】:

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

【讨论】:

  • 谢谢,朋友们!你真的很快! :) Slaks,恐怕我不明白“将您的控件直接放在 JApplet 实例(this)中”。你的意思是?对不起,我是小程序的新手:S
  • @sutil:使用JApplet 实例(即this)而不是创建JFrame
【解决方案3】:

Click here 获取有关如何在 Web 浏览器中显示小程序的示例源代码。

谢谢 迪帕克

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-16
    • 2012-10-30
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 2010-10-23
    • 2011-02-25
    相关资源
    最近更新 更多