JAVA高手来帮帮我
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA高手来帮帮我相关的知识,希望对你有一定的参考价值。
求和工具。编写一个程序,在第一个文本框中输入第一个加数;第二个文本框中输入第二个加数;当单击第三个“=”按钮时,在第三个文本框中显示出前两个加数之和。(要求是一个界面表示出这道题)
import javax.swing.SwingUtilities;import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
public class GetSum extends JFrame
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextField first = null;
private JLabel plus = null;
private JTextField second = null;
private JButton getBt = null;
private JTextField sum = null;
private JButton clearBt = null;
private JTextField getFirst()
if (first == null)
first = new JTextField();
first.setText("0");
first.setBounds(new Rectangle(5, 19, 80, 22));
return first;
private JTextField getSecond()
if (second == null)
second = new JTextField();
second.setText("0");
second.setBounds(new Rectangle(106, 19, 80, 22));
return second;
private JButton getGetBt()
if (getBt == null)
getBt = new JButton("=");
getBt.setBounds(new Rectangle(185, 19, 44, 22));
getBt.addActionListener(new java.awt.event.ActionListener()
public void actionPerformed(java.awt.event.ActionEvent e)
int firstNum = Integer.parseInt(first.getText().trim());
int secondNum = Integer.parseInt(second.getText().trim());
int sumNum = firstNum + secondNum;
sum.setText(""+sumNum);
);
return getBt;
private JTextField getSum()
if (sum == null)
sum = new JTextField();
sum.setEditable(false);
sum.setBounds(new Rectangle(229, 19, 102, 22));
return sum;
private JButton getClearBt()
if (clearBt == null)
clearBt = new JButton();
clearBt.setBounds(new Rectangle(138, 58, 64, 18));
clearBt.setText("清空");
clearBt.addActionListener(new java.awt.event.ActionListener()
public void actionPerformed(java.awt.event.ActionEvent e)
first.setText("0");
second.setText("0");
sum.setText("");
);
return clearBt;
public static void main(String[] args)
SwingUtilities.invokeLater(new Runnable()
public void run()
GetSum thisClass = new GetSum();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
);
public GetSum()
super();
initialize();
this.setResizable(false);
private void initialize()
this.setBounds(100, 300, 354, 118);
this.setContentPane(getJContentPane());
this.setTitle("求和");
private JPanel getJContentPane()
if (jContentPane == null)
plus = new JLabel();
plus.setBounds(new Rectangle(84, 21, 23, 18));
plus.setHorizontalAlignment(SwingConstants.CENTER);
plus.setText("+");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getFirst(), null);
jContentPane.add(plus, null);
jContentPane.add(getSecond(), null);
jContentPane.add(getGetBt(), null);
jContentPane.add(getSum(), null);
jContentPane.add(getClearBt(), null);
return jContentPane;
是不是要这样的啊?呵呵.
不过,要是输入的不是数字的话可不行啊,因为我没有判断输入的是不是数字,你可以自己添加方法。还有,我只是用了整型的数字,你可以改成double类型的。 参考技术A 无分...茫然... 参考技术B 去网上下载计算器程序源代码用java的写的 参考技术C import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class Coin extends JFrame
//总框架
public Coin()
setTitle("投币游戏");
CoinPanel panel = new CoinPanel();
add(panel);
pack();
public static void main(String[] args)
Coin coin = new Coin();
coin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
coin.setVisible(true);
//创建版面
class CoinPanel extends JPanel
private JButton display1 = new JButton("正面数:");
private JButton display2 = new JButton("反面数:");
private JButton display3 = new JButton("差面数:");
private JButton display4 = new JButton("0");
private JButton display5 = new JButton("0");
private JButton display6 = new JButton("0");
private JPanel pan;
private JPanel panel1;//输入面版
private JPanel panel2;//显示面版
private boolean start;//开始标志
JTextField tf1 ;
JTextField tf2 ;
//static Integer a;
public CoinPanel()
setLayout(new BorderLayout());
tf1 = new JTextField(10);
tf2 = new JTextField(10);
JButton bt1 = new JButton("开 始");
JButton bt2 = new JButton("开 始");
JPanel panel_tf1 = new JPanel( );
JPanel panel_tf2 = new JPanel( );
panel_tf1.add(tf1);
panel_tf2.add(tf2);
panel_tf1.setBorder(new TitledBorder("投币次数"));
panel_tf2.setBorder(new TitledBorder("正方面差数"));
JPanel pan = new JPanel(new FlowLayout(FlowLayout.CENTER));
//pan.add(panel_tf3);
//tf1.getText();
//创建事件侦听对象
bt1.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent event)
//System.out.print(tf1.getText());
int input1 = Integer.parseInt(tf1.getText());
//String input1 = tf1.getText();
//String input2 = tf2.getText();
int Z=0,F=0;
double P = 0.5D;
Random random = new Random();
//int input1 =10;
for(int i=0;i<input1;i++)
double randomNum = random.nextDouble() ;
if(randomNum<=P)
Z++;
else
F++;
Integer a = new Integer(Z);
Integer b = new Integer(F);
Integer c = new Integer(Z-F);
display4.setText(a.toString());
display5.setText(b.toString());
display6.setText(c.toString());
// System.out.println(a.toString());
);
bt2.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent event)
Random random = new Random();
int Z=0,F=0;
double P = 0.5D;
//int input2 = 3;
boolean B = true;
int input2 = Integer.parseInt(tf2.getText());
while(B)
double randomNum = random.nextDouble() ;
if(randomNum<=P)
Z++;
else
F++;
if(Z-F==input2)
B = false;
Integer a = new Integer(Z);
Integer b = new Integer(F);
Integer c = new Integer(Z-F);
display4.setText(a.toString());
display5.setText(b.toString());
display6.setText(c.toString());
//System.out.println(a.toString());
);
pan.add(panel_tf1);
pan.add(bt1);
pan.add(panel_tf2);
pan.add(bt2);
add(pan,BorderLayout.NORTH);
//添加数字及符号按钮的面板面板
panel1 = new JPanel();
panel1.setLayout(new GridLayout(3, 2));//设置面板布局为4行4列
//添加数字及符号按钮
display1.setEnabled(false);
panel1.add(display1);
display4.setEnabled(false);
panel1.add(display4);
display2.setEnabled(false);
panel1.add(display2);
display5.setEnabled(false);
panel1.add(display5);
display3.setEnabled(false);
panel1.add(display3);
display6.setEnabled(false);
panel1.add(display6);
add(panel1, BorderLayout.SOUTH);
我以前写的一个小投币,游戏,你稍微改下就可以了,有输入框,事件自己写吧! 参考技术D import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GetSum extends JFrame
JPanel pane1=new JPanel();
JPanel pane2=new JPanel();
JPanel pane3=new JPanel();
JButton button=new JButton("=");
JTextField num1=new JTextField(10);
JTextField num2=new JTextField(10);
JTextField sum=new JTextField(10);
private GetSum()
pane1.setLayout(new GridLayout(1,2,30,30));
pane1.add(num1);
pane1.add(num2);
pane1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"加数输入区"));
add(pane1,BorderLayout.NORTH);
pane2.add(button);
add(pane2,BorderLayout.CENTER);
pane3.add(sum);
pane3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"结果显示区"));
add(pane3,BorderLayout.SOUTH);
setSize(400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
button.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent arg0)
double a=Integer.parseInt(num1.getText());
double b=Integer.parseInt(num2.getText());
Double c=a+b;
sum.setText(c.toString());
);
public static void main(String[] args)
new GetSum();
我写的比较简短些吧,(*^__^*) ……
不过我也没判断输入的是不是数字。不过我用的是double型哟! 第5个回答 2008-12-07 //以前我写过..后面删除了..呵呵..现在重新给你写个.你看看吧
//给你加了判断.这样就不会出错了..
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test extends JFrame
JTextField jt1, jt2, jt3, jt4;
public Test()
Container c=getContentPane();
c.setLayout(new FlowLayout());
JLabel jl1=new JLabel("参数数值");
jt1=new JTextField(10);
c.add(jl1);
c.add(jt1);
JLabel jl2=new JLabel("参数数值");
jt2=new JTextField(10);
c.add(jl2);
c.add(jt2);
JLabel jl3=new JLabel("= - * /");
jt3=new JTextField(10);
c.add(jl3);
c.add(jt3);
final JLabel jl4=new JLabel("结果");
jt4=new JTextField(10);
jt4.setEditable(false);
c.add(jl4);
c.add(jt4);
JButton jb=new JButton("计算");
c.add(jb);
setSize(190, 180);
setVisible(true);
jb.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent event)
try
int a=Integer.parseInt(jt1.getText());
int b=Integer.parseInt(jt2.getText());
if(jt3.getText().equals("+"))
jt4.setText((a+b)+"");
else if(jt3.getText().equals("-"))
jt4.setText((a-b)+"");
else if(jt3.getText().equals("*"))
jt4.setText((a*b)+"");
else if(jt3.getText().equals("/"))
jt4.setText((a/b)+"");
else
JOptionPane.showMessageDialog(null, "检查你输入的是否是 = - * / 中一个");
catch (Exception e)
JOptionPane.showMessageDialog(null,"请输入正确的参数");
);
public static void main(String[] args)
Test s = new Test();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JAVA 闹钟程序
请高手帮我个忙啊。。我想做个JAVA闹钟。请高手给我个实例代码。。谢谢了。。在网上百度了几个都没用。。(或者我不知道用)。。请高手帮帮忙啊。。小弟我刚学。。谢谢
import java.util.*;import java.awt.*;
import java.applet.*;
import java.text.*;
import java.awt.event.*;
public class Alarm extends Applet implements Runnable
Thread timer=null; //创建线程timer
Image gif1; //clockp:闹钟的外壳,闹铃和报时物
boolean setflag=false,stopflag=false,cancelflag=false;
Panel setpanel;
//获取声音文件
AudioClip ring=getAudioClip(getCodeBase(), "1.mid");
Button setbutton=new Button("SET");
Button cancelbutton=new Button("CANCEL");
Button stopbutton=new Button("STOP");
//响应按钮事件
private ActionListener setli=new ActionListener()
public void actionPerformed(ActionEvent e)
setflag=true;
;
private ActionListener cancelli=new ActionListener()
public void actionPerformed(ActionEvent e)
setflag=true;
;
private ActionListener stopli=new ActionListener()
public void actionPerformed(ActionEvent e)
ring.stop();
//清除的方法
//g.clearRect(83,280,20,30);
;
Label note1=new Label("Alarm clock:");
//GregorianCalendar提供的是一个日历式的东东,上面又多了很多的参数,是方便操作了不少。而Date类的功能远不及其,求个和日期有联系的还要自己计算。
GregorianCalendar cal=new GregorianCalendar();
GregorianCalendar cal2=new GregorianCalendar();
SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式
Date dummy=new Date(); //生成Data对象
String lastdate=df.format(dummy);
Font F=new Font("TimesRoman",Font.PLAIN,14);//设置字体格式
Date dat=null;
Date timeNow;
Color fgcol=Color.blue;
Color fgcol2=Color.darkGray;
Color backcolor=Color.blue;
Label hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)
int i;
int s,m,h;
TextField sethour,setmin,setsec;//显示当前时间文本框和定时文本框
//在Applet程序中,首先自动调用初始化完成必要的初始化工作,紧接着自动调用start,在进入执行程序和返回到该页面时被调用,而从该页面转到别的页面时,stop被调用,关闭浏览器时,执行destroy。
public void init()//初始化方法
int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数
setLayout(null); //将布局管理器初始化为null
setpanel=new Panel();
setpanel.setLayout(null);
setpanel.add(note1);
note1.setBounds(30,100,60,20);
note1.setBackground(backcolor);
note1.setForeground(Color.black);
//定时用的文本框(时、分、秒)
sethour=new TextField("00",5);
setmin=new TextField("00",5);
setsec=new TextField("00",5);
hlabel2=new Label();
mlabel2=new Label();
slabel2=new Label();
//定时的小时文本框的位置、大小
setpanel.add(sethour);
sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);
sethour.setBackground(Color.white);
//在文本框后加入单位“时”
setpanel.add(hlabel2);
hlabel2.setText("h");
hlabel2.setBackground(backcolor);
hlabel2.setForeground(Color.black);
hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定时的分钟文本框的位置、大小
setpanel.add(setmin);
setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);
setmin.setBackground(Color.white);
//在文本框后加入单位“分”
setpanel.add(mlabel2);
mlabel2.setText("m");
mlabel2.setBackground(backcolor);
mlabel2.setForeground(Color.black);
mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定时的秒文本框的位置、大小
setpanel.add(setsec);
setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);
setsec.setBackground(Color.white);
//在文本框后加入单位“秒”
setpanel.add(slabel2);
slabel2.setText("s");
slabel2.setBackground(backcolor);
slabel2.setForeground(Color.black);
slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
//设置闹钟控制按钮(on,off)
setpanel.add(cancelbutton);
setpanel.add(setbutton);
setpanel.add(stopbutton);
cancelbutton.setBounds(90,180,40,20);
setbutton.setBounds(140,180,40,20);
stopbutton.setBounds(522,180,40,20);
setbutton.addActionListener(setli);
cancelbutton.addActionListener(cancelli);
stopbutton.addActionListener(stopli);
stopbutton.setVisible(false);
//将面板加入当前容器中,并设置面板的大小和背景色
add(setpanel);
setpanel.setBounds(300,1,250,420);
setpanel.setBackground(backcolor);
/*int xcenter,ycenter,s,m,h;
//闹钟中心点所在位置
xcenter=145;
ycenter=162;
s=(int)cal.get(Calendar.SECOND);
m=(int)cal.get(Calendar.MINUTE);
h=(int)cal.get(Calendar.HOUR_OF_DAY);
//初始化指针位置
lastxs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);
lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);
lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);
lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);
lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);
lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);
lasts=s; */
MediaTracker mt=new MediaTracker(this);//为给定组件创建一个跟踪媒体的MediaTracker对象,把图片添加到被跟踪的图片组
//Java允?Sapplet??HTML所在的位置(decument base)下?d?Y料,也允?Sapplet?钠涑淌酱a所在的位置(code base)下?d?Y料。藉由呼叫getDocumentBase()?cgotCodeBase()可得到URL物件。?@些函?????湍阏业侥阆胂螺d的?n案的位置
//clockp=getImage(getDocumentBase(),"11.png");
gif1=getImage(getCodeBase(),"2.gif");
//i为id号
mt.addImage(gif1,i++);
try
mt.waitForAll();
catch(InterruptedException e)
;//等待加载结束
resize(600,420);//设置窗口大小
//窗口显示有改变的时候调用paint
public void paint(Graphics g)
//重写paint()方法
int xh,yh,xm,ym,xs,ys,strike_times;
int xcenter,ycenter;
String today;
xcenter=148;
ycenter=186;
dat=new Date();
//用当前时间初始化日历时间
cal.setTime(dat);
//读取当前时间
s=(int)cal.get(Calendar.SECOND);
m=(int)cal.get(Calendar.MINUTE);
h=(int)cal.get(Calendar.HOUR_OF_DAY);
//换一种时间表达形式
today=df.format(dat);
//指针位置
xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);
xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*12+xcenter);
yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*12+ycenter);
//设置字体和颜色
g.setFont(F);
//前景色
g.setColor(getBackground()); //取背景色的
g.drawImage(gif1,75,110,this);
//以数字方式显示年、月、日和时间
g.drawString(today,55,415);
//画指针
g.drawLine(xcenter,ycenter,xs,ys);
g.drawLine(xcenter,ycenter-1,xm,ym); //(x1,y1,x2,y2)
g.drawLine(xcenter-1,ycenter,xm,ym);
g.drawLine(xcenter,ycenter-1,xh,yh);
g.drawLine(xcenter-1,ycenter,xh,yh);
int timedelta;//记录当前时间与闹铃定时的时差
Integer currh,currm,currs;//分别记录当前的时、分、秒
Date dat2=new Date();
cal2.setTime(dat2);
//读取当前时间
currh=(int)cal2.get(Calendar.SECOND);
currm=(int)cal2.get(Calendar.MINUTE);
currs=(int)cal2.get(Calendar.HOUR_OF_DAY);
//这样做的话说我API已过时
//timeNow=new Date();
//currh=new Integer(timeNow.getHours());
//currm=new Integer(timeNow.getMinutes());
//currs=new Integer(timeNow.getSeconds());
if(setflag)
//判断是否设置了闹钟
//判断当前时间是否为闹钟所定的时间
if((currh.intValue()==Integer.valueOf(sethour.getText()).intValue())&&(currm.intValue()==Integer.valueOf(setmin.getText()).intValue())&&(currs.intValue()==Integer.valueOf(setsec.getText()).intValue()))
ring.play();
g.drawImage(gif1,83,280,this);
stopbutton.setVisible(true);
timedelta=currm.intValue()*60+currs.intValue()-Integer.valueOf(setmin.getText()).intValue()*60-Integer.valueOf(setsec.getText()).intValue();
if((timedelta>=30))
//若当前时间与闹钟相差时间超过30秒,闹钟自动停
ring.stop();
//清除的方法
g.clearRect(83,280,20,30);
dat=null;
public void start()
if(timer==null)
timer=new Thread(this);//将timer实例化
timer.start();
public void stop()
timer=null;
//给创建线程后start之后自动执行的函数
public void run()
//在run()方法中,调用repaint()方法,以重绘小程序区,进行时钟显示的更新。接着调用sleep方法让当前线程(也就是我们创建的线程clockthread)睡眠1000毫秒,因为我们每秒钟要更新一下显示,所以让它睡眠1秒
while(timer!=null)
try
timer.sleep(1000);
catch(InterruptedException e)
//调用repaint时,会首先清除掉paint方法之前的画的内容,再调用paint方法
repaint();//刷新画面
timer=null;
//当AWT接收到一个applet的重绘请求时,它就调用applet的 update(),默认地,update() 清除applet的背景,然后调用 paint()。重载 update(),将以前在paint()中的绘图代码包含在update()中,从而避免每次重绘时将整个区域清除
//有两种方法可以明显地减弱闪烁:重载 update()或使用双缓冲。
//使用双缓冲技术:另一种减小帧之间闪烁的方法是使用双缓冲,它在许多动画Applet中被使用。其主要原理是创建一个后台图像,将需要绘制的一帧画入图像,然后调用DrawImage()将整个图像一次画到屏幕上去;好处是大部分绘制是离屏的,将离屏图像一次绘至屏幕上比直接在屏幕上绘制要有效得多,大大提高做图的性能。
// 双缓冲可以使动画平滑,但有一个缺点,要分配一张后台图像,如果图像相当大,这将需要很大一块内存;当你使用双缓冲技术时,应重载 update()。
public void update(Graphics g)
Image offscreen_buf=null;
//采用双缓冲技术的update()方法
if(offscreen_buf==null)
offscreen_buf=createImage(600,420);
Graphics offg=offscreen_buf.getGraphics();
offg.clipRect(1,1,599,419);
paint(offg);
Graphics ong=getGraphics();
ong.clipRect(1,1,599,419);
ong.drawImage(offscreen_buf,0,0,this);
/** Creates a new instance of AlarmClock */
参考资料:http://conjs.cn
参考技术A //OK 写好了...怕你不懂 帮你加了注释package 娱乐;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class Alarm extends JFrame implements Runnable
JLabel ri ,shi, fen, miao, dangqian;
JButton queding, dakai;
JTextField music,RI, SHI, FEN, MIAO;
int h=0,f=0,m=0,r=0;
boolean fo=false;
public AudioClip soumd1;
public Alarm()
Container c = getContentPane();
c.setLayout(new GridLayout(3, 1));
JPanel jp = new JPanel();
dangqian = new JLabel();
jp.add(dangqian);
c.add(jp);
JPanel jp1 = new JPanel();
music = new JTextField(20);
dakai = new JButton("选择闹铃音乐");
jp1.add(music);
jp1.add(dakai);
c.add(jp1);
ri = new JLabel("日");
RI = new JTextField(4);
shi = new JLabel("时");
SHI = new JTextField(4);
fen = new JLabel("分");
FEN = new JTextField(4);
miao = new JLabel("秒");
MIAO = new JTextField(4);
JPanel jp2 = new JPanel();
jp2.add(ri);
jp2.add(RI);
jp2.add(shi);
jp2.add(SHI);
jp2.add(fen);
jp2.add(FEN);
jp2.add(miao);
jp2.add(MIAO);
queding = new JButton("确定");
jp2.add(queding);
c.add(jp2);
setSize(400, 130);
setVisible(true);
dakai.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent event)
JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
fileChooser.setCurrentDirectory(new File(".")); // 设置文件选择器当前目录
fileChooser
.setFileFilter(new javax.swing.filechooser.FileFilter()
public boolean accept(File file) // 可接受的文件类型
String name = file.getName().toLowerCase();
return name.endsWith(".wav")
|| name.endsWith(".au")
|| file.isDirectory();
public String getDescription() // 文件描述
return "音乐文件(*.wav,*.au)";
);
if (fileChooser.showOpenDialog(Alarm.this) == JFileChooser.APPROVE_OPTION) // 弹出文件选择器,并判断是否点击了打开按钮
String fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到选择文件或目录的绝对路径
music.setText(fileName);
);
queding.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent event)
if(queding.getText().equals("确定"))
try
r=Integer.parseInt(RI.getText());
h=Integer.parseInt(SHI.getText());
f=Integer.parseInt(FEN.getText());
m=Integer.parseInt(MIAO.getText());
if(1<=h&&h<=31&&0<=h&&h<=23&&0<=f&&f<=59&&0<=m&&m<=59)
fo=true;
else
JOptionPane.showMessageDialog(null, "输入时间错误");
catch(Exception e)
JOptionPane.showMessageDialog(null, "请输入正确的时间");
else
fo=false;
RI.setEditable(true);
SHI.setEditable(true);
FEN.setEditable(true);
MIAO.setEditable(true);
queding.setText("确定");
soumd1.stop();
);
public static void main(String agrs[])
Alarm s = new Alarm();
Thread t1 = new Thread(s);
t1.start();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public void run()
while (true)
Date now = new Date();
dangqian.setText("当前时间: " + now.toString());
if(fo)
RI.setEditable(false);
SHI.setEditable(false);
FEN.setEditable(false);
MIAO.setEditable(false);
queding.setText("关闭");
SimpleDateFormat ri=new SimpleDateFormat("dd"); //封装 为了获取日期
SimpleDateFormat shi=new SimpleDateFormat("kk"); //封装 为了获取小时
SimpleDateFormat fen=new SimpleDateFormat("mm"); //封装 为了获取分钟
SimpleDateFormat miao=new SimpleDateFormat("ss"); //封装 为了获取秒钟
int riqi=Integer.parseInt(ri.format(now)); //获取日期
int shizhong=Integer.parseInt(shi.format(now)); //获取小时
int fenzhong=Integer.parseInt(fen.format(now)); //获取分钟
int miaozhong=Integer.parseInt(miao.format(now)); //获取秒钟
if(riqi==r&&shizhong==h&&fenzhong==f&&miaozhong==m) //判断条件
try
soumd1=Applet.newAudioClip(new File(music.getText()).toURL()); //播放音乐
soumd1.loop(); //我设置的是循环播放..这样不起床都不行..
fo=false;
catch (MalformedURLException e)
e.printStackTrace();
try
Thread.sleep(1000);
catch (InterruptedException ie)
本回答被提问者和网友采纳 参考技术B import java.util.Timer;
import java.util.TimerTask;
public class MyDoit
public static void main(String[] args)
new Timer().schedule(new TimerTask()
public void run()
System.out.println("简单的闹钟");
, 0, 1000 * 60 * 60 * 24);
以上是关于JAVA高手来帮帮我的主要内容,如果未能解决你的问题,请参考以下文章
想问下游戏开发高手,flex+java socket怎么实现多人在线,先登录得玩家可以看到后进来的玩家???谢谢!