import javaawt;//计算器实例
import javaawtevent;
public class calculator
{
public static void main(String args[])
{
MyWindow my=new MyWindow("计算器");
}
}
class MyWindow extends Frame implements ActionListener
{ StringBuffer m=new StringBuffer();
int p;
TextField tex;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,jia,jian,cheng,chu,deng,dian,qingling,kaifang;
MyWindow(String s)
{
super(s);
//StringBuffer s2=new StringBuffer();
//String s;
tex=new TextField(18);
b0=new Button(" 0 ");
b1=new Button(" 1 ");
b2=new Button(" 2 ");
b3=new Button(" 3 ");
b4=new Button(" 4 ");
b5=new Button(" 5 ");
b6=new Button(" 6 ");
b7=new Button(" 7 ");
b8=new Button(" 8 ");
b9=new Button(" 9 ");
dian=new Button(" ");
jia=new Button(" + ");
jian=new Button(" - ");
cheng=new Button(" × ");
chu=new Button(" / ");
deng=new Button(" = ");
qingling=new Button(" 清零 ");
kaifang=new Button(" √ ");
setLayout(new FlowLayout());
add(tex);
add(b0);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(dian);
add(jia);
add(jian);
add(cheng);
add(chu);
add(kaifang);
add(qingling);
add(deng);
b0addActionListener(this);
b1addActionListener(this);
b2addActionListener(this);
b3addActionListener(this);
b4addActionListener(this);
b5addActionListener(this);
b6addActionListener(this);
b7addActionListener(this);
b8addActionListener(this);
b9addActionListener(this);
jiaaddActionListener(this);
jianaddActionListener(this);
chengaddActionListener(this);
chuaddActionListener(this);
dianaddActionListener(this);
dengaddActionListener(this);
qinglingaddActionListener(this);
kaifangaddActionListener(this);
setBounds(200,200,160,280);
setResizable(false);//不可改变大小
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent ee)
{ Systemexit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(egetSource()==b0)
{
m=mappend("0");
texsetText(StringvalueOf(m));
}
if(egetSource()==b1)
{
m=mappend("1"); texsetText(StringvalueOf(m));
}
if(egetSource()==b2)
{
m=mappend("2"); texsetText(StringvalueOf(m));
}
if(egetSource()==b3)
{
m=mappend("3"); texsetText(StringvalueOf(m));
}
if(egetSource()==b4)
{
m=mappend("4"); texsetText(StringvalueOf(m));
}
if(egetSource()==b5)
{
m=mappend("5"); texsetText(StringvalueOf(m));
}
if(egetSource()==b6)
{
m=mappend("6"); texsetText(StringvalueOf(m));
}
if(egetSource()==b7)
{
m=mappend("7"); texsetText(StringvalueOf(m));
}
if(egetSource()==b8)
{
m=mappend("8"); texsetText(StringvalueOf(m));
}
if(egetSource()==b9)
{
m=mappend("9"); texsetText(StringvalueOf(m));
}
if(egetSource()==jia)
{
m=mappend("+"); texsetText(StringvalueOf(m));
}
if(egetSource()==jian)
{
m=mappend("-"); texsetText(StringvalueOf(m));
}
if(egetSource()==cheng)
{
m=mappend(""); texsetText(StringvalueOf(m));
}
if(egetSource()==chu)
{
m=mappend("/"); texsetText(StringvalueOf(m));
}
if(egetSource()==dian)
{
m=mappend(""); texsetText(StringvalueOf(m));
}
String mm=StringvalueOf(m);
int p1=mmindexOf("+");
int p2=mmindexOf("-");
int p3=mmindexOf("");
int p4=mmindexOf("/");
if(p1!=-1)
{
p=p1;
}
else if(p3!=-1)
{
p=p3;
}
else if(p2!=-1)
{
p=p2;
}
else if(p4!=-1)
{
p=p4;
}
if(egetSource()==deng)
{
String m1=mmsubstring(0,p);
String m2=mmsubstring(p+1);
String ch=mmsubstring(p,p+1);
//Systemoutprintln(m1);
//Systemoutprintln(m2);
//Systemoutprintln(ch);
if(chequals("+"))
{
float n1=FloatparseFloat(m1);
float n2=FloatparseFloat(m2);
float sum=n1+n2;
String su=StringvalueOf(sum);
texsetText(su);
}
if(chequals("-"))
{
float n1=FloatparseFloat(m1);
float n2=FloatparseFloat(m2);
float sum=n1-n2;
String su=StringvalueOf(sum);
texsetText(su);
}
if(chequals(""))
{
float n1=FloatparseFloat(m1);
float n2=FloatparseFloat(m2);
float sum=n1n2;
String su=StringvalueOf(sum);
texsetText(su);
}
if(chequals("/"))
{
float n1=FloatparseFloat(m1);
float n2=FloatparseFloat(m2);
float sum=n1/n2;
String su=StringvalueOf(sum);
texsetText(su);
}
}
if(egetSource()==qingling)
{StringBuffer kk=new StringBuffer();
m=kk;
texsetText("0");
// Systemoutprintln(mm);
}
if(egetSource()==kaifang)
{
String t=texgetText();
float num=FloatparseFloat(t);
double nub=Mathsqrt(num);
texsetText(StringvalueOf(nub));
}
}
}
帮你做了一个,不知是否满意呢?
import javaappletApplet;
import javaawtButton;
import javaawtColor;
import javaawtGraphics;
import javaawtTextField;
import javaawteventActionEvent;
import javautilRandom;
import javautilVector;
public class Lottery extends Applet {
private static final long serialVersionUID = 1L;
int w,h;
Button ok,out,setup;
String[] msg;
TextField[] gaiLv;
TextField[] jiangPin;
int mx,ml;
int maxNum;
Random ran;
Vector<Integer> fist;
Vector<Integer> sec;
Vector<Integer> third;
Vector<Integer> lucky;
boolean lot=false;
boolean iserr=false;
boolean issetup=false;
String mesg="输入错误";
String priseMsg="继续努力!";
public void init(){
w=400;
h=220;
mx=20;
ml=40;
ran=new Random();
thissetSize(w, h);
thissetLayout(null);
ok=new Button("抽奖");
out=new Button("退出");
setup=new Button("确认设置");
msg=new String[4];
msg[0]="一等奖";
msg[1]="二等奖";
msg[2]="三等奖";
msg[3]="幸运奖";
gaiLv=new TextField[4];
jiangPin=new TextField[4];
for(int i=0;i<4;i++){
gaiLv[i]=new TextField("00"+(i+1));
thisadd(gaiLv[i]);
gaiLv[i]setBounds(mx+ml, 75+i26, 60, 18);
jiangPin[i]=new TextField();
thisadd(jiangPin[i]);
jiangPin[i]setBounds(mx+ml3, 75+i26, 80, 18);
}
thisadd(ok);
oksetBounds(260, 180, 60, 28);
okaddActionListener(new LotButtonAction(this));
thisadd(out);
outsetBounds(330, 180, 60, 28);
outaddActionListener(new LotButtonAction(this));
thisadd(setup);
setupsetBounds(110, 180, 80, 24);
setupaddActionListener(new LotButtonAction(this));
}
public void paint(Graphics g){
gsetColor(Colorwhite);
gfillRect(0, 0, thisgetWidth(), thisgetHeight());
gsetColor(new Color(230,255,230));
gfillRect(0, 0, w, 30);
gsetColor(ColorBLUE);
gdrawString("JAVA抽奖系统", 130, 20);
gsetColor(ColorORANGE);
gdrawRect(10, 40, 230, 170);
gsetColor(ColorBLACK);
gdrawString("设置", mx, 60);
gdrawString("概率", mx+ml, 60);
gdrawString("奖品", mx+ml3, 60);
for(int i=0;i<msglength;i++){
gsetColor(new Color(255-(i30),45,89));
gdrawString(msg[i], 20, 90+i26);
}
if(lot==true){
gsetColor(new Color(rannextInt(255),rannextInt(255),rannextInt(255)));
if(priseMsglength()<=7){
gdrawString(priseMsg, 260, 100);
}else{
gdrawString(priseMsgsubstring(0, 5), 260, 100);
gdrawString(priseMsgsubstring(5), 260, 120);
}
}
if(iserr==true){
gdrawString(mesg, 260, 100);
}
}
public void getLucky(){
float firu=1;
float secu=1;
float thiu=1;
float fouu=1;
float minu=1;
if(gaiLv[0]getText()trim()length()>1){
firu=FloatparseFloat(gaiLv[0]getText());
if(firu<=0||firu>=1){
iserr=true;
return;
}
if(firu<minu){
minu=firu;
}
}
if(gaiLv[1]getText()trim()length()>1){
secu=FloatparseFloat(gaiLv[1]getText());
if(secu<=0||secu>=1){
iserr=true;
return;
}
if(secu<minu){
minu=secu;
}
}
if(gaiLv[2]getText()trim()length()>1){
thiu=FloatparseFloat(gaiLv[2]getText());
if(thiu<=0||thiu>=1){
iserr=true;
return;
}
if(thiu<minu){
minu=thiu;
}
}
if(gaiLv[3]getText()trim()length()>1){
fouu=FloatparseFloat(gaiLv[3]getText());
if(fouu<=0||fouu>=1){
iserr=true;
return;
}
if(fouu<minu){
minu=fouu;
}
}
if(minu>=1||minu<=0){
iserr=true;
return;
}
float aNum=1/minu;
maxNum=(int)aNum;
int count=(int)(firu/minu);
if(firu!=1){
fist=getLotteryVec(maxNum,count);
}else{
fistremoveAllElements();
}
count=(int)(secu/minu);
if(secu!=1){
sec=getLotteryVec(maxNum,count);
}else{
secremoveAllElements();
}
count=(int)(thiu/minu);
if(thiu!=1){
third=getLotteryVec(maxNum,count);
}else{
thirdremoveAllElements();
}
count=(int)(fouu/minu);
if(fouu!=1){
lucky=getLotteryVec(maxNum,count);
}else{
luckyremoveAllElements();
}
issetup=true;
iserr=false;
}
protected Vector<Integer> getLotteryVec(int maxNum,int num){
Vector<Integer> result=new Vector<Integer>();
for(int i=0;i<num;i++){
resultadd(rannextInt(maxNum));
}
return result;
}
protected int getaNum(){
return rannextInt(maxNum);
}
public int isLucky(int pNum){
for(int i=0;i<fistsize();i++){
if(fistget(i)==pNum){
return 1;
}
}
for(int i=0;i<secsize();i++){
if(secget(i)==pNum){
return 2;
}
}
for(int i=0;i<thirdsize();i++){
if(thirdget(i)==pNum){
return 3;
}
}
for(int i=0;i<luckysize();i++){
if(luckyget(i)==pNum){
return 4;
}
}
return -1;
}
public void ButtonActionPerformed(ActionEvent e){
String acName=egetActionCommand();
if(acNameequals("抽奖")){
if(issetup==false){
priseMsg="请先设置参数!";
lot=true;
repaint();
lot=false;
return;
}
lot=true;
priseMsg=getResult(getaNum());
repaint();
// lot=false;
}else if(acNameequals("退出")){
thissetVisible(false);
thisstop();
thisdestroy();
Systemexit(0);
}else if(acNameequals("确认设置")){
lot=false;
getLucky();
repaint();
}
}
public String getResult(int num){
int resu=isLucky(num);
String result="";
switch(resu){
case -1:
result="继续努力!";
break;
case 1:
result="恭喜你!一等奖!\n获得"+jiangPin[0]getText();
break;
case 2:
result="恭喜你!二等奖!\n获得"+jiangPin[1]getText();
break;
case 3:
result="恭喜你!三等奖!\n获得"+jiangPin[2]getText();
break;
case 4:
result="恭喜你!幸运奖!\n获得"+jiangPin[3]getText();
break;
default:
result="继续努力!";
}
return result+num;
}
}
class LotButtonAction implements javaawteventActionListener{
Lottery su;
public LotButtonAction(Lottery bun){
thissu=bun;
}
@Override
public void actionPerformed(ActionEvent e) {
suButtonActionPerformed(e);
}
}
这个语句的意思是创建一个Button对象,并将其实例化,findViewById 就是根据 ID RidButton01实例化Button, 其中RidButton01是项目编译之后在gen目录下自动生成的R文件中的一个整型数,代表的是 Activity所使用布局文件 xml中定义的对应的Button的ID 号,就是拥有属性
android:id=@+id/Button01的Button
你都会编这么多的代码了,怎么就剩下这两步不会?
import javaawtButton;
import javaawtColor;
import javaawtFlowLayout;
import javaawtFrame;
import javaawtLabel;
import javaawtTextField;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventWindowAdapter;
import javaawteventWindowEvent;
public class Round extends Frame implements ActionListener {
TextField t1, t2, t3, t4;
Button b1;
Button btnExit;
public Round() {
setLayout(new FlowLayout());
t1 = new TextField(20);
t1setBackground(Colororange);
t2 = new TextField(20);
t2setBackground(Colororange);
t3 = new TextField(20);
t3setBackground(Colororange);
t4 = new TextField(20);
t4setBackground(Colororange);
b1 = new Button("计算");
btnExit = new Button("退出");
add(new Label("输入圆的半径:"));
add(t1);
add(new Label("得出圆的直径:"));
add(t2);
add(new Label("得出圆的面积:"));
add(t3);
add(new Label("得出圆的周长:"));
add(t4);
add(b1);
add(btnExit);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Systemexit(0);
}
});
b1addActionListener(this);
btnExitaddActionListener(this);
setVisible(true);
setBounds(200, 200, 200, 300);
validate();
}
public void actionPerformed(ActionEvent e) {
if(egetSource()==b1){
double temp, r, a, c;
temp = FloatparseFloat(t1getText());
r = 2 temp;
a = 314 temp temp;
c = 2 314 temp;
t2setText(StringvalueOf(r));
t3setText(StringvalueOf(a));
t4setText(StringvalueOf(c));
}
if(egetSource()==btnExit){
Systemexit(0);
}
}
public static void main(String args[]) {
new Round();
}
}
以上就是关于java程序全部的内容,包括:java程序、java 制作抽奖小程序、在Android编程中,这个语句Button mButton=(Button)findViewBYID(R.id.Button01);...等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)