java - 窗体运行时,执行其他命令

标签 java forms swing

我写了一个方法来创建一个表单,然后在 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


    }
}

我想执行“System.out.println("test***ok");"之后表格关闭。 但是当我运行程序时,在我以表格形式输入信息之前,执行的其他命令! 当表单运行时,其他命令被执行!我该如何设置它。

最佳答案

你走错了路。

这是一个带有注释的例子:

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
    }
}

关于java - 窗体运行时,执行其他命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4171159/

相关文章:

java 8 spring spEL 可重复绑定(bind)减慢每次迭代

java - 如何使用模式验证我的 XML,JAXB 的 XMLStreamReader 一次只读取一个对象/元素?

java - 快速将 int[] 写入磁盘?

php - 在php中检查密码确认

java - 完成操作后关闭 Swing 对话框

java - 为什么 java.awt.Graphics.drawLine 异常慢?

php - 表单、Mysql 和值

reactjs - 如何显示自动完成错误

Java 帮助。如何绘制图像

java - 如何更改jTable中列的颜色?