展开全部
这个要求可不简单,麻雀虽小五脏俱全!
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class DialogTest extends JFrame implements Serializable{
private static final long serialVersionUID=3721L;
private JLabel lab1,lab2,lab3;
private JButton b;
private JTextField tf1,tf2;
private JDialog d;
DialogTest(){
32313133353236313431303231363533e59b9ee7ad9431333365646264this.setLayout(new FlowLayout());
this.setBounds(400,300,400,80);
this.setResizable(false);
this.setTitle("除法运算");
init();
this.setVisible(true);
}
private void init() {
lab1=new JLabel("被除数");
lab2=new JLabel("除数");
lab3=new JLabel();
tf1=new JTextField(10);
tf2=new JTextField(10);
b=new JButton("相除");
myEve();
this.add(lab1);
this.add(tf1);
this.add(lab2);
this.add(tf2);
this.add(b);
}
private void myEve() {
this.setDefaultCloseOperation(3);
d=new JDialog(this,"结果提示:",true);
d.setBounds(450,350,200,80);
d.setLayout(new BorderLayout());
d.add(lab3);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
double a=0,b=0,c=0;
try {
a=Integer.parseInt(tf1.getText().replaceAll("[^\\d]+", ""));
b=Integer.parseInt(tf2.getText().replaceAll("[^\\d]+", ""));
if(a!=0&&b!=0) {
c=a/b;
lab3.setText(""+c);
d.setVisible(true);
}
}catch(RuntimeException e) {
lab3.setText("错误警告:除数为零或者输入为空!");
d.setVisible(true);
}
}
});
}
public static void main(String[] args) {
new DialogTest();
}
}