我想从 GUI 窗口获取输入并存储。

huangapple go评论66阅读模式
英文:

I wanted to take input from GUI window and store

问题

以下是您的GUI代码的翻译部分:

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) {
                userText = textField.getText();
                pwdText = textField_1.getText();
                System.out.println(userText);

                if (e.getSource() == btnNewButton) {
                    frame.dispose();
                }
            }
        });
    }
}

以下是您的主程序代码的翻译部分:

public class Connection2 {
    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("DURATION(sec): ");
        int Duration = sc.nextInt();
    }
}

以下是您的输出部分的翻译:

JMX CONNECTION

HOST AND PORT
localhost
null
null

此输出是在将线程暂停后获得的。

英文:

here is my GUI

  1. during start window will open and take iput HOST, PORT and duration.
  2. clicking Connect button it will close the window.
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 
}
}
});

And, here is my main program where im calling the Frame1 in Connection2.

here while retreiving values from textfields, its showing null

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();

OUTPUT

JMX CONNECTION
HOST AND PORT
localhost
null
null

This output im getting, after giving pause time to thread.

答案1

得分: 1

在这段代码中,它不会等到用户按下按钮,因此它会返回一个空值。

因此,修复这个问题的一个可能方法是在连接类中添加一个方法,该方法在按钮被按下时被调用。

英文:

In this code it dosn't wait until the user press the button so I gives back a null value.

So a possible way to repair this is adding a method in the connection class that is called when the button is pressed

huangapple
  • 本文由 发表于 2020年9月24日 14:24:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64040633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定