我有一个我创建的自定义控件。它运行正常但渲染不正确。
可以看出,设计师(netbeans)保持一切整齐。但是在运行程序中有空格,按钮上的文字不显示。
这是代码。
public class AllocateSpinner extends javax.swing.JPanel {
/**
* Creates new form AllocateSpinner
*/
public AllocateSpinner() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Min = new java.awt.Button();
Decrement = new java.awt.Button();
NumberEdit = new javax.swing.JTextField();
Increment = new java.awt.Button();
Max = new java.awt.Button();
setMinimumSize(new java.awt.Dimension(100, 23));
setPreferredSize(new java.awt.Dimension(100, 23));
setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.X_AXIS));
Min.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
Min.setLabel("|<");
Min.setMaximumSize(new java.awt.Dimension(15, 32767));
Min.setMinimumSize(new java.awt.Dimension(15, 23));
Min.setPreferredSize(new java.awt.Dimension(15, 23));
Min.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MinActionPerformed(evt);
}
});
add(Min);
Decrement.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
Decrement.setLabel("<<");
Decrement.setMaximumSize(new java.awt.Dimension(15, 32767));
Decrement.setMinimumSize(new java.awt.Dimension(15, 23));
Decrement.setPreferredSize(new java.awt.Dimension(15, 23));
Decrement.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DecrementActionPerformed(evt);
}
});
add(Decrement);
NumberEdit.setText("0");
NumberEdit.setMinimumSize(new java.awt.Dimension(20, 23));
NumberEdit.setPreferredSize(new java.awt.Dimension(20, 23));
NumberEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NumberEditActionPerformed(evt);
}
});
add(NumberEdit);
Increment.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
Increment.setLabel(">>");
Increment.setMaximumSize(new java.awt.Dimension(15, 32767));
Increment.setMinimumSize(new java.awt.Dimension(15, 23));
Increment.setPreferredSize(new java.awt.Dimension(15, 23));
Increment.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
IncrementActionPerformed(evt);
}
});
add(Increment);
Max.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
Max.setLabel(">|");
Max.setMaximumSize(new java.awt.Dimension(15, 32767));
Max.setMinimumSize(new java.awt.Dimension(15, 23));
Max.setPreferredSize(new java.awt.Dimension(15, 23));
Max.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MaxActionPerformed(evt);
}
});
add(Max);
}// </editor-fold>
private void MinActionPerformed(java.awt.event.ActionEvent evt) {
setValue(_MinValue);
}
private void DecrementActionPerformed(java.awt.event.ActionEvent evt) {
setValue(_Value - _ValueStep);
}
private void IncrementActionPerformed(java.awt.event.ActionEvent evt) {
setValue(_Value + _ValueStep);
}
private void MaxActionPerformed(java.awt.event.ActionEvent evt) {
setValue(_MaxValue);
}
private void NumberEditActionPerformed(java.awt.event.ActionEvent evt) {
try {
setValue(Integer.parseInt(NumberEdit.getText()));
} catch (NumberFormatException e) {
NumberEdit.setText(_Formatter.format(_Value));
}
}
// Variables declaration - do not modify
private java.awt.Button Decrement;
private java.awt.Button Increment;
private java.awt.Button Max;
private java.awt.Button Min;
private javax.swing.JTextField NumberEdit;
// End of variables declaration
private int _Value = 0;
private int _MinValue = 0;
private int _MaxValue = 100;
private int _ValueStep = 1;
private NumberFormat _Formatter = NumberFormat.getIntegerInstance();
/**
* Sets the value. If the value is less than MinValue or greater than MaxValue the value is set to the limit.
* @param value The value to set.
*/
public void setValue(int value) {
if (_Value != value) {
int ov = _Value;
value = Math.max(_MinValue, value);
value = Math.min(_MaxValue, value);
_Value = value;
NumberEdit.setText(_Formatter.format(_Value));
firePropertyChange("Value", ov, _Value);
}
}
/**
* Get the current value.
* @return The current value.
*/
public int getValue() {
return _Value;
}
/**
* Sets the minimum value. If the current value is less than the new minimum value the current value is set to minimum.
* @param value The new minimum value to set.
*/
public void setMinValue(int value) {
if (_MinValue != value) {
int ov = _MinValue;
value = Math.min(_MaxValue, value);
_MinValue = value;
firePropertyChange("MinValue", ov, _MinValue);
if (_Value < _MinValue) {
setValue(_MinValue);
}
}
}
/**
* Get the minimum value that can be set.
* @return The minimum value.
*/
public int getMinValue() {
return _MinValue;
}
/**
* Set the maximum value. If the current value is greater than the new maximum the current value is set to maximum.
* @param value The new Maximum value.
*/
public void setMaxValue(int value) {
if (_MaxValue != value) {
int ov = _MaxValue;
value = Math.max(_MinValue, value);
_MaxValue = value;
firePropertyChange("MaxValue", ov, _MaxValue);
if (_Value > _MaxValue) {
setValue(_MaxValue);
}
}
}
/**
* Get the maximum value that can be set.
* @return The maximum value.
*/
public int getMaxValue() {
return _MaxValue;
}
/**
* Set the amount that the number will be changed on increment or decrement operations.
* @param value The step amount.
*/
public void setValueStep(int value) {
if (_ValueStep != value) {
int ov = _ValueStep;
_ValueStep = value;
firePropertyChange("ValueStep", ov, _ValueStep);
}
}
/**
* Get the amount that the value changes on increment or decrement operations.
* @return The step amount.
*/
public int getValueStep() {
return _ValueStep;
}
/**
* Set the number formatter to use.
* @param formater The formatter to use.
*/
public void setFormatter(NumberFormat formater) {
if (_Formatter != formater) {
NumberFormat of = _Formatter;
_Formatter = formater;
NumberEdit.setText(_Formatter.format(_Value));
this.firePropertyChange("Formatter", of, _Formatter);
}
}
/**
* Get the formatter currently being used.
* @return The NumberFormat currently being used.
*/
public NumberFormat getFormatter() {
return _Formatter;
}
}
答案 0 :(得分:0)
评论&#34;你的按钮是awt按钮,而不是摆动JButton&#34;从Button到JButton的后续调整似乎导致了这个问题的解决。