我试图创建一个简单的RGM模型,使用三个TextFields(红色,绿色,蓝色)将文本颜色更改为用户设置的颜色。一旦我将数字输入三个字段,然后按“创建”按钮,文本的颜色将更改为整数(R,G,B)设置的颜色。如果按下该按钮时,任何文本字段的内容都不是整数,则应清除无效字段并显示相应的消息。不得清除包含整数的文本字段。
我正在努力做一个部分,如果输入任何不是整数的地方。我尝试过尝试并捕获每个颜色整数(R,G,B)的方法,如下所示:
try{
R = Integer.parseInt(Tred.getText());
}catch (Exception ex){
out.setText("Invalid Input in Red.");
R = 0;
Tred.setText("");
}
虽然这对我没什么用。这是我的完整动作执行,它处理按下按钮时发生的事情:
@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);
}
}
}
}
答案 0 :(得分:0)
很好的问题,下次请给我们一些可执行代码,以帮助我们更好地理解和解决您的问题。从我能看到的,无法运行代码,你只是错过了一件小事。而不是跳到try catch语句,而是在语句之外进行类型更改,并将其作为变量存储。
例如:
String inputconverter=Tred.getText();
无论它是否为整数,这都不会给出错误,因此它可以省略try catch语句,你甚至不需要使用:R = Integer.parseInt(Tred.getText());
,因为你实际上从未将R用于程序的任何方面。而是直接进入:
Integer.parseInt(inputconverter)
希望这有帮助!