为什么jframe在终止后会停止整个执行

问题描述

实际上,我在这里调用jframe类时遇到问题。在主要方法完成执行之前执行Jframe整理执行。

这是主类的代码,我在其中调用jframe类 '''

public class Connection2{

//  public static final String HOST = "localhost";
//  public static final String PORT = "5555";

public static String HOST;
public static String PORT;
public static long START;
public static long END;

public static void main(String[] args) throws InterruptedException,ClassNotFoundException,sqlException,IOException {


    Frame1 frame = new Frame1();
    
    Scanner sc = new Scanner(system.in);
    System.out.println("JMX CONNECTION:");
    System.out.println("HOST AND PORT");
    frame.main(args); >here the GUI starts and after clicling Jbutton it stops



    Thread.sleep(10000);
    HOST = frame.textField.getText();
    PORT= frame.textField_1.getText();
    System.out.println(HOST);
    System.out.println("TEST DURATION IN SECONDS");
    System.out.println("DURATON(sec): ");
    int Duration = sc.nextInt();

    START = System.currentTimeMillis();
    END = START + Duration*1000; // 60 seconds * 1000 ms/sec  60*1000= 1 minute

    Connection2 object = new Connection2();

    Csvwriter csv = new Csvwriter();
    MenuDrivenSystem menu = new MenuDrivenSystem();
    Thread.sleep(3000);
    while(System.currentTimeMillis() < END){



        try
        {

            object.waitMethod();
            Thread.sleep(2000);



        }
        catch(Exception E)
        {
            E.printstacktrace();
            //JOptionPane.showMessageDialog(null,"CSV NOT SAVED");
        }

    }
    csv.main(args);
    System.out.println("Database and csv push done............");
    menu.main(args);
}

'''

这是我的jFrame类 '''

public class Frame1 {

public JFrame frame;
public JTextField textField;
public JTextField textField_1;
public JTextField textField_2;
public JLabel lblNewLabel;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokelater(new Runnable() {
        public void run() {
            try {
                Frame1 window = new Frame1();
                window.frame.setVisible(true);

            } catch (Exception e) {
                e.printstacktrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Frame1() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
public void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground( new Color(240,240,240));
    frame.setBounds(100,100,450,300);
    frame.setDefaultCloSEOperation(JFrame.disPOSE_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("Connect");
    btnNewButton.setForeground(Color.BLACK);
    btnNewButton.setBackground(Color.GRAY);
    btnNewButton.setFont(new Font("Tahoma",Font.BOLD,16));
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //messape
            String userText;
            String pwdText;
            userText = textField.getText();
            pwdText = textField_1.getText();
            System.out.println(userText);
                   
            if(e.getSource()==btnNewButton) {
                   System.exit(0);
                   // This exit Your GUI 
                }
            

        }
    });
    
    
    btnNewButton.setBounds(156,142,115,29);
    frame.getContentPane().add(btnNewButton);

    textField = new JTextField();
    textField.setBounds(144,16,146,26);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    lblNewLabel = new JLabel("URL OR HOST:");
    lblNewLabel.setBounds(28,18,114,23);
    frame.getContentPane().add(lblNewLabel);

    textField_1 = new JTextField();
    textField_1.setBounds(144,58,26);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);

    textField_2 = new JTextField();
    textField_2.setBounds(144,26);
    frame.getContentPane().add(textField_2);
    textField_2.setColumns(10);

    JLabel lblNewLabel_1 = new JLabel("PORT:");
    lblNewLabel_1.setBounds(38,61,75,20);
    frame.getContentPane().add(lblNewLabel_1);

    JLabel lblNewLabel_2 = new JLabel("DURATION(S):");
    lblNewLabel_2.setBounds(27,103,20);
    frame.getContentPane().add(lblNewLabel_2);
}

}

''' 我希望在打开GUI后,它应该接受用户的输入,然后关闭窗口并移至下一行代码

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)