[英]Jframe disappears in @Test JUnit
I am doing some automation using JUnit 4 with Java and now I want to add some graphic mode using Jframe, I have this code in @Test (gui is the JFrame object): 我正在使用Java的JUnit 4进行一些自动化,现在我想使用Jframe添加一些图形模式,我在@Test中有以下代码(gui是JFrame对象):
@Test
public void initData() {
try{
setLayout(new FlowLayout());
gui.setTitle("Linkedin Candidates Search Tool");
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(400, 200);
gui.getContentPane().setLayout(null);
gui.add(ScreenTools.createLabel("Enter password: ", 5, 80, 110, 15));
textopassword = ScreenTools.createTextField(120, 80, 150, 20,"");
gui.add(textopassword);
boton = ScreenTools.createButton("Generate",5,120,60,15);
gui.add(boton);
gui.setVisible(true);
accion e = new accion();
boton.addActionListener(e);
}catch (Exception e){
System.out.println("Error M001. " + e);
}
and then in the listener of "boton" (it is a button) I have some actions for automation: 然后在“ boton”的监听器中(它是一个按钮),我有一些自动化操作:
public class accion implements ActionListener {
public void actionPerformed (ActionEvent e){
try{
openingTabs();
}catch (Exception f ){
System.out.println("Error M001. " + f);
}
}
}
My problem is that Jframe window is being closed and doesn't let me to perform the action with the button, any idea? 我的问题是Jframe窗口正在关闭,不允许我通过按钮执行操作,有什么主意吗?
If I understand correctly, JUnit ends after last line boton.addActionListener(e);
如果我理解正确,那么JUnit在最后一行
boton.addActionListener(e);
之后结束boton.addActionListener(e);
immediately . 立即 。 This is normal.
这很正常。 A typical swing application starts a dedicated thread for the UI:
一个典型的swing应用程序启动UI的专用线程:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
BTW, one of the traits of good tests is being automated. 顺便说一句,好的测试的特征之一就是自动化。 Just saying.
只是说。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.