下面的示例展示如何创建和使用JFormattedTextField
在基于Swing的应用程序中的文本字段上指定日期格式。
使用以下API -
JFormattedTextField
- 创建格式化文本字段。DateFormat
- 为JFormattedTextField提供日期格式。SimpleDateFormat(patten)
- 获取日期模式的所需日期格式。
示例
package com.yiibai.swingdemo;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SwingTester implements PropertyChangeListener {
private static JFormattedTextField principleTextField;
private static JFormattedTextField rateTextField;
private static JFormattedTextField yearsTextField;
private static JFormattedTextField amountTextField;
public static void main(String[] args) {
SwingTester tester = new SwingTester();
createWindow(tester);
}
private static void createWindow(SwingTester tester) {
JFrame frame = new JFrame("Swing JFormattedTextField示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createUI(frame, tester);
frame.setSize(560, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void createUI(final JFrame frame, SwingTester tester) {
JPanel panel = new JPanel();
LayoutManager layout = new GridLayout(6,2);
panel.setLayout(layout);
panel.setSize(300, 200);
panel.setBorder(BorderFactory.createTitledBorder("格式化示例:"));
NumberFormat principleFormat = NumberFormat.getNumberInstance();
principleTextField = new JFormattedTextField(principleFormat);
principleTextField.setName("Principle");
principleTextField.setColumns(10);
JLabel principleLabel = new JLabel("原金:");
principleLabel.setLabelFor(principleTextField);
principleTextField.setValue(new Double(199999));
principleTextField.addPropertyChangeListener("value", tester);
NumberFormat rateFormat = NumberFormat.getPercentInstance();
rateFormat.setMinimumFractionDigits(2);
rateTextField = new JFormattedTextField(rateFormat);
rateTextField.setName("Rate");
rateTextField.setColumns(10);
JLabel rateLabel = new JLabel("利率:");
rateLabel.setLabelFor(rateTextField);
rateTextField.setValue(new Double(0.1));
rateTextField.addPropertyChangeListener("value", tester);
yearsTextField = new JFormattedTextField();
yearsTextField.setName("Years");
yearsTextField.setColumns(10);
JLabel yearLabel = new JLabel("年数:");
yearLabel.setLabelFor(yearsTextField);
yearsTextField.setValue(new Double(1));
yearsTextField.addPropertyChangeListener("value", tester);
NumberFormat amountFormat = NumberFormat.getCurrencyInstance();
amountTextField = new JFormattedTextField(amountFormat);
amountTextField.setName("总量");
amountTextField.setColumns(10);
amountTextField.setEditable(false);
JLabel amountLabel = new JLabel("总金量:");
amountLabel.setLabelFor(amountTextField);
amountTextField.setValue(new Double(1999999 + 1999999 * 0.10));
DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
JFormattedTextField today = new JFormattedTextField(dateFormat);
today.setName("今天");
today.setColumns(10);
today.setEditable(false);
JLabel todayLabel = new JLabel("日期:");
todayLabel.setLabelFor(today);
today.setValue(new Date());
panel.add(principleLabel);
panel.add(principleTextField);
panel.add(rateLabel);
panel.add(rateTextField);
panel.add(yearLabel);
panel.add(yearsTextField);
panel.add(amountLabel);
panel.add(amountTextField);
panel.add(todayLabel);
panel.add(today);
JPanel mainPanel = new JPanel();
mainPanel.add(panel);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
double amount, rate, years, principle;
principle = ((Number)principleTextField.getValue()).doubleValue();
rate = ((Number)rateTextField.getValue()).doubleValue() * 100;
years = ((Number)yearsTextField.getValue()).doubleValue();
amount = principle + principle * rate * years / 100;
amountTextField.setValue(new Double(amount));
}
}
执行上面示例代码,得到以下结果: