RGB JTextfields - 即使在需要 3 个值时其他框为空也会改变颜色

RGB JTextfields - changes colour even if other boxes are empty when 3 values are required

如果在蓝色文本字段 (tblue) 中输入任何内容,即使其他文本字段为空,它也会将文本更改为该颜色,如果文本框为空或包含字母,它应该会产生错误消息。其他盒子不会发生此错误,它会按预期工作,即使它是相同的代码。

package FirstCE203Project;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class CE203_2016_Ex1 extends JFrame {

    JTextField tred, tgreen, tblue;
    JLabel message;                              //to reference later on
    JButton goButton, reset;


    public CE203_2016_Ex1() {

        JPanel panel1 = new JPanel();       //creates panels for the boxes that will hold the rgb values
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();

        tred = new JTextField("Red", 10);
        tgreen = new JTextField("Green", 10);             //creates boxes for rgb values
        tblue = new JTextField("Blue", 10);

        panel2.setLayout(new GridBagLayout());           //sets text to the centre of the panel

        panel1.add(tred);
        panel1.add(tgreen);                            //adding panels to frame
        panel1.add(tblue);


        add(panel1, BorderLayout.SOUTH); //adding panels to frame              //location of panels on frame
        add(panel2, BorderLayout.CENTER);
        add(panel3, BorderLayout.NORTH);


        message = new JLabel("hello");  //text
        message.setForeground(new Color(255, 0, 0));     //original text set to red

        JButton goButton = new JButton("Change"); //adds button to change colour
        JButton reset = new JButton("Reset");


        panel1.add(goButton);
        panel2.add(message);
        panel3.add(reset);                  //adding buttons to panels

        goButton.addActionListener(new ButtonHandler(this));
        reset.addActionListener(new ButtonHandler1(this));                    //creating action listener

    }


    class ButtonHandler implements ActionListener {

        private CE203_2016_Ex1 theApp;

        public ButtonHandler(CE203_2016_Ex1 theApp) {
            this.theApp = theApp;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int a=0, b=0, c=0;

            try {
                a = Integer.parseInt(theApp.tred.getText());

                if (a < 0) {
                    a = 200;                                           //if statements for values above and below the targets set
                    tred.setText("200");
                }

                if (a > 255) {
                    a = 255;
                    tred.setText("255");
                }
                message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
                message.setForeground(new Color(a, b, c)); //changes colour to desired input
            }

            catch(NumberFormatException ex) {

                message.setText("invalid input! please enter numbers only");  //text
                message.setForeground(new Color(0, 0, 0));     //error message set to black
                tred.setText("");
            }

            try {
                b = Integer.parseInt(theApp.tgreen.getText());

                if (b < 0) {
                    b = 200;
                    tgreen.setText("200");
                }

                if (b > 255) {
                    b = 255;
                    tgreen.setText("255");
                }
                message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
                message.setForeground(new Color(a, b, c)); //changes colour to desired input
            }
            catch(NumberFormatException ey) {

                message.setText("invalid input! please enter numbers only");  //text
                message.setForeground(new Color(0, 0, 0));     //original text set to red
                tgreen.setText("");

            }

            try {
                c = Integer.parseInt(theApp.tblue.getText());

                if (c < 0) {
                    c = 200;
                    tblue.setText("200");
                }

                if (c > 255) {
                    c = 255;
                    tblue.setText("255");
                }
                message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
                message.setForeground(new Color(a, b, c)); //changes colour to desired input
            }
            catch(NumberFormatException ez) {

                message.setText("invalid input! please enter numbers only");  //text
                message.setForeground(new Color(0, 0, 0));     //original text set to red
                tblue.setText("");
            }

        }
    }


    class ButtonHandler1 implements ActionListener {

        private CE203_2016_Ex1 theApp;

        public ButtonHandler1(CE203_2016_Ex1 theApp) {
            this.theApp = theApp;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
            message.setForeground(new Color(255, 0, 0));                 //resets message back to red
            tred.setText("Red");
            tgreen.setText("Green");
            tblue.setText("Blue");
        }
    }

    public static void main(String[] args) {
        JFrame app = new CE203_2016_Ex1();
        app.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        app.setSize(700, 700);
        app.setVisible(true);
    }
}

它实际上做了你想要的,但文本会立即被覆盖(这就是你看不到变化的原因)。

假设您输入了无效的绿色值 (b)。

消息将设置为 "invalid input ..."。

try {
    b = Integer.parseInt(theApp.tgreen.getText());
    ...
} catch (NumberFormatException ey) {
    message.setText("invalid input! please enter numbers only");
    ...
}

但是你有一个有效的蓝色输入(c):

try {
    c = Integer.parseInt(theApp.tblue.getText());
    ...
    message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
    ...
}

这将用 "CE203 Assignment ..." 覆盖 "invalid input ..." 值。


可能的解决方案:

    @Override
    public void actionPerformed(ActionEvent e) {
        int a = 0, b = 0, c = 0;
        boolean isValidA = true, isValidB = true, isValidC = true;

        try {
            a = Integer.parseInt(theApp.tred.getText());
        } catch (NumberFormatException ex) {
            isValidA = false;
        }

        try {
            b = Integer.parseInt(theApp.tgreen.getText());
        } catch (NumberFormatException ex) {
            isValidB = false;
        }

        try {
            c = Integer.parseInt(theApp.tblue.getText());
        } catch (NumberFormatException ex) {
            isValidC = false;
        }

        if (!isValidA) {
            tred.setText("");
        } else {
            if (a < 0) {
                a = 200;
                tred.setText("200");
            } else if (a > 255) {
                a = 255;
                tred.setText("255");
            }
        }

        if (!isValidB) {
            tgreen.setText("");
        } else {
            if (b < 0) {
                b = 200;
                tgreen.setText("200");
            } else if (b > 255) {
                b = 255;
                tgreen.setText("255");
            }
        }

        if (!isValidC) {
            tblue.setText("");
        } else {
            if (c < 0) {
                c = 200;
                tblue.setText("200");
            } else if (c > 255) {
                c = 255;
                tblue.setText("255");
            }
        }
        if (isValidA && isValidB && isValidC) {
            message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
            message.setForeground(new Color(a, b, c));
        } else {
            message.setText("invalid input! please enter numbers only");
            message.setForeground(new Color(0, 0, 0));
        }

    }