java编程:输入一个整数,分别显示其百位、十位和个位数字,图形用户如图。要求;整数文本行可编辑

其它文本行仅用于显示不可编辑,整数文本行响应文本编辑事件:当输入数据错误时,处理异常,弹出对话框,提示重新输入信息。

急急急。thank you

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();
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-01-05
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class NewClass3 extends JFrame{

JButton jButton1;
JLabel jLabel1;
JLabel jLabel2;
JLabel jLabel3;
JLabel jLabel4;
JTextField jTextField1;
JTextField jTextField2;
JTextField jTextField3;
JTextField jTextField4;
JButton jButton;
public NewClass3()
{
jButton = new JButton("点我转换");
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jTextField1 = new JTextField(20);
jTextField2 = new JTextField(20);
jTextField3 = new JTextField(20);
jTextField4 = new JTextField(20);
jTextField2.setEditable(false);
jTextField3.setEditable(false);
jTextField4.setEditable(false);
jLabel1.setText("输入数字");
jLabel2.setText("百位数字");
jLabel3.setText("十位数字");
jLabel4.setText("个位数字");
this.add(jLabel1);
this.add(jTextField1);
this.add(jLabel2);
this.add(jTextField2);
this.add(jLabel3);
this.add(jTextField3);
this.add(jLabel4);
this.add(jTextField4);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

try {
int i =Integer.parseInt(jTextField1.getText()) ;
if(i>=1000||i<0){

JOptionPane.showMessageDialog(null,"输入数字必须小于1000大于0");
}else{
int i1 = i/100;
int i2 = i/10%10;
int i3 = i%10;

jTextField2.setText(i1+"");
jTextField3.setText(i2+"");
jTextField4.setText(i3+"");

}

} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"\""+jTextField1.getText()+"\""+"不能转换为数字,请重新输入");
}

}
});
this.add(jButton);
this.setLayout(new FlowLayout());
this.setBounds(200, 100, 300, 170);
this.setVisible(true);
}
public static void main(String[] args)
{
new NewClass3();
}

}本回答被网友采纳
第2个回答  2013-01-05
//输入
string x=12345;
try{
//判断是否为整数
int test=Integer.parseInt(x);
}
catch{Exception e
}
int l=x.length;
//个位数
string a=x.substring(l-2,1);
//十位数
string b=x.substring(i-3,1);
//百位数
string c=x.substring(i-4,1);