java 代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AppletTest extends JApplet implements ActionListener
{
private String colorOn;
private boolean val = false;
private String msg ="";
private JButton btn = new JButton("Change Color");
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == btn)
{
val = !val;
repaint();
}
}
public void init()
{
Container ct = getContentPane();
ct.setLayout(new FlowLayout());
ct.add(btn);
btn.addActionListener(this);
msg = getParameter("msg");
colorOn = getParameter("colorOn");
if(colorOn.equals("true"))
val = true;
}
public void paint(Graphics g)
{
if(val == true)
g.setColor(Color.RED);
else
g.setColor(Color.BLUE);
g.drawString(msg,50,50);
}
public void setParameter(String paramName,String paramValue)
{
if(paramName.equals("colorOn"))
val = !val;
repaint();
}
public String getMsg()
{
return msg;
}
public int add(String firstNum,String secondNum)
{
int result = Integer.parseInt(firstNum) + Integer.parseInt(secondNum);
JOptionPane.showMessageDialog(this,""+result);
return result;
}
}
.HTML 代码如下:
<APPLET CODE="AppletTest.class" WIDTH="300" HEIGHT="200" name="byteChart">
<param name="colorOn" value="sdddd">
<param name=msg value="Parameter from Html">
</APPLET>
<script>
function ColorOn()
{
byteChart.setParameter("colorOn","true");
byteChart.add("500","82");
}
</script>
<input type="button" value="Set Color" οnclick="ColorOn()">