简体   繁体   English

如果内容不是整数,则清除 TextFields [JApplet]

[英]Clearing TextFields if content is not an integer [JApplet]

Im trying to create a simple RGM model that changes the text colour to the colour set by the user using three TextFields (Red,Green,Blue).我试图创建一个简单的 RGM 模型,该模型使用三个文本字段(红色、绿色、蓝色)将文本颜色更改为用户设置的颜色。 Once i input numbers into the three fields then press a "Create" button, the colour of the text will change to the colour set by the integers (R,G,B).一旦我在三个字段中输入数字,然后按“创建”按钮,文本的颜色将更改为整数(R、G、B)设置的颜色。 If when the button is pressed, the content of any text fields is not an integer, the invalid field(s) should be cleared and an appropriate message should appear.如果按下按钮时,任何文本字段的内容都不是整数,则应清除无效字段并应显示适当的消息。 Text fields containing integers must never be cleared though.但是,永远不能清除包含整数的文本字段。

I am struggling to do the part where the where if anything that isnt an integer is entered.我正在努力完成输入不是整数的地方的部分。 I have tried try and catch methods on each colour integer (R,G,B) like so:我已经尝试在每个颜色整数(R、G、B)上尝试捕获方法,如下所示:

            try{
                R = Integer.parseInt(Tred.getText());
            }catch (Exception ex){
                out.setText("Invalid Input in Red.");
                R = 0;
                Tred.setText("");
            }

Though this does nothing for me.虽然这对我没有任何作用。 Here is my full actionPerformed which handles what happens when the buttons are pressed:这是我的完整 actionPerformed 处理按下按钮时发生的情况:

            @Override
    public void actionPerformed(ActionEvent e) {

        try{
            try{
                R = Integer.parseInt(Tred.getText());
            }catch (Exception ex){
                out.setText("Invalid Input in Red.");
                R = 0;
                Tred.setText("");
            }
            try{
                G = Integer.parseInt(Tgreen.getText());
            }catch (Exception ex){
                out.setText("Invalid Input in Green.");
                G = 0;
                Tgreen.setText("");
            }
            try{
                B = Integer.parseInt(Tblue.getText());
            }catch (Exception ex){
                out.setText("Invalid Input in Blue.");
                B = 0;
                Tblue.setText("");
            }

            inputColor = new Color(R,G,B);

            switch (theOp){
                case 'C':
                    out.setText("My Name");
                    out.setForeground(inputColor);
                    break;

                case 'R':
                    out.setText("Welcome");
                    out.setForeground(Color.green);
                    Tred.setText("");
                    Tgreen.setText("");
                    Tblue.setText("");
                    break;
            }


        }catch(Exception e1){

            out.setText("Invalid Input captured");
            out.setForeground(Color.black);

        }
    }
}

Would i maybe need to use an if statement for each colour integer ?我可能需要为每个颜色整数使用 if 语句吗?

[UPDATE] As requested, here is an executable example of my code: [更新] 根据要求,这是我的代码的可执行示例:

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

    public class Ex1 extends JApplet {
        //Here i create the 3 primary colour integers
        int R = 0,G = 0,B = 0;
        //inputColor will be the color the user created
        Color inputColor = new Color(R,G,B);
        TextField Tred,Tgreen,Tblue;
        JButton buttCreate,buttReset;
        JLabel sR,sG,sB,out;
        JPanel topPanel,centrePanel;


public void init()

{
    this.setSize(700, 100);

    topPanel = new JPanel();
    centrePanel = new JPanel();

    buttCreate = new JButton("Create!");
    buttCreate.addActionListener(new ButtonHandler(this,'C'));
    buttReset = new JButton("Reset");
    buttReset.addActionListener(new ButtonHandler(this,'R'));


    Tred = new TextField("",3);
    Tgreen = new TextField("",3);
    Tblue = new TextField("",3);

    sR = new JLabel("R:");
    sG = new JLabel("G:");
    sB = new JLabel("B:");

    out = new JLabel
            ("Welcome to CE203 Assignment 1,submitted by: Lewis Horsley 1405160");
    out.setForeground(Color.green);



    topPanel.add(sR);
    topPanel.add(Tred);
    topPanel.add(sG);
    topPanel.add(Tgreen);
    topPanel.add(sB);
    topPanel.add(Tblue);
    topPanel.add(buttCreate);
    topPanel.add(buttReset);

    centrePanel.add(out);

    this.add(centrePanel, BorderLayout.CENTER);
    this.add(topPanel, BorderLayout.NORTH);


}

class ButtonHandler implements ActionListener {

    Ex1 theApplet;
    char theOp;

    ButtonHandler(Ex1 app, char op){
        this.theApplet = app;
        this.theOp = op;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        try{
            try{
                R = Integer.parseInt(Tred.getText());
            }catch (Exception ex){
                out.setText("Invalid Input in Red.");
                R = 0;
                Tred.setText("");
            }
            try{
                G = Integer.parseInt(Tgreen.getText());
            }catch (Exception ex){
                out.setText("Invalid Input in Green.");
                G = 0;
                Tgreen.setText("");
            }
            try{
                B = Integer.parseInt(Tblue.getText());
            }catch (Exception ex){
                out.setText("Invalid Input in Blue.");
                B = 0;
                Tblue.setText("");
            }

            inputColor = new Color(R,G,B);

            switch (theOp){
                case 'C':
                    out.setText("My Name");
                    out.setForeground(inputColor);
                    break;

                case 'R':
                    out.setText("Welcome");
                    out.setForeground(Color.green);
                    Tred.setText("");
                    Tgreen.setText("");
                    Tblue.setText("");
                    break;
            }


        }catch(Exception e1){

            out.setText("Invalid Input captured");
            out.setForeground(Color.black);

        }
    }
}


}

Great question, next time please give us some executable code to help us better understand and solve your problem.好问题,下次请给我们一些可执行代码,以帮助我们更好地理解和解决您的问题。 From what I can see without being able to run the code you just missed a little thing.从我所看到的,无法运行代码,你只是错过了一点点。 Instead of jumping right into the try catch statement, do the type changes outside of the statement and store that as a variable first.不要直接跳到 try catch 语句中,而是先在语句之外进行类型更改,然后将其存储为变量。

For example:例如:

String inputconverter=Tred.getText();

Regardless if it is an integer or not, this will not give an error so it can be left out of a try catch statement, and you don't even need to use the: R = Integer.parseInt(Tred.getText());无论它是否是整数,这都不会出错,因此可以将其排除在 try catch 语句之外,您甚至不需要使用: R = Integer.parseInt(Tred.getText()); as you never actually use R for any aspect of the program.因为您实际上从未在程序的任何方面使用 R。 Instead go right into:而是直接进入:

Integer.parseInt(inputconverter)

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM