can not handle swing button event when run form applet
This is my applet
public class TestApplet extends Applet{
public void init(){
}
public void start(){
Swsmall b = new Swsmall();
}
}
This is my Swsmall file
public Swsmall() {
JFrame frame = new JFrame ("Stand alone");
JButton jl = new JButton("Exits properly");
frame.getContentPane().add(jl);
frame.setSize(180,80);
frame.setVisible(true);
jl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);}});
}
this my jsp file
<body>
<applet code="TestApplet.class" width="400" height="400"></applet>
</body>
I am able to run applet su开发者_如何学编程ccessfully but I can't get any responce on button click event
When I run same application on java console it works perfect
Calling System.exit(0) from Java applet will not destroy an applet. Try calling something else from action listener (i.e. System.out.println("something"); would print in Java applet console), and you'll see that it's called correctly, but in this case it probably doesn't work as you expected it to work.
frame.dispose();
Code for creating the JFrame should be placed in the init() method. You should also be using the invokeAndWait() method.
Read the section from the Swing tuorial on How to Make Applets for more information and a working example.
精彩评论