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>
我在编译时没有错误。我正在尝试为ActionListeners使用匿名类,但这是我第一次使用它们。当我启动Chrome时,所有显示的都是白色背景。任何帮助都会很好。感谢。
答案 0 :(得分:4)
我通常使用appletviewer来调试applet。将以下行添加到程序顶部并重新编译:
//<applet code="StockGUI.class" width="500" height="300"></applet>
然后,您可以使用以下命令从命令行测试applet:
appletviewer StockGUI.java
您可以添加调试语句以查看发生的情况。我这样做时会看到密码提示。
答案 1 :(得分:2)
检查Java控制台。它可能会导致失败。