我目前正在创建一个小型应用程序,该应用程序从某些 JTextFields 中的用户获取 RGB 值,并且需要更改在 JFrame 中间打印的文本的颜色。我需要能够判断用户是否在 JTextField 之一中插入非整数值以抛出错误消息并从 JTextField 中删除错误的输入并在其他 JTextField 中保留正确的值。
public class App extends JFrame {
public JTextField txtR, txtG, txtB;
private JButton reset, set;
private JPanel northPane, centrePane, southPane;
public JLabel mainText;
public Color colour;
public App() {
northPane = new JPanel();
northPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
centrePane = new JPanel();
centrePane.setLayout(new GridBagLayout());
southPane = new JPanel();
txtR = new JTextField(3);
txtG = new JTextField(3);
txtB = new JTextField(3);
set = new JButton("Set");
set.addActionListener(new ButtonHandler(this));
reset = new JButton("Reset");
mainText = new JLabel("CE203 Assignment 1, submitted by: 1704074");
colour = new Color(0,0,255);
mainText.setForeground(colour);
northPane.add(reset);
centrePane.add(mainText);
southPane.add(txtR);
southPane.add(txtG);
southPane.add(txtB);
southPane.add(set);
add(northPane, BorderLayout.NORTH);
add(centrePane, BorderLayout.CENTER);
add(southPane, BorderLayout.SOUTH);
setSize(400,400);
}
public static void main(String[] args) {
App frame = new App();
frame.setVisible(true);
}
}
这是按钮处理程序
class ButtonHandler implements ActionListener {
private App theApp;
private int valueR, valueG, valueB;
ButtonHandler( App app ) {
theApp = app;
}
}
}
这是我的代码,我认为我需要的只是 catch 块中每个 JTextField 的 if ,但我不知道该使用什么。
子衿沉夜
相关分类