首页 > 解决方案 > Java:带有错误变量检测的错误处理

问题描述

我目前正在创建一个小型应用程序,该应用程序在一些 JTextFields 中从用户那里获取 RGB 值,并且需要更改在 JFrame 中间打印的文本的颜色。我需要能够判断用户是否在其中一个 JTextField 中插入非整数值以引发错误消息并从 JTextField 中删除错误的输入,并在其他 JTextField 中保留正确的值。

public class App extends JFrame {
    public JTextField txtR, txtG, txtB;
    private JButton reset, set;
    private JPanel northPane, centrePane, southPane;
    public JLabel mainText;
    public Color colour;

    public App() {
        northPane = new JPanel();
        northPane.setLayout(new FlowLayout(FlowLayout.RIGHT));

        centrePane = new JPanel();
        centrePane.setLayout(new GridBagLayout());

        southPane = new JPanel();

        txtR = new JTextField(3);
        txtG = new JTextField(3);
        txtB = new JTextField(3);

        set = new JButton("Set");
        set.addActionListener(new ButtonHandler(this));
        reset = new JButton("Reset");

        mainText = new JLabel("CE203 Assignment 1, submitted by: 1704074");
        colour = new Color(0,0,255);
        mainText.setForeground(colour);

        northPane.add(reset);

        centrePane.add(mainText);

        southPane.add(txtR);
        southPane.add(txtG);
        southPane.add(txtB);
        southPane.add(set);

        add(northPane, BorderLayout.NORTH);
        add(centrePane, BorderLayout.CENTER);
        add(southPane, BorderLayout.SOUTH);

        setSize(400,400);
    }

    public static void main(String[] args) {
        App frame = new App();
        frame.setVisible(true);
    }
}

这是按钮处理程序

class ButtonHandler implements ActionListener {
    private App theApp;
    private int valueR, valueG, valueB;

    ButtonHandler( App app ) {
        theApp = app;
    }
    public void actionPerformed(ActionEvent e) {
        try {
            valueR = Integer.parseInt(theApp.txtR.getText());
            valueG = Integer.parseInt(theApp.txtG.getText());
            valueB = Integer.parseInt(theApp.txtB.getText());
            theApp.colour = new Color(valueR, valueG, valueB);
            theApp.mainText.setForeground(theApp.colour);
        }
        catch (NumberFormatException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Please enter integer values in the fields ","Wrong input", JOptionPane.ERROR_MESSAGE);
        }
    }
}

这是我的代码,我认为我需要的只是 catch 块中每个 JTextField 的 if,但我不知道该使用什么。

标签: java

解决方案


你快到了。正如您所指出的,问题在于您使用相同的catch语句来处理所有三个文本字段,而无法确定哪一个有问题。

一个快速(如果有点不优雅)的解决方案是使用并行数组来迭代文本框并相应地设置每个值:

public void actionPerformed(ActionEvent e) {
    JTextField[] fields = {theApp.txtR, theApp.txtG, theApp.txtB};
    int[] colourValues = new int[3];
    int i = 0;
    boolean error = false;
    for (JTextField field : fields) {
        try {
            colourValues[i++] = Integer.parseInt(field.getText());
        }
        catch (NumberFormatException ex) {
            error = true;
            field.setText("");
        }
    }
    if (!error) {
        theApp.colour = new Color(colourValues[0], colourValues[1], colourValues[2]);
        theApp.mainText.setForeground(theApp.colour);
    } else {
        JOptionPane.showMessageDialog(null, "Please enter integer values in the fields ","Wrong input", JOptionPane.ERROR_MESSAGE);
    }
}

如果您想避免并行数组,另一种选择是使用Color(int rgb)构造函数,并在循环的每次迭代中设置适当的组件。

public void actionPerformed(ActionEvent e) {
    JTextField[] fields = {theApp.txtR, theApp.txtG, theApp.txtB};
    int rgb = 0, i = 0;
    boolean error = false;
    for (JTextField field : fields) {
        try {
            rgb |= (Integer.parseInt(field.getText())) << (16 - i);
            i += 8;
        }
        catch (NumberFormatException ex) {
            error = true;
            field.setText("");
        }
    }
    if (!error) {
        theApp.colour = new Color(rgb);
        theApp.mainText.setForeground(theApp.colour);
    } else {
        JOptionPane.showMessageDialog(null, "Please enter integer values in the fields ","Wrong input", JOptionPane.ERROR_MESSAGE);
    }
}

另请注意,在验证每个值都是整数之后,您还需要验证它们是否介于 0 和 255 之间。


推荐阅读