gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:52:38 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com