我有一个GUI,在这个GUI中,用户在许多字段中输入度量,并根据测量结果计算结果。我正在努力为这些领域实现以下内容-
我在表中做了类似的事情(模型保留了“标准”单元中的值,并且处理了一个自定义呈现器和单元格编辑器,显示用户当前单元中的值,并将值存储在模型中的“标准”单元中)。
我不认为JTextField有可重写的呈现器,文档编辑器看起来有点吓人,而且用户不喜欢JFormattedTextField,所以我想我应该创建一个自定义JTextField,它存储一个“标准”值,并使用我以前在表中使用的inputVerifier。
示例代码在下面(它几乎可以工作)。我使用一个JComboBox作为选项对话框的替补,并且只实现了一个文本字段。
我需要一些专家的建议-
卷值更改(f) //我的焦点侦听器从我的焦点侦听器启动更新模型//,验证:'aa‘//来自我的输入验证器无效编号//来自我的输入验证器
文本框得到一个红色的轮廓,我听到嘟嘟声,但是组合框是活动的。文本字段以空值结束,因为当我更改它的值时,将调用combobox操作监听器。为什么允许我更改combox值?我怎么才能阻止这一切?
我希望对文本字段进行验证,并在用户输入(CR)并停留在字段中或离开字段时更新基础数据的视图。这就是为什么行动和焦点听众。
任何更正或见解都是最受欢迎的。
UnitsTextField.java
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class UnitsTextField extends JTextField
{
Double modelValue = null;
Double viewValue = null;
UnitsTextField( int cols )
{
super( cols );
}
public void updateModel() throws Exception
{
System.out.println( "Updating model" );
modelValue = Conversion.modelValue( this.getText() );
}
public void refreshView()
{
this.setText( Conversion.viewString( modelValue ) );
}
public Double getModelValue()
{
return modelValue;
}
} UnitsLabel.java
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class UnitsLabel extends JLabel
{
public void refreshView()
{
super.setText( Conversion.viewLabel() );
}
}Conversion.java
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Conversion
{
public static enum UNITS {CC, L, GAL};
public static Map<String,UNITS> unitTypes =
new HashMap<String, UNITS>()
{
{
put( "Cubic centimeters", UNITS.CC );
put( "Liters", UNITS.L );
put( "Gallons", UNITS.GAL );
}
};
public static Map<UNITS,Double> unitConversions =
new HashMap<UNITS, Double>()
{
{
put( UNITS.CC, 1.0 );
put( UNITS.L, 1000.0 );
put( UNITS.GAL, 4404.9 );
}
};
private static UNITS unitType = UNITS.CC;
public static void setUnitType( UNITS unit )
{
unitType = unit;
}
public static void setUnitType( String unitString )
{
unitType = unitTypes.get(unitString);
}
public static String[] getUnitNames()
{
return (unitTypes.keySet()).toArray(new String[0]);
}
public static String viewLabel()
{
return unitType.toString();
}
public static Double modelValue( String viewString ) throws Exception
{
Double value = null;
if (viewString != null && viewString.length() > 0)
{
value = Double.parseDouble( viewString );
value = value * unitConversions.get(unitType);
}
return value;
}
public static String viewString( Double modelValue )
{
Double value = null;
if (modelValue != null)
{
value = modelValue / unitConversions.get(unitType);
}
return (value == null) ? "" : value.toString();
}
}DoubleVerifier.java
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.text.NumberFormat;
import java.awt.Toolkit;
public class DoubleVerifier extends InputVerifier implements ActionListener
{
public boolean shouldYieldFocus(JComponent input)
{
JTextField tf = (JTextField) input;
boolean inputOK = verify(input);
if (inputOK)
{
tf.setBorder( new LineBorder( Color.black ) );
return true;
}
else
{
tf.setBorder( new LineBorder( Color.red ) );
Toolkit.getDefaultToolkit().beep();
return false;
}
}
public boolean verify(JComponent input)
{
JTextField tf = (JTextField) input;
String txt = tf.getText();
double n;
System.out.println( "Verifying: '" + txt + "'" );
if (txt.length() != 0)
{
try
{
n = Double.parseDouble(txt);
}
catch (NumberFormatException nf)
{
System.out.println( "Invalid number" );
return false;
}
}
return true;
}
public void actionPerformed(ActionEvent e)
{
System.out.println( "Input verification" );
JTextField source = (JTextField) e.getSource();
shouldYieldFocus(source);
}
}VolumeTextFieldTest.java
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
class VolumeTextFieldTest extends JFrame
{
private JComboBox volumeCombo;
private UnitsLabel volumeLabel;
private UnitsTextField volumeField;
public VolumeTextFieldTest()
{
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
volumeCombo = new JComboBox( Conversion.getUnitNames() );
volumeCombo.addActionListener( new VolumeListener() );
volumeCombo.addFocusListener( new VolumeListener() );
volumeLabel = new UnitsLabel();
volumeLabel.refreshView();
volumeField = new UnitsTextField(8);
DoubleVerifier dVerify = new DoubleVerifier();
volumeField.setInputVerifier( dVerify );
volumeField.addActionListener( dVerify );
volumeField.addActionListener( new VolumeValueListener() );
volumeField.addFocusListener( new VolumeValueListener() );
JPanel myPane = new JPanel();
myPane.add(volumeCombo);
myPane.add(volumeField);
myPane.add(volumeLabel);
getContentPane().add(myPane);
setVisible(true);
}
public class VolumeListener implements ActionListener, FocusListener
{
@Override
public void actionPerformed( ActionEvent ae )
{
System.out.println( "Volume type changed" );
Conversion.setUnitType( (String) volumeCombo.getSelectedItem() );
volumeLabel.refreshView();
volumeField.refreshView();
}
@Override
public void focusGained( FocusEvent fg )
{
}
@Override
public void focusLost( FocusEvent fl )
{
System.out.println( "Volume type changed" );
Conversion.setUnitType( (String) volumeCombo.getSelectedItem() );
volumeLabel.refreshView();
volumeField.refreshView();
}
}
public class VolumeValueListener implements ActionListener, FocusListener
{
@Override
public void actionPerformed( ActionEvent ae )
{
System.out.println( "Volume value changed (a)" );
try
{
volumeField.updateModel();
volumeField.refreshView();
}
catch (Exception e)
{}
}
@Override
public void focusGained( FocusEvent fg )
{
}
@Override
public void focusLost( FocusEvent fl )
{
System.out.println( "Volume value changed (f)" );
try
{
volumeField.updateModel();
volumeField.refreshView();
}
catch (Exception e)
{}
}
}
public static void main(String[] args)
{
try
{
SwingUtilities.invokeLater( new Runnable()
{
public void run ()
{
VolumeTextFieldTest runme = new VolumeTextFieldTest();
}
});
}
catch (Exception e)
{
System.out.println( "GUI did not start" );
}
}
}发布于 2013-02-09 20:48:03
我从额外的研究中了解到我的部分问题。InputVerifier只关注焦点。如果输入无效,那么它将不会转移焦点,但是,它将允许操作事件发生。我所看到的抱怨与那些拥有“退出”按钮的人有关,即使字段中的数据无效,也会执行该按钮的操作。在我的例子中,我有一个组合框,它的操作仍然可以执行,即使InputVerifier正在抱怨无效的数据(文本字段得到红色边框并发出嘟嘟声)。因此,关于这个问题的那个方面,我不认为有一个好的解决办法。一个建议是一个变量,所有操作侦听器在执行操作之前都会检查该变量,该变量将由InputVerifier设置。我已经(理想地)将我的可重用例程放在单独的文件中,所以这个解决方案会有一些问题。
我仍然不确定如何优雅地处理这样一种情况:我有几个不同的泛型操作(验证输入、转换单元、更新视图),在这种情况下,任何给定字段只需要一些操作,并且我希望按顺序分配ActionListeners和FocusListeners。我现在唯一的想法是拥有一个基本侦听器,例如验证输入,然后扩展它并覆盖actionPerformed、focusGained和focusLost方法,尽管看起来我最终会复制每个组合的代码。
https://stackoverflow.com/questions/14756211
复制相似问题