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

时间:2016-11-08 22:27:53

标签: java jtextfield rgb

如果在蓝色文本字段(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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

假设您为绿色(200 OK)输入了无效值。

该消息将设置为“无效输入...”。

b

但是你有一个蓝色的有效输入(try { b = Integer.parseInt(theApp.tgreen.getText()); ... } catch (NumberFormatException ey) { message.setText("invalid input! please enter numbers only"); ... } ):

c

这将使用“CE203 Assignment ...”覆盖“无效输入...”值。

可能的解决方案:

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