Java 如果内容不是整数[JApplet],则清除文本字段

Java 如果内容不是整数[JApplet],则清除文本字段,java,integer,japplet,Java,Integer,Japplet,我试图创建一个简单的RGM模型,将文本颜色更改为用户使用三个文本字段(红色、绿色、蓝色)设置的颜色。一旦我在三个字段中输入数字,然后按下“创建”按钮,文本的颜色将更改为整数(R、G、B)设置的颜色。如果按下按钮时,任何文本字段的内容都不是整数,则应清除无效字段,并显示相应的消息。但是,包含整数的文本字段决不能清除 我正在努力完成输入非整数的where if部分。我在每个颜色整数(R、G、B)上尝试了try-and-catch方法,如下所示: 虽然这对我来说没什么用。下面是我执行的完整操作,它处理

我试图创建一个简单的RGM模型,将文本颜色更改为用户使用三个文本字段(红色、绿色、蓝色)设置的颜色。一旦我在三个字段中输入数字,然后按下“创建”按钮,文本的颜色将更改为整数(R、G、B)设置的颜色。如果按下按钮时,任何文本字段的内容都不是整数,则应清除无效字段,并显示相应的消息。但是,包含整数的文本字段决不能清除

我正在努力完成输入非整数的where if部分。我在每个颜色整数(R、G、B)上尝试了try-and-catch方法,如下所示:

虽然这对我来说没什么用。下面是我执行的完整操作,它处理按下按钮时发生的情况:

            @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);

        }
    }
}
我可能需要为每个颜色整数使用if语句吗

[更新]根据要求,以下是我的代码的可执行示例:

    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);

        }
    }
}


}

好问题,下次请给我们一些可执行代码,以帮助我们更好地理解和解决您的问题。据我所知,在无法运行代码的情况下,您只是错过了一点。不要直接跳入try-catch语句,而是在语句之外进行类型更改,并首先将其存储为变量

例如:

String inputconverter=Tred.getText();
无论它是否为整数,这都不会给出错误,因此可以将其从try-catch语句中删除,并且您甚至不需要使用:
R=integer.parseInt(Tred.getText())因为你从来没有在程序的任何方面使用过R。而是直接进入:

Integer.parseInt(inputconverter)

希望这有帮助

“虽然这对我来说没什么用”-你的断章取义的代码片段对我们来说也一样。考虑提供一个说明你的问题的方法。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应,这就是为什么在我被告知尝试只显示相关代码片段之前,我没有发布可执行代码的原因。我会更新。看看我的例子中提供的链接,它会让你了解我们正在寻找什么。你发布的例子对我来说似乎很好。我可以在一个或多个字段中输入无效文本,并且根据您的规范,无效字段已正确更新。可变名称不应以大写字符开头。有些变量名是正确的,有些是不正确的。如果您希望人们阅读您的代码,请保持一致并遵循Java约定。
Integer.parseInt(inputconverter)