微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我想从GUI窗口中输入信息并存储

如何解决我想从GUI窗口中输入信息并存储

这是我的GUI

  1. 在开始窗口期间,将打开并输入主机,端口和持续时间。
  2. 单击“连接”按钮将关闭窗口。
public class Frame1 {
    
    public String userText;
    public String pwdText;
    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.EXIT_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
                
                userText = textField.getText();
                pwdText = textField_1.getText();
                System.out.println(userText);
                         
                if(e.getSource()==btnNewButton) {
                       frame.dispose();
                       
                       // This exit Your GUI 
                    }
                

            }
        });
        

这是我的主程序,在其中我调用 Connection2 中的 Frame1

在这里从文本字段检索值时,显示为空

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);
        
        
        HOST =frame.userText;
        PORT =frame.pwdText;
        Thread.sleep(10000);
        System.out.println(HOST);
        System.out.println(PORT);
        
        
        Thread.sleep(20000);
        System.out.println(HOST);
        System.out.println("TEST DURATION IN SECONDS");
        System.out.println("DURATON(sec): ");
        int Duration = sc.nextInt();

输出

JMX CONNECTION

HOST AND PORT
localhost
null
null

在给线程暂停时间之后,此输出即时消息。

解决方法

在此代码中,它不会等到用户按下按钮后才返回空值。

因此,解决此问题的一种可能方法是在连接类中添加一个在按下按钮时调用的方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。