java程序设计代码:显示一个三位整数的各位数字 输入一个整数,分别显示其百位、十位和个位数字。 20
图形用户界面如图所示。要求:整数文本框可编辑,其他文本框仅用于显示不可编辑;整数文本框响应文本编辑事件;当输入数据错误时,处理异常,弹出对话框,提示重新输入信息。我的QQ...
图形用户界面如图所示。要求:整数文本框可编辑,其他文本框仅用于显示不可编辑;整数文本框响应文本编辑事件;当输入数据错误时,处理异常,弹出对话框,提示重新输入信息。
我的QQ1546705880 展开
我的QQ1546705880 展开
2个回答
展开全部
输入结束后,回车就出结果了
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test {
private JFrame f = new JFrame("三位数拆分");
private JLabel numLabel = new JLabel("三位数");
private JLabel hundredLabel = new JLabel("百位");
private JLabel tenthLabel = new JLabel("十位");
private JLabel indLabel = new JLabel("个位");
private JTextField hund = new JTextField();
private JTextField ten = new JTextField();
private JTextField indiv = new JTextField();
private JTextField number = new JTextField();
public Test(){
f.setLayout(new GridLayout(4, 2));
f.add(numLabel);
f.add(number);
f.add(hundredLabel);
f.add(hund);
f.add(tenthLabel);
f.add(ten);
f.add(indLabel);
f.add(indiv);
hund.setEditable(false);
ten.setEditable(false);
indiv.setEditable(false);
number.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(!number.getText().matches("\\d{3}")){
JOptionPane.showMessageDialog(null, "Error! It must be 3 digits only!");
number.setText("");
}else{
hund.setText(number.getText().substring(0, 1));
ten.setText(number.getText().substring(1, 2));
indiv.setText(number.getText().substring(2, 3));
}
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
}
public static void main(String args[]) {
new Test();
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test {
private JFrame f = new JFrame("三位数拆分");
private JLabel numLabel = new JLabel("三位数");
private JLabel hundredLabel = new JLabel("百位");
private JLabel tenthLabel = new JLabel("十位");
private JLabel indLabel = new JLabel("个位");
private JTextField hund = new JTextField();
private JTextField ten = new JTextField();
private JTextField indiv = new JTextField();
private JTextField number = new JTextField();
public Test(){
f.setLayout(new GridLayout(4, 2));
f.add(numLabel);
f.add(number);
f.add(hundredLabel);
f.add(hund);
f.add(tenthLabel);
f.add(ten);
f.add(indLabel);
f.add(indiv);
hund.setEditable(false);
ten.setEditable(false);
indiv.setEditable(false);
number.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(!number.getText().matches("\\d{3}")){
JOptionPane.showMessageDialog(null, "Error! It must be 3 digits only!");
number.setText("");
}else{
hund.setText(number.getText().substring(0, 1));
ten.setText(number.getText().substring(1, 2));
indiv.setText(number.getText().substring(2, 3));
}
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
}
public static void main(String args[]) {
new Test();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询