package org.kiley.swing.ui;
import java.awt.BorderLayout;import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class IntegerDisplay extends JPanel implements DocumentListener{
private JTextField input;
private JTextField hundredsPlace;
private JTextField decadePlace;
private JTextField unitPlace;
public IntegerDisplay(){
initComponents();
}
private void initComponents(){
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JPanel inputPanel = new JPanel();
JLabel label = new JLabel("正整数:");
inputPanel.add(label);
input = new JTextField();
input.setPreferredSize(new Dimension(100,22));
input.getDocument().addDocumentListener(this);
inputPanel.add(input);
this.add(inputPanel);
JPanel dispaly1 = new JPanel();
label = new JLabel("百位:");
dispaly1.add(label);
hundredsPlace = new JTextField();
hundredsPlace.setPreferredSize(new Dimension(100,22));
hundredsPlace.setEditable(false);
dispaly1.add(hundredsPlace);
this.add(dispaly1);
JPanel display2 = new JPanel();
label = new JLabel("十位:");
display2.add(label);
decadePlace = new JTextField();
decadePlace.setPreferredSize(new Dimension(100,22));
decadePlace.setEditable(false);
display2.add(decadePlace);
this.add(display2);
JPanel display3 = new JPanel();
label = new JLabel("个位:");
display3.add(label);
unitPlace = new JTextField();
unitPlace.setPreferredSize(new Dimension(100,22));
unitPlace.setEditable(false);
display3.add(unitPlace);
this.add(display3);
}
private void synDisplay(int number){
if(number != -1){
hundredsPlace.setText(getHundredsPlace(number)+"");
decadePlace.setText(getDecadePlace(number)+"");
unitPlace.setText(getUnitPlace(number)+"");
}else {
hundredsPlace.setText("");
decadePlace.setText("");
unitPlace.setText("");
}
}
private int getIntegerFromInputField() {
int a = -1;
String s = input.getText().trim();
if (!s.equals("")) {
try {
a = Integer.parseInt(s);
if(a< 0 ||a >= 1000){
a = -1;
Object[] options = { "确定" };
JOptionPane.showOptionDialog(this, "请输入0-1000的正整数!", "错误",
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
null, options, options[0]);
}
} catch (NumberFormatException e) {
Object[] options = { "确定" };
JOptionPane.showOptionDialog(this, "输入有误,请重新输入!", "错误",
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
null, options, options[0]);
}
}
return a;
}
private int getHundredsPlace(int a){
return a/100;
}
private int getDecadePlace(int a){
int b = getHundredsPlace(a);
return (a-b*100)/10;
}
private int getUnitPlace(int a){
int b = getHundredsPlace(a);
int c = getDecadePlace(a);
return a-b*100-c*10;
}
@Override
public void insertUpdate(DocumentEvent e) {
int number = getIntegerFromInputField();
synDisplay(number);
}
@Override
public void removeUpdate(DocumentEvent e) {
int number = getIntegerFromInputField();
synDisplay(number);
}
@Override
public void changedUpdate(DocumentEvent e) {
}
public static void main(String[] argv){
JFrame frame = new JFrame("Integer Display");
frame.getContentPane().add(new IntegerDisplay(),BorderLayout.CENTER);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800,600));
frame.pack();
}
}
温馨提示:内容为网友见解,仅供参考