第1个回答 2011-12-10
这个是别人写的,真是借花献佛了
----------------------------------------------------------
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class MyFrame2 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton jb1, jb2;
private JTextField jtf1, jtf2, jtf3, jtf4;
private JLabel jl1, jl2, jl3, jl4;
private Integer num;
private int num1;
private int num2;
private int num3;
public MyFrame2() {
this.getContentPane().setLayout(null);
this.setSize(400, 400);
jb1 = new JButton("计算");
jb2 = new JButton("结束");
jtf1 = new JTextField();
jtf2 = new JTextField();
jtf3 = new JTextField();
jtf4 = new JTextField();
jl1 = new JLabel("请输入整数:");
jl2 = new JLabel("百位:");
jl3 = new JLabel("十位:");
jl4 = new JLabel("个位:");
jb1.setBounds(new Rectangle(50, 230, 100, 30));
jb2.setBounds(new Rectangle(220, 230, 100, 30));
jb1.addActionListener(this);
jb2.addActionListener(this);
jl1.setBounds(new Rectangle(10, 50, 80, 25));
jtf1.setBounds(new Rectangle(90, 50, 170, 25));
jl2.setBounds(new Rectangle(10, 140, 40, 25));
jtf2.setBounds(new Rectangle(50, 140, 70, 25));
jl3.setBounds(new Rectangle(130, 140, 40, 25));
jtf3.setBounds(new Rectangle(170, 140, 70, 25));
jl4.setBounds(new Rectangle(250, 140, 40, 25));
jtf4.setBounds(new Rectangle(290, 140, 70, 25));
jtf2.setEditable(false);
jtf3.setEditable(false);
jtf4.setEditable(false);
jtf1.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {
String s = jtf1.getText();
try {
num = Integer.parseInt(s);
} catch (NumberFormatException e1) {
num = null;
JOptionPane.showMessageDialog(null, "请正确输入整型数字!");
System.out.println("...");
jtf1.requestFocus();
}
}
public void focusGained(FocusEvent e) {
}
});
this.getContentPane().add(jb1);
this.getContentPane().add(jb2);
this.getContentPane().add(jl1);
this.getContentPane().add(jl2);
this.getContentPane().add(jl3);
this.getContentPane().add(jl4);
this.getContentPane().add(jtf1);
this.getContentPane().add(jtf2);
this.getContentPane().add(jtf3);
this.getContentPane().add(jtf4);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jb2) {
System.exit(0);
} else if (e.getSource() == jb1) {
solveNum(num);
jtf2.setText(num1 + "");
jtf3.setText(num2 + "");
jtf4.setText(num3 + "");
}
}
private void solveNum(Integer n) {
if (n == null)
return;
num1 = n / 100;
num2 = n / 10 % 10;
num3 = n % 10;
}
public static void main(String[] args) {
new MyFrame2();
}
}本回答被提问者采纳