0
如果在蓝色文本框(tblue)中输入了任何内容(即使其他内容为空),则会将文本更改为该颜色(如果框为空或包含字母,它应该会产生一个错误信息。这个错误不会发生在其他盒子上,尽管它是相同的代码,它仍然可以正常工作。RGB JTextfields - 在需要3个值时即使其他框为空也会更改颜色
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);
}
}
所以我需要改变什么?我仍然对你的意思感到困惑 – sb33
我刚刚试过这个,现在它只适用于第一个盒子?显然int a现在也是多余的 – sb33
是的,我不能让这个工作队友,只有第一个盒子清除,如果它现在无效? – sb33