我刚写的,可以用来检测输入的是数字而且是正数,否则弹出提示,输入框获得焦点 String inValue = jTextField.getText();
try{
int in = Integer.parseInt(inValue);
if(in<0){
throw new NumberFormatException();
}
}catch(NumberFormatException ne){
JOptionPane.showMessageDialog(this,"输入数据类型有错误,请输入整型正数");
jTextField.requestFocus();
}
try{
int in = Integer.parseInt(inValue);
if(in<0){
throw new NumberFormatException();
}
}catch(NumberFormatException ne){
JOptionPane.showMessageDialog(this,"输入数据类型有错误,请输入整型正数");
jTextField.requestFocus();
}
刚刚我试过了,确实不行
sigh
不过jTextField.grabFocus();可以
你运行我的代码就知道了,点击按钮执行……
package alpha;
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
public class Test extends JFrame {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
public Test() {
try {
jbInit(); }
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(500,400);
jPanel1.setLayout(xYLayout1);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jPanel1.add(jButton1, new XYConstraints(168, 190, -1, -1));
jPanel1.add(jTextField1, new XYConstraints(129, 106, 135, -1));
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
} void jButton1_actionPerformed(ActionEvent e) {
Object[] options = { "OK", "CANCEL" };
int alpha = JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
System.out.println(alpha);
if (alpha==0) {
jTextField1.grabFocus();
} }
///
public static void main(String[] args) {
try {
Test st = new Test();
st.show();
}
catch(Exception e) {
}
}
//*/
}
不过jTextField.grabFocus();可以
你运行我的代码就知道了,点击按钮执行……
package alpha;
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
public class Test extends JFrame {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
public Test() {
try {
jbInit(); }
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(500,400);
jPanel1.setLayout(xYLayout1);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jPanel1.add(jButton1, new XYConstraints(168, 190, -1, -1));
jPanel1.add(jTextField1, new XYConstraints(129, 106, 135, -1));
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
} void jButton1_actionPerformed(ActionEvent e) {
Object[] options = { "OK", "CANCEL" };
int alpha = JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
System.out.println(alpha);
if (alpha==0) {
jTextField1.grabFocus();
} }
///
public static void main(String[] args) {
try {
Test st = new Test();
st.show();
}
catch(Exception e) {
}
}
//*/
}