What code would I use to ask a user to enter their grade into a pop-up window?
When a JButton is pressed, I want a little box to pop-up and prompt the user to enter their grade. Furthermore, would it be possible to get th开发者_开发问答e value of the entered double value?
Thanks for all your time. I appreciate it!
Use JOptionPane.showInputDialog().
You can find a nice tutorial at: http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
You want a JOptionPane. Use something like the following code snippet inside the JButton's ActionListener:
JTextArea textArea = new JTextArea();
textArea.setEditable(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.requestFocus();
textArea.requestFocusInWindow();
scrollPane.setPreferredSize(new Dimension(800, 600));
JOptionPane.showMessageDialog(
(ControlWindow) App.controller.control, scrollPane,
"Paste Info", JOptionPane.PLAIN_MESSAGE);
String info = textArea.getText();
You could parse/validate the double value from the output string. You could also use different swing components - this example is a scrollable text area.
The simplest approach would perhaps be to use JoptionPane.showInputDialog(...)
.
However, be aware that it will crash if someone tries to enter anything other than a double.
@Override
public void actionPerformed(ActionEvent actionEvent) {
double someNumber = Double.parseDouble(
JOptionPane.showInputDialog(this, "Type in grade:"));
}
精彩评论