是否可以像这样在 JTextField 中分发字符串:(蓝色文本字段中的“布鲁诺”)。
JTextField 自动划分为许多等间距的位置作为最大列值。
我编写这段代码来解释为什么字体跟踪对我不起作用:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.font.TextAttribute;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class SpacedFieldTest extends JFrame{
Map<TextAttribute, Object> fontAttributes = new HashMap<TextAttribute, Object>();
int maxColumn;
String text;
public SpacedFieldTest(String text, int maxColumn) {
this.text = text;
this.maxColumn = maxColumn;
addContent();
pack();
}
private void addContent() {
setLayout(new FlowLayout());
final SpacedField field = new SpacedField();
configureField(field, "Arial", 20f);
getContentPane().add(field);
JSpinner spinner = createSpinner(field);
getContentPane().add(spinner);
JLabel tracking = new JLabel("Tracking");
getContentPane().add(tracking);
}
private JSpinner createSpinner(final SpacedField field) {
float min = 0.0f;
float value = 0.1f;
float max = 3.0f;
float stepSize = 0.1f;
SpinnerNumberModel model = new SpinnerNumberModel(value, min, max, stepSize);
JSpinner spinner = new JSpinner(model){
@Override
public Object getValue() {
fontAttributes.put(TextAttribute.TRACKING, super.getValue());
field.setFont(field.getFont().deriveFont(fontAttributes));
return super.getValue();
}
};
spinner.setPreferredSize(new Dimension(50,25));
JSpinner.NumberEditor editor = (JSpinner.NumberEditor)spinner.getEditor();
DecimalFormat format = editor.getFormat();
format.setMinimumFractionDigits(1);
return spinner;
}
private void configureField(JTextField field, String family, float fontSize) {
fontAttributes.put(TextAttribute.SIZE, fontSize);
fontAttributes.put(TextAttribute.FAMILY, family);
Font font = Font.getFont(fontAttributes);
field.setFont(font);
field.setText(text);
}
class SpacedField extends JTextField{
final int WIDTH = 300;
final int HEIGHT = 25;
int gridWidth;
public SpacedField() {
gridWidth = WIDTH/maxColumn;
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setDocument(new SpacedFieldDocument());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
paintGrid(g);
}
private void paintGrid(Graphics g) {
for(int i=1; i<=maxColumn; i++){
g.drawLine(i*gridWidth, HEIGHT/2, i*gridWidth, HEIGHT);
}
}
}
class SpacedFieldDocument extends PlainDocument{
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if(getLength()>maxColumn)
return;
super.insertString(offs, str, a);
}
}
public static void main(String args[]){
final SpacedFieldTest sf = new SpacedFieldTest("abcde", 5);
sf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
sf.setVisible(true);
}
});
}
}