开发者

While the form is running, other commands are executed

开发者 https://www.devze.com 2023-01-24 17:09 出处:网络
i write a method to create a form, then some other commands after that in main.(java) package pak2; import javax.swing.*;

i write a method to create a form, then some other commands after that in main.(java)

package pak2;
import javax.swing.*;

public class form6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

          JFrame jframe = new JFrame();

          JButton jButton = new JButton("JButton");

          jframe.getContentPane().add(jButton);

          jframe.pack();
          jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jframe.setVisible(true);

            System.out.println("test***ok");//testtttttttttttttt


    }
}

i want to execute "System.out.println("test***ok");" after that the form are closed. but when i run program, before i enter information in form, Other commands开发者_StackOverflow社区 that are executed! While the form is running, other commands are executed! how can i set it.


You're going the wrong way about this.

Here's an example with comments:

public class Form2  {

    public static void main(String[] args) {

        final JFrame jframe = new JFrame();

        final JButton jButton = new JButton("JButton");

        /**
         * Once you create a JFrame, the frame will "listen" for events.
         * If you want to do something when the user clicks a button for example
         * you need to add an action listener (an object that implements ActionListener)
         * 
         * One way of doing this is by using an anonymous inner class:
         */
        jButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource().equals(jButton)){
                    jframe.dispose();
                    System.out.println("Button clicked!");
                }

            }
        });

        jframe.getContentPane().add(jButton);
        jframe.pack();
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // displaying the form will NOT block execution
        jframe.setVisible(true);
        System.out.println("test***ok");// testtttttttttttttt
    }
}


There are two important things you need to know about Swing and Frames before continuing:

  1. Constructing components and calling methods on components must always be done on the Event Dispatch Thread (EDT). This article explains the principle of the single-thread rule in Swing. Note that this rule also applies to the Main thread. Use SwingUtilities.invokeLater and SwingUtilities.invokeAndWait to do this correctly.
  2. JFrames are independent elements. Making one visible will not block the calling thread. However, if that's what you want to do, then use a JDialog. Dialogs are designed for blocking and waiting for user input, and if you create a modal dialog, making it visible will block the calling thread (and if you set a parent Frame or Dialog, then you can make it stay on top too). A good example of this is JOptionPane (give it a try!) Other than that, and the fact that JDialog extending Dialog instead of Frame, it is almost identical and you can add whatever elements you want to a base dialog.

Some example code:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            // The boolean parameter 'modal' makes this dialog block.
            JDialog d = new JDialog((Frame) null, true);
            d.add(new JLabel("This is my test dialog."));
            d.setLocationRelativeTo(null);
            d.pack();

            System.out.println("Dialog is opening...");
            d.setVisible(true);
            System.out.println("Dialog is closed.");
            System.exit(0);
        }
    });
}

I hope that answers your question. :)


In your code main() function is the calling function and form6() is the called function. After the form6() function is called from the main() function it returns back to the main() function. Remember this the control always returns back to the calling function after the called function is executed.


I'm not sure what the implementation behind your form is, but you'll need it to block for input if you don't want it later code to be executed immediately afterward, maybe using Scanner?

0

精彩评论

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