我嘗試創建一個簡單的RGM模型,將文本顏色更改爲用戶使用三個TextField(紅,綠,藍)設置的顏色。一旦我將數字輸入到三個字段中,然後按下「創建」按鈕,文本的顏色將更改爲由整數(R,G,B)設置的顏色。如果按下按鈕時,任何文本字段的內容都不是整數,則應清除無效字段並顯示相應的消息。包含整數的文本字段不能被清除。如果內容不是整數,清除TextFields [JApplet]
我正在努力做的地方,如果任何東西不是一個整數輸入。我曾嘗試每種顏色的整數(R,G,B)try和catch方法,像這樣:
try{
R = Integer.parseInt(Tred.getText());
}catch (Exception ex){
out.setText("Invalid Input in Red.");
R = 0;
Tred.setText("");
}
雖然這並沒有爲我。這裏是我的全部的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);
}
}
}
我會也許需要一個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);
}
}
}
}
*「雖然這對我沒有任何幫助」* - 您的上下文代碼片段也不適合我們。考慮提供一個[可運行的示例](https://stackoverflow.com/help/mcve),它可以證明你的問題。這不是代碼轉儲,而是您正在做的事情的一個例子,它突出了您遇到的問題。這將導致更少的混淆和更好的迴應 – MadProgrammer
我之前拒絕發佈可執行代碼的原因是我被告知嘗試只顯示相關代碼片段。我會更新。 – Volken
看看我的示例中提供的鏈接,它應該爲您提供我們正在尋找的內容的一些想法 – MadProgrammer