开发者

my program exits from close button of window but not from my exit button.help me i have used System.exit(0).tell me another way

Here's my code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
import java.util.*;
import java.applet.*;
/*<applet code=tabledemo.class height=300 width=300></applet>*/

public class tabledemo extends JApplet implements ActionListener
{
    JScrollPane jsp1;
    JPanel jp,jp1,jp2,jp3;
    JTable jt1;
    int i,j;
    DefaultTableModel tm=new DefaultTableModel(3,3);
    String data[][]={{"1001","Ram","BCA"},
            {"1002","Sham","MCA"},
            {"1003","Mohan","BCA"}};

    String head[]={"Rollno","Name","Course"};
    JTextField jtf1,jtf2,jtf3;
    JButton b1,b2,b3,b4;

    public void init()
    {
        jp=new JPanel();
        jp.setLayout(new BorderLayout());
        jp1=new JPanel();
        jp1.setLayout(new BorderLayout());
        jp2=new JPanel();
        jp2.setLayout(new FlowLayout());
        jp3=new JPanel();
        jp3.setLayout(new FlowLayout());

        jt1=new JTable();
        jt1.setModel(tm);

        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
                tm.setValueAt(data[i][j],i,j);
            }
        }


        jp1.add(jt1,BorderLayout.CENTER);
        jp.add(jp1,BorderLayout.CENTER);

        jtf1=new JTextField(25);    
        jtf2=new JTextField(25);    
        jtf3=new JTextField(25);    
        jp2.add(jtf1);
        jp2.add(jtf2);
        jp2.add(jtf3);


        b1=new JButton("Add Row");  
        b2=new JButton("Display Count");
        b3=new JButton("Cours开发者_StackOverflow社区ewise Count");
        b4=new JButton("Exit");
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);

        jp1.add(jp2,BorderLayout.SOUTH);
        jp3.add(b1);
        jp3.add(b2);
        jp3.add(b3);
        jp3.add(b4);
        jp.add(jp3,BorderLayout.SOUTH);
        int v,h;

        v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

        jsp1=new JScrollPane(jp,v,h);
        Container c=getContentPane();
        c.add(jsp1);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getActionCommand().equals("Add Row"))
        {
            String [] r={jtf1.getText(),jtf2.getText(),jtf3.getText()};
            tm.addRow(r);
        }
        else if(ae.getActionCommand().equals("Display Count"))
        {
            String name;
            name=JOptionPane.showInputDialog("enter student name:");
            jtf1.setText(name);
        }
        else if(ae.getActionCommand().equals("Exit"))
        {
            int ans=0;
            ans=JOptionPane.showConfirmDialog(null,"Are you sure");
            if(ans==JOptionPane.YES_OPTION)
            {
                dispose();
                System.exit(0);
                //processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
            }

        }
    }
}


As Daniel already said, you cannot close the browser from your applet, as it is not allowed for security reasons.

The only possibilities you have are to use the showDocument method to redirect to a new website, or you simply hide your applet by using swing methods (eg. setVisible).

You could also let your applet create a new frame which can be closed!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜