Java 当窗体运行时,将执行其他命令
我编写了一个创建表单的方法,然后在main中编写了一些其他命令 我想执行“System.out.println(“test***ok”);”,然后关闭表单。 但当我运行程序时,在我将信息输入表单之前,执行其他命令!Java 当窗体运行时,将执行其他命令,java,forms,swing,Java,Forms,Swing,我编写了一个创建表单的方法,然后在main中编写了一些其他命令 我想执行“System.out.println(“test***ok”);”,然后关闭表单。 但当我运行程序时,在我将信息输入表单之前,执行其他命令! 当窗体运行时,将执行其他命令!如何设置它。在代码中,main()函数是调用函数,form6()是被调用函数。从main()函数调用form6()函数后,它将返回main()函数。记住这一点,在被调用函数执行后,控件总是返回到调用函数。我不确定表单背后的实现是什么,但是如果不希望稍后立
当窗体运行时,将执行其他命令!如何设置它。在代码中,main()函数是调用函数,form6()是被调用函数。从main()函数调用form6()函数后,它将返回main()函数。记住这一点,在被调用函数执行后,控件总是返回到调用函数。我不确定表单背后的实现是什么,但是如果不希望稍后立即执行代码,则需要它来阻止输入,也许是使用扫描仪?你在这方面走错了方向 下面是一个带有注释的示例:
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
}
}
在继续之前,您需要了解关于Swing和Frames的两个重要方面:
SwingUtilities.invokeLater
和SwingUtilities.invokeAndWait
正确执行此操作JFrame
s是独立的元素。使一个线程可见不会阻止调用线程。但是,如果您想这样做,那么就使用JDialog
。对话框是为阻止和等待用户输入而设计的,如果您创建一个模式对话框,使其可见将阻止调用线程(如果您设置一个父框架
或对话框
,那么您也可以使其保持在顶部)。一个很好的例子是JOptionPane
(试一试!),除此之外,JDialog
扩展Dialog
而不是Frame
,它几乎是相同的,您可以向基本对话框添加任何想要的元素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);
}
});
}
我希望这能回答你的问题 …除非抛出异常且未捕获。“被调用函数执行后,控件始终返回调用函数。”如何将其设置为“被调用函数完全返回其值后,控件返回调用函数”。我不明白您到底在问什么。更准确一点。你是什么意思?؟؟؟“你需要它来阻止输入”??扫描仪?输出为:单击testok按钮!我想先单击按钮,然后会显示一条消息,首先是“button clicked!”,然后是“testok”??!!!也许你应该先读一本好的java书,或者浏览java教程,先学习一些基本的OOP知识。我有一个表单,当用户输入信息并单击sumbit时,信息应该传递给其他指令。例如,用户在文本框中输入数字。这个数字保存在全局变量中,下一条指令使用它。但当窗体运行时,将执行下一条指令。有没有办法做到这一点?我想在表单关闭后运行其他指令。请将这些指令放入
actionPerformed()
方法中,如示例所示
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);
}
});
}