JAVA:如何在按钮被按下时设置要返回的字符串值?

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

JAVA: How to set a string value to be returned, when a button is pressed?

问题

以下是翻译好的部分:

当我从其他 .java 文件中调用 Main.main(null)
在用户能够按下 "Student" 按钮之前main() 返回 `return_str = "Not assigned!";`,
然后执行 `return_str = "student sign-in";`。

public class Main extends JFrame {

	private static JPanel contentPane;
	static Main frame;
	static String return_str;
	static JButton btnStudent = new JButton("Student");

	/**
	 * Launch the application.
	 */

	public static String main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new Main();
					frame.setVisible(true);

					btnStudent.addActionListener(new Action_student());
					contentPane.add(btnStudent);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		return_str = "Not assigned!";
		System.out.println(return_str);
		return return_str;
	}

	/**
	 * Create the frame.
	 */
	public Main() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		btnStudent.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		btnStudent.setBounds(171, 148, 85, 21);
		contentPane.add(btnStudent);

	}

	static class Action_student implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			return_str = "student sign-in";
		}

	}

}
英文:

When I call the Main.main(null) from other .java file,
return_str = "Not assigned!"; is returned by the main(), before the user could press the "Student" button and
return_str = "student sign-in"; is executed.


public class Main extends JFrame {
private static JPanel contentPane;
static Main frame;
static String return_str;
static JButton btnStudent = new JButton("Student");
/**
* Launch the application.
*/
public static String main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new Main();
frame.setVisible(true);
btnStudent.addActionListener(new Action_student());
contentPane.add(btnStudent);
} catch (Exception e) {
e.printStackTrace();
}
}
});
return_str = "Not assigned!";
System.out.println(return_str);
return return_str;
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnStudent.setFont(new Font("Times New Roman", Font.PLAIN, 16));
btnStudent.setBounds(171, 148, 85, 21);
contentPane.add(btnStudent);
}
static class Action_student implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
return_str = "student sign-in";
}
}
}

答案1

得分: 1

根据方法的名称,帧创建是“稍后调用”的,意味着在另一个线程中进行。因此,您必须让主程序等待按钮被按下。

在Main类中添加一个新变量:

static boolean studentEntered = false;

在动作监听器中将studentEntered标志设置为true:

static class Action_student implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        return_str = "学生签到";
        Main.studentEntered = true;
    }
}

在main方法中,将最后几行修改为:

return_str = "未分配!";
while (!studentEntered) {
    // 只要标志未切换,就循环执行
}
System.out.println(return_str);
try {
    return return_str;
} finally {
    // 通过应用程序关闭帧,否则您的应用程序将不会停止
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
英文:

As the method already indicates, the frame creation is 'invoked later' meaning in another thread.
So you have to let your main routine wait for the button to be pressed.

Add a new variable to the Main class:

static boolean studentEntered = false;

In the action listener sets the studentEntered flag to true:

static class Action_student implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
return_str = "student sign-in";
Main.studentEntered = true;
}
}

In the main method you change your last lines to:

return_str = "Not assigned!";
while (!studentEntered) {
// loop as long as the flag is not switched
}
System.out.println(return_str);
try {
return return_str;
} finally {
// close the frame by the application otherwise your application will not stop
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}

答案2

得分: 0

问题是监听器是如何工作的!

当你创建按钮时,你正在为其分配一个监听器,这意味着仅当按钮被按下时,你的字符串才会改变!

输出是正确的,因为在创建监听器之后,它会执行其余的代码,在你的情况下:

return_str = "未分配!";
System.out.println(return_str);

你需要改变返回字符串的方式。

英文:

The problem is how a Listener works!

When you create your button, you are assigning a listener to that, it means that ONLY when the button is pressed your string is going to change!

The output is correct, because after creating the listener it executes the rest of code, in your case

return_str = "Not assigned!";
System.out.println(return_str);

You have to change the way you return the string.

huangapple
  • 本文由 发表于 2020年10月20日 21:20:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64446062.html
匿名

发表评论

匿名网友

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

确定