JTextField 输入异常检查和输入问题 return
Problems with JTextField input exception check and input return
我有这个调用 JFrame
并在返回该输入之前请求输入的代码。
代码如下:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
public class testing1 {
public JFrame frame;
public JTextField textField;
String keyword;
ClickHandler cl = new ClickHandler();
JTextArea messageText;
String NumberUnits;
public static void main(String[] args) {
testing1 t = new testing1();
int NUnits = t.AskUnits();
System.out.println("Number of Units: " + NUnits);
}
private testing1() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
JPanel questionPanel = new JPanel();
questionPanel.setBackground(Color.WHITE);
textField = new JTextField();
textField.setColumns(10);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(99)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(10)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
.addGap(11))
.addComponent(questionPanel, GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))
.addGap(106))
.addGroup(groupLayout.createSequentialGroup()
.addGap(132)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 173, GroupLayout.PREFERRED_SIZE)
.addContainerGap(131, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap(38, Short.MAX_VALUE)
.addComponent(questionPanel, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(90))
);
messageText = new JTextArea();
questionPanel.add(messageText);
JButton okButton = new JButton("OK");
okButton.setBackground(Color.WHITE);
okButton.addActionListener(cl);
okButton.setActionCommand("ok");
panel.add(okButton);
JButton cancelButton = new JButton("CANCEL");
cancelButton.setBackground(Color.WHITE);
cancelButton.addActionListener(cl);
cancelButton.setActionCommand("cancel");
panel.add(cancelButton);
frame.getContentPane().setLayout(groupLayout);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public int AskUnits() {
keyword = "AskUnits";
messageText.setText("How many units are in this course?");
NumberUnits = textField.getText();
// String NumberUnits = JOptionPane.showInputDialog(null, "How many units are in this course?", courseName, JOptionPane.INFORMATION_MESSAGE);
try {
int NumberofUnits = Integer.parseInt(NumberUnits);
if (NumberofUnits < 0 || NumberofUnits > 100) {
JOptionPane.showMessageDialog(null, "Input out of bounds",
"ERROR", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
return NumberofUnits;
} catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Input is not a number",
"ERROR", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
return 0;
} // all clear
public class ClickHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String yourChoice = event.getActionCommand();
switch(keyword) {
case "AskUnits":
switch(yourChoice) {
case "ok": System.out.println("OK GOT"); break;
case "cancel": System.exit(0); break;
}
break;
}
}
}
}
我对这段代码的问题:
甚至在输入代码之前,“INPUT IS NOT A NUMBER”异常就出现了。
我想在打印出从输入接收到的 NUnits
值之前接收输入 NUnits
。但是,NUnits
在输入任何内容之前打印 0。
抛出异常是因为您在程序启动后立即调用 AskUnits()
,这会启动输入处理而不给用户输入值的机会。如果您将 AskUnits()
方法调用移动到您的点击处理程序中,用户将有机会输入一个值,您可以在单击 OK
后将其打印到控制台。
如果您使用 AskUnits()
方法来设置屏幕(要问的问题等),您至少应该移动输入处理代码(下面的所有内容,包括 NumberUnits = textField.getText()
调用)到一个单独的方法,然后您可以从 OK
按钮的点击处理程序调用该方法以打印输入的值。
@camickr 对 Java 命名约定给出了一些很好的建议。这是来自 Oracle 的一个很好的资源,它也涵盖了所有其他程序标识符。该页面已存档,但约定未更改。
来自Code Conventions for the Java Programming Language
Naming conventions make programs more understandable by making them easier to read.
大多数 SO 成员欣赏的东西,这反过来又使问题更有可能在此处获得帮助。
我有这个调用 JFrame
并在返回该输入之前请求输入的代码。
代码如下:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
public class testing1 {
public JFrame frame;
public JTextField textField;
String keyword;
ClickHandler cl = new ClickHandler();
JTextArea messageText;
String NumberUnits;
public static void main(String[] args) {
testing1 t = new testing1();
int NUnits = t.AskUnits();
System.out.println("Number of Units: " + NUnits);
}
private testing1() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
JPanel questionPanel = new JPanel();
questionPanel.setBackground(Color.WHITE);
textField = new JTextField();
textField.setColumns(10);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(99)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(10)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
.addGap(11))
.addComponent(questionPanel, GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))
.addGap(106))
.addGroup(groupLayout.createSequentialGroup()
.addGap(132)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 173, GroupLayout.PREFERRED_SIZE)
.addContainerGap(131, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap(38, Short.MAX_VALUE)
.addComponent(questionPanel, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(90))
);
messageText = new JTextArea();
questionPanel.add(messageText);
JButton okButton = new JButton("OK");
okButton.setBackground(Color.WHITE);
okButton.addActionListener(cl);
okButton.setActionCommand("ok");
panel.add(okButton);
JButton cancelButton = new JButton("CANCEL");
cancelButton.setBackground(Color.WHITE);
cancelButton.addActionListener(cl);
cancelButton.setActionCommand("cancel");
panel.add(cancelButton);
frame.getContentPane().setLayout(groupLayout);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public int AskUnits() {
keyword = "AskUnits";
messageText.setText("How many units are in this course?");
NumberUnits = textField.getText();
// String NumberUnits = JOptionPane.showInputDialog(null, "How many units are in this course?", courseName, JOptionPane.INFORMATION_MESSAGE);
try {
int NumberofUnits = Integer.parseInt(NumberUnits);
if (NumberofUnits < 0 || NumberofUnits > 100) {
JOptionPane.showMessageDialog(null, "Input out of bounds",
"ERROR", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
return NumberofUnits;
} catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Input is not a number",
"ERROR", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
return 0;
} // all clear
public class ClickHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String yourChoice = event.getActionCommand();
switch(keyword) {
case "AskUnits":
switch(yourChoice) {
case "ok": System.out.println("OK GOT"); break;
case "cancel": System.exit(0); break;
}
break;
}
}
}
}
我对这段代码的问题:
甚至在输入代码之前,“INPUT IS NOT A NUMBER”异常就出现了。
我想在打印出从输入接收到的
NUnits
值之前接收输入NUnits
。但是,NUnits
在输入任何内容之前打印 0。
抛出异常是因为您在程序启动后立即调用 AskUnits()
,这会启动输入处理而不给用户输入值的机会。如果您将 AskUnits()
方法调用移动到您的点击处理程序中,用户将有机会输入一个值,您可以在单击 OK
后将其打印到控制台。
如果您使用 AskUnits()
方法来设置屏幕(要问的问题等),您至少应该移动输入处理代码(下面的所有内容,包括 NumberUnits = textField.getText()
调用)到一个单独的方法,然后您可以从 OK
按钮的点击处理程序调用该方法以打印输入的值。
@camickr 对 Java 命名约定给出了一些很好的建议。这是来自 Oracle 的一个很好的资源,它也涵盖了所有其他程序标识符。该页面已存档,但约定未更改。
来自Code Conventions for the Java Programming Language
Naming conventions make programs more understandable by making them easier to read.
大多数 SO 成员欣赏的东西,这反过来又使问题更有可能在此处获得帮助。