如果我有以下 Java 代码:
import javax.swing.JFrame;
class GuessMyNumber extends JFrame {
public static void main(String[] args) {
InputBox box = new InputBox("Guess a number between 1 and 100:");
box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
box.setSize(360, 360);
box.setVisible(true);
}
}
输入框类:
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class InputBox extends JFrame {
private JLabel text1;
private JTextField textField1;
public InputBox(String prompt) {
super("Guess My Number");
setLayout(new FlowLayout());
text1 = new JLabel(prompt);
add(text1);
textField1 = new JTextField(20);
add(textField1);
Handler handler = new Handler();
textField1.addActionListener(handler);
}
private class Handler implements ActionListener {
String in = "";
public void actionPerformed(ActionEvent event) {
if (event.getSource() == textField1) {
in = event.getActionCommand();
}
}
}
}
如何in
从主类访问?我是Java GUI 的绝对初学者,所以请不要太苛刻!
谢谢