黑马程序员技术交流社区
标题:
关于java程序修改问题
[打印本页]
作者:
hanrongle
时间:
2013-8-17 07:19
标题:
关于java程序修改问题
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Jsq extends Applet implements ActionListener{
Label output1;
Label output2;
Label output3;
Label output4;
Label output5;
TextField field1=new TextField(8);
TextField field2=new TextField(8);
TextField field3=new TextField(8);
Button button1=new Button("相加");
Button button2=new Button("相减");
Button button3=new Button("相乘");
Button button4=new Button("相除");
Button button5=new Button("清空");
public void init() {
output1=new Label("有理数的第一个操作数:以a/b的形式输入");
output2=new Label(" ");
output3=new Label("有理数的第二个操作数:以a/b的形式输入");
output4=new Label("结果:以a/b的形式输出 ");
output5=new Label(" ");
add(output1);
add(field1);
add(output2);
add(output3);
add(field2);
add(output4);
add(field3);
add(button1);
button1.addActionListener(this);
add(button2);
button2.addActionListener(this);
add(button3);
button3.addActionListener(this);
add(button4);
button4.addActionListener(this);
add(button5);
button5.addActionListener(this);
add(output5);
}
public void actionPerformed(ActionEvent e){
int a,b,c,d;
String[] s1=field1.getText().split("/");
String[] s2=field2.getText().split("/");
a=Integer.parseInt(s1[0]);
b=Integer.parseInt(s1[1]);
c=Integer.parseInt(s2[0]);
d=Integer.parseInt(s2[1]);
if(b==0||d==0){
output5.setText("分母不能为零!!!");
return;
}
else if(e.getSource()==button1){
output2.setText("+");
field3.setText(ToString(a*d+b*c,b*d));
}
else if(e.getSource()==button2){
}
else if(e.getSource()==button3){
output2.setText("*");
field3.setText(ToString(a*c,b*d));
}
else if(e.getSource()==button4){
}
else{
field1.setText("");
}
}
public String ToString(int zi,int mu){
if(mu==0)return "分母不能为零";
if(zi==0)return zi+"/1";
else{
int ma=max(zi,mu);
return zi/ma+"/"+mu/ma;
}
}
public int max(int a,int b){
int temp,ma;
if(a<b){temp=a;a=b;b=temp;}
while((ma=a%b)!=0){
a=b;b=ma;
}
return b;
}
}
复制代码
这是我写的一个程序,怎样让它出现分母不能为0的警号,窗口的尺寸如何设置,还有怎么样让它带有背景颜色
求大神帮忙改改,然后把代码发给我
作者:
手心里的温柔
时间:
2013-8-19 14:35
可以用 JOptionPane.showMessageDialog(this, "分母不能为0!") 弹出消息对话框来实现
希望可以帮到你、
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2