java尝试/捕获JOptionPane的问题,需要捕获不正确的整数

时间:2017-05-13 03:40:31

标签: java try-catch joptionpane

有点坚持这个问题,但我怎么能让我尝试捕获只允许字符串进入输入框。 (相关的代码是在底部,对于凌乱的格式仍然很新的图形编程抱歉)

我认为下面的代码可行但是我猜它没有,我现在不太确定它是否会在sting输入中捕获所有的int,因为int正在输入为字符串。我试图使用布尔值,但这并没有真正起作用。此外,如果您可以查看电子邮件try / catch以及弹出错误框,如果有@符号,因为它有点类似。  感谢

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.InputMismatchException;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;


public class MainPanel extends JPanel {

    Player myPlayer;
    Player myOtherPlayer;
    private int WIDTH = 1000;
    private int HEIGHT = 1000;
    private int WALLWIDTH = 100;
    private int WALLHEIGHT = 100;
    private ArrayList<Wall> walls = new ArrayList<Wall>();
    String Name;
    String Email;
    int favNum;

    Timer myTimer = new Timer(500, new timerListener());    
    JLabel myTimeLabel;
    int time =1;

    public MainPanel()
    {
        setPreferredSize(new Dimension(WIDTH,HEIGHT));
        JLabel myLabel= new JLabel ("Game ends once 30 seconds is receahed:");
        myLabel.setFont(new Font("Serif", Font.BOLD,32));
        myLabel.setForeground(Color.WHITE);
        myTimeLabel= new JLabel (Integer.toString(time));
        myTimeLabel.setFont(new Font("Serif", Font.BOLD,32));
        myTimer.start();

        add(myLabel);
        add(myTimeLabel);
        myPlayer = new Player(0,100, "toad.png", KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,this, 50, 38);
        myOtherPlayer = new Player(200,200, "toad.png", KeyEvent.VK_W, KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_D,this, 50, 38);
        createWalls();
 }

    public ArrayList<Wall> getWalls() {
        return walls;
    }

    public void createWalls()
    {
        int j = 0;
        for(int i = 0; i < HEIGHT/WALLHEIGHT; i++)
        {
            for(int k = 0; k < WIDTH/WALLWIDTH; k++)
            {
                if(i == 0 || i == (HEIGHT/WALLHEIGHT-1))
                {
                    walls.add(new Wall(k*WALLWIDTH,j,"road.png", 100, 100));
                }

            }
            j+=WALLHEIGHT;
        }
    }


    private class timerListener implements ActionListener
    {


        public void actionPerformed(ActionEvent e) {
            time++;
            myTimeLabel.setText(Integer.toString(time));
            myTimeLabel.setForeground(Color.WHITE);
            if(time == 31)
            {
                myTimer.stop();
            }
            repaint();
        }

    }


    public void paintComponent(Graphics page)
    {
        super.paintComponent(page);

        page.drawImage(myPlayer.getImageIcon().getImage(), myPlayer.getX(), myPlayer.getY(), null);
        page.drawImage(myOtherPlayer.getImageIcon().getImage(), myOtherPlayer.getX(), myOtherPlayer.getY(), null);


        for(int i = 0; i < walls.size(); i++)
        {
            page.drawImage(walls.get(i).getImageIcon().getImage(), walls.get(i).getX(), walls.get(i).getY(), null);


        }

        page.setFont(new Font("Arial", Font.PLAIN,32)); 
        page.drawString("Player 1 Score: " + myPlayer.getScore(), 100, 800);
        page.drawString("Player 2 Score: " + myOtherPlayer.getScore(), 100, 850);
        myPlayer.checkOffScreen();

        if(time == 5)
        {
            page.drawString("GAME OVER", WIDTH/2-100, HEIGHT/2);

            try{
                Name = JOptionPane.showInputDialog("What is your name?");
                }catch (NumberFormatException  e) {
                    if (!Name.matches("^[a-z][a-z ]*[a-z]?$")){
                    JOptionPane.showInputDialog("You entered a number, this only accepts alphabetical letters. Please enter your name");}
                }

            try{
                favNum= Integer.parseInt(JOptionPane.showInputDialog("What is your favorite number?")); 
                }catch (NumberFormatException  e) {

                JOptionPane.showInputDialog("You entered a text, this only accepts numbers . Please enter your  favorite number");}

            try{
                Email =JOptionPane.showInputDialog("Please enter your email");
                }catch (InputMismatchException  e) {
                JOptionPane.showInputDialog("You enter your email incorrectly. Please include an @ sign with it");}
                }

            }

    }

1 个答案:

答案 0 :(得分:0)

您应该使用regular expressions来执行此操作,而不是捕获异常。尝试捕获是很好的,但是你不应该将用户输入视为异常,因为你应该检查它,例如:

String regex = "\\d+";
String input = JOptionPane.showInputDialog("What is your favorite number?");
if (!input.matches(regex)) {
   JOptionPane.showInputDialog("You entered a text, this only accepts numbers . Please enter your  favorite number");}
}