Java JApplet 将不再显示在任何浏览器中
Applet 以前可以用,但现在不行了。我对 GUI 的经验不太丰富,更不用说 Applet 了。 这是 GUI 代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class StockGUI extends JApplet implements ActionListener
{
private final String sPASS = "abc123";
//Stock oStock = new Stock();
//StockInput oStockInput = new StockInput();
//////////////////////////////////////////////////////////
JPanel jPanel;
JPasswordField jPass;
JButton jBEnter, jBYes, jBNo;
JTextArea jTAInput, jTACurrent;
JTextField jTxtField;
JLabel jLEnterP, jLQue, jLCurrent;
JScrollPane jScroll;
public void init()
{
getContentPane().setLayout(null);
jPanel = new JPanel(); jPanel.setSize(500,1500);
////////////////////////////////////////////////
jPass = new JPasswordField(20);
jPass.setActionCommand(sPASS);
jLEnterP = new JLabel("Enter Password:");
jPanel.add(jLEnterP);
jPanel.add(jPass);
jPass.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals(sPASS))
{
char[] cAry = jPass.getPassword();
if(isPassword(cAry))
{
doPass();
}
}
}
});
getContentPane().add(jPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals("Yes"))
{
doInput();
}
if(cmd.equals("No") || cmd.equals("Done"))
{
doMain();
}
}
public void doPass()
{
jPanel.removeAll();
jLQue = new JLabel("Do you need to input more data?");
jBYes = new JButton("Yes");
jBYes.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doInput();
}
});
jBNo = new JButton("No");
jBNo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doMain();
}
});
jPanel.add(jLQue);
jPanel.add(jBYes);
jPanel.add(jBNo);
refresh();
}
public void doInput()
{
jPanel.removeAll();
jLQue = new JLabel("Enter Data:");
jTAInput = new JTextArea(25,50);
jLCurrent = new JLabel("Current Data");
jTACurrent = new JTextArea(25, 25);
jTACurrent.setEditable(false);
//jTACurrent.setText(oStockInput.getList());
jScroll = new JScrollPane(jTACurrent);
jPanel.add(jLQue);
jPanel.add(jTAInput);
refresh();
}
public void doMain()
{
jPanel.removeAll();
jPanel.add(new JLabel("HELLO WORKD!!"));
refresh();
}
public void refresh()
{
jPanel.repaint();
jPanel.revalidate();
super.validate();
super.repaint();
}
public boolean isPassword(char[] cAry)
{
return Arrays.equals(cAry, sPASS.toCharArray());
}
HTML 代码:
// <html>
//<body>
//<p align=center>
//<applet code="StockGUI.class" width=500 height=1500>
// </applet>
//</p>
//</body>
//</html>
我在编译时没有收到错误。我正在尝试对 ActionListener 使用匿名类,但这是我第一次使用它们。当我启动 Chrome 时,它显示的只是白色背景。任何帮助都会很好。谢谢。
Applet used to work, but now it will not. I am not very experienced with GUI's much less Applets.
Here is the GUI code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class StockGUI extends JApplet implements ActionListener
{
private final String sPASS = "abc123";
//Stock oStock = new Stock();
//StockInput oStockInput = new StockInput();
//////////////////////////////////////////////////////////
JPanel jPanel;
JPasswordField jPass;
JButton jBEnter, jBYes, jBNo;
JTextArea jTAInput, jTACurrent;
JTextField jTxtField;
JLabel jLEnterP, jLQue, jLCurrent;
JScrollPane jScroll;
public void init()
{
getContentPane().setLayout(null);
jPanel = new JPanel(); jPanel.setSize(500,1500);
////////////////////////////////////////////////
jPass = new JPasswordField(20);
jPass.setActionCommand(sPASS);
jLEnterP = new JLabel("Enter Password:");
jPanel.add(jLEnterP);
jPanel.add(jPass);
jPass.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals(sPASS))
{
char[] cAry = jPass.getPassword();
if(isPassword(cAry))
{
doPass();
}
}
}
});
getContentPane().add(jPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals("Yes"))
{
doInput();
}
if(cmd.equals("No") || cmd.equals("Done"))
{
doMain();
}
}
public void doPass()
{
jPanel.removeAll();
jLQue = new JLabel("Do you need to input more data?");
jBYes = new JButton("Yes");
jBYes.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doInput();
}
});
jBNo = new JButton("No");
jBNo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doMain();
}
});
jPanel.add(jLQue);
jPanel.add(jBYes);
jPanel.add(jBNo);
refresh();
}
public void doInput()
{
jPanel.removeAll();
jLQue = new JLabel("Enter Data:");
jTAInput = new JTextArea(25,50);
jLCurrent = new JLabel("Current Data");
jTACurrent = new JTextArea(25, 25);
jTACurrent.setEditable(false);
//jTACurrent.setText(oStockInput.getList());
jScroll = new JScrollPane(jTACurrent);
jPanel.add(jLQue);
jPanel.add(jTAInput);
refresh();
}
public void doMain()
{
jPanel.removeAll();
jPanel.add(new JLabel("HELLO WORKD!!"));
refresh();
}
public void refresh()
{
jPanel.repaint();
jPanel.revalidate();
super.validate();
super.repaint();
}
public boolean isPassword(char[] cAry)
{
return Arrays.equals(cAry, sPASS.toCharArray());
}
The HTML Code:
// <html>
//<body>
//<p align=center>
//<applet code="StockGUI.class" width=500 height=1500>
// </applet>
//</p>
//</body>
//</html>
I get no errors on compile. I am trying to use Anonymous Classes for the ActionListeners, but this is the first time I have used them. When I launch it Chrome, all it shows is a white background. Any help would be good. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常使用 appletviewer 来调试 applet。将以下行添加到程序顶部并重新编译:
然后您可以使用以下命令从命令行测试小程序:
appletviewer StockGUI.java
您可以添加调试语句来查看发生了什么。当我执行此操作时,我会看到输入密码的提示。
I usually debug applets by using appletviewer. Add the following line to the top of your program and recompile:
Then you can test the applet from the command line using:
appletviewer StockGUI.java
You can add debug statements to see what is happening. I see a prompt for the password when I do this.
检查 Java 控制台。它可能会提供失败的原因。
Check the Java Console. It is likely to provide the cause of the failure.