【发布时间】: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