| |

VerySource

 Forgot password?
 Register
Search
View: 742|Reply: 5

The problem of closing both the applet and application java programs.

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-11 14:30:01
| Show all posts |Read mode
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);
  }

}
Reply

Use magic Report

0

Threads

18

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-6-4 17:15:01
| Show all posts
Your idea is wrong

Even if the applet is an application, two low-level containers are needed. One is JFrame and one is JApplet
When it is started by the main function, it is displayed by JFrame, and it is started by JApplet when it is started by the page
In other words, you need a container JPanel, put all the interfaces in this JPanel, and then put this JPanel in JFrame or JApplet when needed


I don't know if you understand what I mean. As far as the interface program itself is concerned, it is neither an Applet nor a Frame, but a panel, but it only needs to be displayed in any way, so let it be ok as its container.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-5 17:15:01
| Show all posts
I understand it, but now I first want to know how to close this program. Clicking the X button in Windows has no effect. Do you know how to get the message of clicking the X button in Windows?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-7 15:00:01
| Show all posts
Use JFrame:

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.*;
import javax.swing.JFrame;

/**
* <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);
      }
    });

  }
  protected void processWindowEvent(WindowEvent e) {
      System.out.print("hello");
      System.exit(0);
  }

//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;
    JFrame frame;
    frame = new JFrame();
    frame.setTitle("Applet Frame");
    frame.getContentPane().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);
    //frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  void jButton2_actionPerformed(ActionEvent e) {
    stradacTab.setSelectedIndex(1);
  }

  void jButton1_actionPerformed(ActionEvent e) {
    stradacTab.setSelectedIndex(0);
  }

}
Reply

Use magic Report

0

Threads

18

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-6-7 20:45:02
| Show all posts
Okay, only solve your exit problem
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-7 23:30:01
| Show all posts
it works, 3q
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list