gpt4 book ai didi

Java:向 ChangeEvent 添加文本字段

转载 作者:行者123 更新时间:2023-12-01 12:14:26 26 4
gpt4 key购买 nike

我有一个家庭作业,我必须更改颜色选择器(它有 3 个 slider ,RGB 并显示颜色),以便有文本字段,并且当在字段中输入值时,它也会更改 slider 。我明白了基本的想法,但是我很难找到更改文本字段的解决方案,它们只能与 Action 事件一起使用。这是我的代码

package colorchooser;
import java.util.Vector;
import java.awt.Color;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
*
* @author -
*/
public class ColorChooser extends javax.swing.JPanel implements ChangeListener, ActionListener{

private Vector listeners;

/**
* Creates new form ColorChooser
*/
public ColorChooser() {
initComponents();
listeners = new Vector();
sldRed.addChangeListener(this);
sldGreen.addChangeListener(this);
sldBlue.addChangeListener(this);
txtRed.addActionListener(this);

// Variables declaration - do not modify
private javax.swing.JButton btnSetColor;
private javax.swing.JLabel lblBlue;
private javax.swing.JLabel lblGreen;
private javax.swing.JLabel lblRed;
private javax.swing.JSlider sldBlue;
private javax.swing.JSlider sldGreen;
private javax.swing.JSlider sldRed;
private javax.swing.JTextField txtBlue;
private javax.swing.JTextField txtGreen;
private javax.swing.JTextField txtRed;
// End of variables declaration

@Override
public void stateChanged(ChangeEvent ce) {
int r = sldRed.getValue();
int g = sldGreen.getValue();
int b = sldBlue.getValue();
Color color = new Color(r,g,b);
fireColorEvent(new ColorEvent(this,color));
txtRed.setText(Integer.toString(r));
txtGreen.setText(Integer.toString(g));
txtBlue.setText(Integer.toString(b));
}

public void addColorListener(ColorListener colorListener){
listeners.addElement(colorListener);
}

public void actionPerformed(ActionEvent ae) {

if(ae.getSource()==btnSetColor){
try
{
if(Integer.parseInt(txtRed.getText()) <= 255){
sldRed.setValue(Integer.parseInt(txtRed.getText()));
}
else
{
txtRed.setText("0");
JOptionPane.showMessageDialog(this,"Error: Please enter a value equal to or less than 255");
}


if(Integer.parseInt(txtBlue.getText()) <= 255){
sldBlue.setValue(Integer.parseInt(txtBlue.getText()));
}
else
{
txtBlue.setText("0");
JOptionPane.showMessageDialog(this,"Error: Please enter a value equal to or less than 255");
}

if(Integer.parseInt(txtGreen.getText()) <= 255){
sldGreen.setValue(Integer.parseInt(txtGreen.getText()));
}
else
{
txtGreen.setText("0");
JOptionPane.showMessageDialog(this,"Error: Please enter a value equal to or less than 255");
}
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this,"Error: Please Enter Numbers");
txtRed.setText("0");
txtGreen.setText("0");
txtBlue.setText("0");
sldRed.setValue(Integer.parseInt(txtRed.getText()));
sldGreen.setValue(Integer.parseInt(txtGreen.getText()));
sldBlue.setValue(Integer.parseInt(txtBlue.getText()));
}
}
}

您可以看到我正在向 slider 添加更改监听器,但我必须向 txtRed 添加操作监听器,因为它不接受 addChangeListener。关于解决方法有什么想法吗?

谢谢!

最佳答案

创建一个可以处理已更改字段的方法...

protected void fieldWasChanged(JTextField field) {
try {
int value = Integer.parseInt(field.getText());
JSlider slider = null;
if (field == txtBlue) {
slider = sldBlue;
} else if (field == txtRed) {
slider = sldRed;
} else if (field == txtGreen) {
slider = sldGreen;
}
if (slider != null) {
slider.setValue(value);
}
} catch (NumberFormatException exp) {
exp.printStackTrace();
// Display error message
}
}

接下来,创建一个实现 DocumentListener 接口(interface)的内部类...

public class DocumentHandler implements DocumentListener {

private JTextField field;

public DocumentHandler(JTextField field) {
this.field = field;
}

protected void documentUpdated() {
fieldWasChanged(field);
}

@Override
public void insertUpdate(DocumentEvent e) {
documentUpdated();
}

@Override
public void removeUpdate(DocumentEvent e) {
documentUpdated();
}

@Override
public void changedUpdate(DocumentEvent e) {
documentUpdated();
}

}

这将监视 Document 底层字段的更改并调用您的 fieldWasChanged 方法...

接下来,使用此处理程序注册每个字段...

protected void registerDocumentHandler(JTextField field) {
DocumentHandler handler = new DocumentHandler(field);
field.getDocument().addDocumentListener(handler);
}

//...

registerDocumentHandler(txtBlue);
registerDocumentHandler(txtRed);
registerDocumentHandler(txtGreen);

现在,如果您实际尝试过此操作,您会发现在输入内容的那一刻,您的 UI 将变得无响应,并且您的程序最终会因 StackOverflowException 崩溃,发生这种情况是因为 DocumentHandler 正在更新 JSlider,后者正在更新字段,后者正在通知 DocumentHandler 等等......

一个简单的解决方案是放入一个标志,而 true 会阻止 DocumentHandler 通知 fieldWasChanged 方法...

public class DocumentHandler implements DocumentListener {

private JTextField field;
private boolean isUpdating;

public DocumentHandler(JTextField field) {
this.field = field;
}

protected void documentUpdated() {
if (!isUpdating) {
try {
fieldWasChanged(field);
} finally {
isUpdating = false;
}
}
}

//...

现在您可以输入并获取实时反馈,您可以将文本粘贴到字段中,它们将更新 slider ...

现在,话虽如此,我鼓励您尝试使用 JFormattedTextFieldJSlider 而不是 JTextField,因为它们可以帮助验证数据他们自己。请参阅How to Use Formatted Text FieldsHow to Use Spinners了解更多详情。

如果您不想使用这些,但仍希望限制用户只能输入数字,请查看 Implementing a Document FilterDocumentFilter Examples

关于Java:向 ChangeEvent 添加文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27098444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com