本文整理汇总了Java中javax.swing.JFormattedTextField.setHorizontalAlignment方法的典型用法代码示例。如果您正苦于以下问题:Java JFormattedTextField.setHorizontalAlignment方法的具体用法?Java JFormattedTextField.setHorizontalAlignment怎么用?Java JFormattedTextField.setHorizontalAlignment使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.JFormattedTextField
的用法示例。
在下文中一共展示了JFormattedTextField.setHorizontalAlignment方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createMtPanel
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Creates the {@link JPanel} showing the mission time.
*
* @param maxwidth
* the dimension of the longest label
* @return the JPanel showing the mission time
*/
private JPanel createMtPanel(Dimension maxwidth) {
Double mtVal = inverse.evaluate(reliabilityFunction, standardMT);
JLabel pmtLabel = new JLabel("P[MT] =");
pmtLabel.setMinimumSize(maxwidth);
JLabel mtLabel = new JLabel("MT:");
mtLabel.setMinimumSize(maxwidth);
mtProbability = new JFormattedTextField(mtFieldFormat);
mtProbability.addActionListener(MeasurePanel.this);
mtProbability.setPreferredSize(new Dimension(70, 15));
mtProbability.setHorizontalAlignment(SwingConstants.RIGHT);
mtProbability.setText(standardMT.toString());
mt = new JLabel(mtVal.toString());
return createSubPanel("Mission-Time", pmtLabel, mtProbability, mtLabel, mt);
}
示例2: IntegerEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Construct a <code>JSpinner</code> editor that supports displaying and
* editing the value of a <code>SpinnerIntModel</code> with a
* <code>JFormattedTextField</code>. <code>This</code>
* <code>IntegerEditor</code> becomes both a <code>ChangeListener</code>
* on the spinner and a <code>PropertyChangeListener</code> on the new
* <code>JFormattedTextField</code>.
*
* @param spinner the spinner whose model <code>this</code> editor will
* monitor
* @param format the <code>NumberFormat</code> object that's used to
* display and parse the value of the text field.
* @exception IllegalArgumentException if the spinners model is not an
* instance of <code>SpinnerIntModel</code>
*
* @see #getTextField
* @see SpinnerIntModel
* @see java.text.DecimalFormat
*/
private IntegerEditor(JSpinner spinner, NumberFormat format) {
super(spinner);
if (!(spinner.getModel() instanceof SpinnerIntModel)) {
throw new IllegalArgumentException("model not a SpinnerIntModel");
}
format.setGroupingUsed(false);
format.setMaximumFractionDigits(0);
SpinnerIntModel model = (SpinnerIntModel) spinner.getModel();
NumberFormatter formatter = new IntegerEditorFormatter(model, format);
DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
JFormattedTextField ftf = getTextField();
ftf.setEditable(true);
ftf.setFormatterFactory(factory);
ftf.setHorizontalAlignment(JTextField.RIGHT);
try {
String minString = formatter.valueToString(model.getMinimum());
String maxString = formatter.valueToString(model.getMaximum());
// Trying to approximate the width difference between "m" and "0" by multiplying with 0.7
ftf.setColumns((int) Math.round(0.7 * Math.max(maxString.length(), minString.length())));
} catch (ParseException e) {
// Nothing to do, the component width will simply be the default
}
}
示例3: IntegerEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
public IntegerEditor(int min, int max) {
super(new JFormattedTextField());
ftf = (JFormattedTextField) getComponent();
minimum = new Integer(min);
maximum = new Integer(max);
// Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setFormat(integerFormat);
intFormatter.setMinimum(minimum);
intFormatter.setMaximum(maximum);
ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
ftf.setValue(minimum);
ftf.setHorizontalAlignment(JTextField.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
// React when the user presses Enter while the editor is
// active. (Tab is handled as specified by
// JFormattedTextField's focusLostBehavior property.)
ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
ftf.getActionMap().put("check", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (!ftf.isEditValid()) { // The text is invalid.
if (userSaysRevert()) { // reverted
ftf.postActionEvent(); // inform the editor
}
} else
try { // The text is valid,
ftf.commitEdit(); // so use it.
ftf.postActionEvent(); // stop editing
} catch (java.text.ParseException exc) {
}
}
});
}
示例4: DateCellEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Constructor.
*/
public DateCellEditor(DateFormat dateFormat) {
super(new JFormattedTextField());
textField = (JFormattedTextField) getComponent();
this.dateFormat = dateFormat;
DateFormatter dateFormatter = new DateFormatter(dateFormat);
textField.setFormatterFactory(new DefaultFormatterFactory(dateFormatter));
textField.setHorizontalAlignment(JTextField.TRAILING);
textField.setFocusLostBehavior(JFormattedTextField.PERSIST);
// React when the user presses Enter while the editor is
// active. (Tab is handled as specified by
// JFormattedTextField's focusLostBehavior property.)
textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
textField.getActionMap().put("check", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (!textField.isEditValid()) { //The text is invalid.
Toolkit.getDefaultToolkit().beep();
textField.selectAll();
} else {
try { //The text is valid,
textField.commitEdit(); //so use it.
textField.postActionEvent(); //stop editing
} catch (java.text.ParseException ex) {
}
}
}
});
}
示例5: init
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
static void init(int length, boolean hex, JFormattedTextField text) {
ValueFormatter formatter = new ValueFormatter(length, hex);
text.setColumns(length);
text.setFormatterFactory(new DefaultFormatterFactory(formatter));
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.setMinimumSize(text.getPreferredSize());
text.addFocusListener(formatter);
}
示例6: buildNumberTextField
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
private static JTextField buildNumberTextField()
{
// TODO: Verifying user input
NumberFormatter formatter = new NumberFormatter( new DecimalFormat( "0" ) );
formatter.setValueClass( Integer.class );
formatter.setMinimum( Integer.MIN_VALUE );
formatter.setMaximum( Integer.MAX_VALUE );
formatter.setAllowsInvalid( false );
JFormattedTextField result = new JFormattedTextField();
result.setHorizontalAlignment( SwingConstants.RIGHT );
result.setColumns( 10 );
return result;
}
示例7: PHTextFieldNode
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
public PHTextFieldNode( IntegerRange range ) {
super();
_range = new IntegerRange( range );
_listeners = new ArrayList();
EventHandler textFieldEventHandler = new EventHandler();
JLabel phLabel = new JLabel( PHScaleStrings.LABEL_PH );
phLabel.setFont( LABEL_FONT );
_textField = new JFormattedTextField( "XX.XX" );
_textField.setFont( VALUE_FONT );
_textField.setColumns( VALUE_COLUMNS );
_textField.setHorizontalAlignment( JTextField.RIGHT );
_textField.addActionListener( textFieldEventHandler );
_textField.addFocusListener( textFieldEventHandler );
_textField.addKeyListener( textFieldEventHandler );
JPanel valuePanel = new JPanel();
valuePanel.setOpaque( false );
EasyGridBagLayout valuePanelLayout = new EasyGridBagLayout( valuePanel );
valuePanel.setLayout( valuePanelLayout );
valuePanelLayout.addComponent( phLabel, 0, 0 );
valuePanelLayout.addComponent( _textField, 0, 1 );
PSwing valuePanelWrapper = new PSwing( valuePanel );
addChild( valuePanelWrapper );
// initialize
setPH( range.getDefault() );
}
示例8: LaserPowerControl
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Constructor.
*
* @param powerRange
* @param label
* @param units
* @param pattern
* @param columns
* @param wavelength
* @param sliderSize
* @param font
*/
public LaserPowerControl( DoubleRange powerRange, String label, String units, String pattern, int columns,
double wavelength, Dimension sliderSize, Font font ) {
super();
_minPower = powerRange.getMin();
_maxPower = powerRange.getMax();
_listener = new EventHandler();
_listenerList = new EventListenerList();
Color color = VisibleColor.wavelengthToColor( wavelength );
_intensitySlider = new IntensitySlider( color, IntensitySlider.HORIZONTAL, sliderSize );
_intensitySlider.addChangeListener( _listener );
_label = new JLabel( label );
_label.setFont( font );
_formattedTextField = new JFormattedTextField();
_formattedTextField.setFont( font );
_formattedTextField.setColumns( columns );
_formattedTextField.setHorizontalAlignment( JTextField.RIGHT );
_formattedTextField.addActionListener( _listener );
_formattedTextField.addFocusListener( _listener );
_formattedTextField.addKeyListener( _listener );
_formatter = new DecimalFormat( pattern );
_units = new JLabel( units );
_units.setFont( font );
// Opacity
_intensitySlider.setOpaque( false );
_units.setOpaque( false );
// Default state
_power = powerRange.getMin() - 1; // force an update
setPower( powerRange.getDefault() );
}
示例9: SimulationSpeedControlPanel
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
public SimulationSpeedControlPanel( Font titleFont, Font controlFont, OTClock clock ) {
super();
_clock = clock;
_clock.addConstantDtClockListener( new ConstantDtClockAdapter() {
public void dtChanged( ConstantDtClockEvent event ) {
//XXX handle dt changes that are performed by someone else
};
});
// Title
JLabel titleLabel = new JLabel( OTResources.getString( "label.simulationSpeed" ) );
titleLabel.setFont( titleFont );
// Slider
_slider = new SimulationSpeedSlider( clock.getSlowRange(), clock.getFastRange(), clock.getDt() );
_slider.addChangeListener( new ChangeListener() {
public void stateChanged( ChangeEvent e ) {
setSimulationSpeed( _slider.getValue() );
}
});
PhetPCanvas canvas = new PhetPCanvas();
canvas.setBackground( getBackground() );
canvas.setBorder( null );
canvas.getLayer().addChild( _slider );
int margin = 2;
int xOffset = (int) -_slider.getFullBounds().getX() + margin;
int yOffset = (int) -_slider.getFullBounds().getY() + margin;
_slider.setOffset( xOffset, yOffset );
int w = (int) _slider.getFullBounds().getWidth() + ( 2 * margin );
int h = (int) _slider.getFullBounds().getHeight() + ( 2 * margin );
canvas.setPreferredSize( new Dimension( w, h ) );
// Textfield
_textField = new JFormattedTextField( VALUE_FORMAT );
_textField.setFont( controlFont );
_textField.setValue( new Double( _slider.getValue() ) );
_textField.setHorizontalAlignment( JTextField.RIGHT );
_textField.setColumns( VALUE_COLUMNS );
TextFieldListener textFieldListener = new TextFieldListener();
_textField.addActionListener( textFieldListener );
_textField.addFocusListener( textFieldListener );
// Units
JLabel unitsLabel = new JLabel( OTResources.getString( "units.time" ) );
unitsLabel.setFont( controlFont );
EasyGridBagLayout layout = new EasyGridBagLayout( this );
this.setLayout( layout );
layout.setAnchor( GridBagConstraints.WEST );
layout.setFill( GridBagConstraints.HORIZONTAL );
layout.setInsets( OTConstants.SUB_PANEL_INSETS );
int row = 0;
int column = 0;
layout.addComponent( titleLabel, row, column++ );
layout.addComponent( _textField, row, column++ );
layout.addComponent( unitsLabel, row, column++ );
row++;
column = 0;
layout.addComponent( canvas, row, column, 3, 1 );
setSimulationSpeed( clock.getDt() );
}
示例10: ConcentrationControlNode
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
public ConcentrationControlNode( double min, double max ) {
super();
changeListeners = new ArrayList<ChangeListener>();
sliderNode = new ConcentrationSliderNode( min, max );
addChild( sliderNode );
sliderNode.addChangeListener( new SliderListener() );
textField = new JFormattedTextField( TEXTFIELD_FORMAT );
textField.setFont( TEXTFIELD_FONT );
textField.setValue( new Double( sliderNode.getValue() ) );
textField.setHorizontalAlignment( JTextField.RIGHT );
textField.setColumns( TEXTFIELD_COLUMNS );
TextFieldListener textFieldListener = new TextFieldListener();
textField.addActionListener( textFieldListener );
textField.addFocusListener( textFieldListener );
textField.addKeyListener( textFieldListener );
JLabel unitsLabel = new JLabel( AABSStrings.UNITS_MOLES_PER_LITER );
unitsLabel.setFont( UNITS_FONT );
JPanel panel = new JPanel();
panel.setOpaque( false );
panel.add( textField );
panel.add( unitsLabel );
PSwing panelWrapper = new PSwing( panel );
addChild( panelWrapper );
// slider
double xOffset = 0;
double yOffset = 0;
sliderNode.setOffset( xOffset, yOffset );
// text field to the right of slider, vertically centered
xOffset = sliderNode.getFullBoundsReference().getMaxX() + X_SPACING;
yOffset = sliderNode.getFullBoundsReference().getCenterY() - ( panelWrapper.getFullBoundsReference().getHeight() / 2 );
panelWrapper.setOffset( xOffset, yOffset );
}
示例11: IntegerCellEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Constructor.
* @param min the minimum of valid values.
* @param max the maximum of valid values.
*/
public IntegerCellEditor(int min, int max) {
super(new JFormattedTextField());
textField = (JFormattedTextField) getComponent();
minimum = new Integer(min);
maximum = new Integer(max);
// Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setFormat(integerFormat);
intFormatter.setOverwriteMode(false);
intFormatter.setMinimum(minimum);
intFormatter.setMaximum(maximum);
textField.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
textField.setValue(minimum);
textField.setHorizontalAlignment(JTextField.TRAILING);
textField.setFocusLostBehavior(JFormattedTextField.PERSIST);
// React when the user presses Enter while the editor is
// active. (Tab is handled as specified by
// JFormattedTextField's focusLostBehavior property.)
textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
textField.getActionMap().put("check", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (!textField.isEditValid()) { //The text is invalid.
Toolkit.getDefaultToolkit().beep();
textField.selectAll();
} else {
try { //The text is valid,
textField.commitEdit(); //so use it.
textField.postActionEvent(); //stop editing
} catch (java.text.ParseException ex) {
}
}
}
});
}
示例12: DoubleCellEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Constructor.
* @param min the minimum of valid values.
* @param max the maximum of valid values.
*/
public DoubleCellEditor(double min, double max) {
super(new JFormattedTextField());
textField = (JFormattedTextField) getComponent();
minimum = new Double(min);
maximum = new Double(max);
// Set up the editor for the double cells.
doubleFormat = NumberFormat.getNumberInstance();
NumberFormatter doubleFormatter = new NumberFormatter(doubleFormat);
doubleFormatter.setFormat(doubleFormat);
doubleFormatter.setOverwriteMode(false);
doubleFormatter.setMinimum(minimum);
doubleFormatter.setMaximum(maximum);
textField.setFormatterFactory(new DefaultFormatterFactory(doubleFormatter));
textField.setValue(minimum);
textField.setHorizontalAlignment(JTextField.TRAILING);
textField.setFocusLostBehavior(JFormattedTextField.PERSIST);
// React when the user presses Enter while the editor is
// active. (Tab is handled as specified by
// JFormattedTextField's focusLostBehavior property.)
textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
textField.getActionMap().put("check", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (!textField.isEditValid()) { //The text is invalid.
Toolkit.getDefaultToolkit().beep();
textField.selectAll();
} else {
try { //The text is valid,
textField.commitEdit(); //so use it.
textField.postActionEvent(); //stop editing
} catch (java.text.ParseException ex) {
}
}
}
});
}
示例13: IntensityControl
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Constructor.
*
* @param size
* @param font
*/
public IntensityControl( Dimension size, Font font ) {
super();
_listenerList = new EventListenerList();
_eventSource = this;
EventHandler listener = new EventHandler();
_slider = new IntensitySlider( Color.RED, IntensitySlider.HORIZONTAL, size );
_slider.addChangeListener( listener );
_formattedTextField = new JFormattedTextField();
_formattedTextField.setFont( font );
_formattedTextField.setColumns( 3 );
_formattedTextField.setHorizontalAlignment( JTextField.RIGHT );
_formattedTextField.addActionListener( listener );
_formattedTextField.addFocusListener( listener );
_formattedTextField.addKeyListener( listener );
_units = new JLabel( "%" );
_units.setFont( font );
// Opacity
setOpaque( false );
_slider.setOpaque( false );
_units.setOpaque( false );
// Layout
EasyGridBagLayout layout = new EasyGridBagLayout( this );
layout.setInsets( new Insets( 0, 0, 0, 3 ) ); // top, left, bottom, right
this.setLayout( layout );
layout.setAnchor( GridBagConstraints.WEST );
int row = 0;
int col = 0;
layout.addComponent( _slider, row, col++ );
layout.addComponent( _formattedTextField, row, col++ );
layout.addComponent( _units, row, col++ );
// Default state
setValue( 0 );
}
示例14: AbstractValueControl
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Constructor.
*
* @param slider
* @param label label that appears to the left of the value
* @param textFieldPattern pattern used to format the text field (see DecimalFormat)
* @param units units that appear to the right of the value
* @param layoutStrategy
* @throws IllegalArgumentException
*/
public AbstractValueControl( AbstractSlider slider, String label, String textFieldPattern, String units, ILayoutStrategy layoutStrategy ) {
super();
_slider = slider;
_slider.setPaintTicks( true ); // Needed to make the knob reasonably large, see #2979.
_majorTickSpacing = _slider.getModelRange(); // default is major tick marks at min and max
_minorTickSpacing = 0;
_upDownArrowDelta = _slider.getModelRange() / 100;
_slider.setUpDownArrowDelta( _upDownArrowDelta );
_textFieldFormat = new DecimalFormat( textFieldPattern );
_tickFormat = new DecimalFormat( textFieldPattern ); // use same format for ticks and textfield
_majorTicksVisible = true; // major tick labels visible
_minorTicksVisible = false; // minor tick labels are typically not visible
_labelTable = null; // instantiated when addTickLabel is called
_paintTickLabels = true;
_signifyOutOfBounds = true;
_notifyWhileAdjusting = true; // provide change notification while slider is dragging
_isAdjusting = false;
_font = new PhetFont();
_listenerList = new EventListenerList();
// Labels
_valueLabel = new HTMLLabel( label );
_unitsLabel = new HTMLLabel( units );
// Textfield
_textField = new JFormattedTextField( _textFieldFormat );
_textField.setValue( new Double( _value ) );
_textField.setHorizontalAlignment( JTextField.RIGHT );
_textField.setColumns( textFieldPattern.length() );
// Layout the components
layoutStrategy.doLayout( this );
// Listeners
_sliderListener = new SliderListener();
_slider.addChangeListener( _sliderListener );
_slider.addMouseListener( _sliderListener );
_textFieldListener = new TextFieldListener();
_textField.addActionListener( _textFieldListener );
_textField.addFocusListener( _textFieldListener );
_textField.addKeyListener( _textFieldListener );
updateTickLabels();
_value = _slider.getModelMin() - 1; // force setValue to initalize the components
setValue( slider.getModelValue() );
_initialized = true;
}