|
There is a java program that is both an applet and an application. When running as an application, when I click the X of a windows window, the program cannot be closed, but the process can only be closed. What statement can be added to close the program using windows X?
code show as below:
import java.awt. *;
import java.awt.event. *;
import java.applet. *;
import javax.swing. *;
import java.awt. *;
import java.awt.event. *;
import javax.swing. *;
import javax.swing.event. *;
/ **
* <p> Title: </ p>
* <p> Description: </ p>
* <p> Copyright: Copyright (c) 2016 </ p>
* <p> Company: </ p>
* @author not attributable
* @version 1.0
* /
public class Applet1 extends Applet {
private boolean isStandalone = false;
JTabbedPane stradacTab = new JTabbedPane ();
JPanel jPanel1 = new JPanel ();
JPanel jPanel2 = new JPanel ();
JScrollPane jScrollPane1 = new JScrollPane ();
JTextField jTextField1 = new JTextField ();
JComboBox jComboBox1 = new JComboBox ();
JTextPane jTextPane1 = new JTextPane ();
JButton jButton1 = new JButton ();
JTextArea jTextArea1 = new JTextArea ();
JToggleButton jToggleButton1 = new JToggleButton ();
JButton jButton2 = new JButton ();
JTextArea jTextArea2 = new JTextArea ();
JEditorPane jEditorPane1 = new JEditorPane ();
// Get a parameter value
public String getParameter (String key, String def) {
return isStandalone? System.getProperty (key, def):
(getParameter (key)! = null? getParameter (key): def);
}
// Construct the applet
public Applet1 () {
}
// Initialize the applet
public void init () {
try {
jbInit ();
}
catch (Exception e) {
e.printStackTrace ();
}
}
// Component initialization
private void jbInit () throws Exception {
this.setLayout (null);
stradacTab.setBounds (new Rectangle (40, 18, 317, 260));
jPanel1.setBounds (new Rectangle (6, 51, 31, 52));
jPanel1.setLayout (null);
jPanel2.setBounds (new Rectangle (1, 129, 38, 87));
jButton1.setBounds (new Rectangle (56, 143, 93, 47));
jButton1.setText ("jButton1");
jButton1.addActionListener (new java.awt.event.ActionListener () {
public void actionPerformed (ActionEvent e) {
jButton1_actionPerformed (e);
}
});
jButton2.setText ("jButton2");
jButton2.addActionListener (new java.awt.event.ActionListener () {
public void actionPerformed (ActionEvent e) {
jButton2_actionPerformed (e);
}
});
jPanel1.add (jButton1, null);
jPanel2.add (jButton2, null);
this.add (stradacTab, null);
stradacTab.add ("druhy", jPanel2);
stradacTab.add ("prvni", jPanel1);
stradacTab.addChangeListener (new ChangeListener () {
public void stateChanged (ChangeEvent e) {
int index = stradacTab.getSelectedIndex ();
String title = stradacTab.getTitleAt (index);
System.out.println ("index =" +
index);
System.out.println ("title =" +
title);
}
});
}
// Start the applet
public void start () {
}
// Stop the applet
public void stop () {
}
// Destroy the applet
public void destroy () {
}
// Get Applet information
public String getAppletInfo () {
return "Applet Information";
}
// Get parameter info
public String [] [] getParameterInfo () {
return null;
}
// Main method
public static void main (String [] args) {
Applet1 applet = new Applet1 ();
applet.isStandalone = true;
Frame frame;
frame = new Frame ();
frame.setTitle ("Applet Frame");
frame.add (applet, BorderLayout.CENTER);
applet.init ();
applet.start ();
frame.setSize (400,320);
Dimension d = Toolkit.getDefaultToolkit (). GetScreenSize ();
frame.setLocation ((d.width-frame.getSize (). width) / 2, (d.height-frame.getSize (). height) / 2);
frame.setVisible (true);
}
void jButton2_actionPerformed (ActionEvent e) {
stradacTab.setSelectedIndex (1);
}
void jButton1_actionPerformed (ActionEvent e) {
stradacTab.setSelectedIndex (0);
}
} |
|