猿问

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;

   

        }

    }

}

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


慕斯王
浏览 172回答 1
1回答

子衿沉夜

你快到了。正如您所指出的,问题在于您使用相同的catch语句来处理所有三个文本字段,而无法确定哪一个有问题。一个快速(如果有些不雅)的解决方案是使用并行数组迭代文本框并相应地设置每个值:public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; JTextField[] fields = {theApp.txtR, theApp.txtG, theApp.txtB};&nbsp; &nbsp; int[] colourValues = new int[3];&nbsp; &nbsp; int i = 0;&nbsp; &nbsp; boolean error = false;&nbsp; &nbsp; for (JTextField field : fields) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colourValues[i++] = Integer.parseInt(field.getText());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (NumberFormatException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.setText("");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!error) {&nbsp; &nbsp; &nbsp; &nbsp; theApp.colour = new Color(colourValues[0], colourValues[1], colourValues[2]);&nbsp; &nbsp; &nbsp; &nbsp; theApp.mainText.setForeground(theApp.colour);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Please enter integer values in the fields ","Wrong input", JOptionPane.ERROR_MESSAGE);&nbsp; &nbsp; }}如果您想避免并行数组,另一种选择是改用Color(int rgb)构造函数,并在循环的每次迭代中设置适当的组件。public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; JTextField[] fields = {theApp.txtR, theApp.txtG, theApp.txtB};&nbsp; &nbsp; int rgb = 0, i = 0;&nbsp; &nbsp; boolean error = false;&nbsp; &nbsp; for (JTextField field : fields) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rgb |= (Integer.parseInt(field.getText())) << (16 - i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 8;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (NumberFormatException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.setText("");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!error) {&nbsp; &nbsp; &nbsp; &nbsp; theApp.colour = new Color(rgb);&nbsp; &nbsp; &nbsp; &nbsp; theApp.mainText.setForeground(theApp.colour);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Please enter integer values in the fields ","Wrong input", JOptionPane.ERROR_MESSAGE);&nbsp; &nbsp; }}另请注意,在验证每个值都是整数之后,您还需要验证它们是否介于 0 和 255 之间。
随时随地看视频慕课网APP

相关分类

Java
我要回答