Ⅰ java秒表小程序编写

收藏的一个小程序。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
public class TimerApplet extends JApplet implements ActionListener{
private JLabel lblTime=new JLabel("时间");
private JButton btnStart=new JButton("开始");
private JButton btnEnd=new JButton("结束");
private long startTime=System.currentTimeMillis();
private boolean isRun=false;
private NumberFormat numberFormat=new DecimalFormat("0.000");
public TimerApplet(){
this.setLayout(new FlowLayout());
this.add(lblTime);
this.add(btnStart);
this.add(btnEnd);
btnStart.addActionListener(this);
btnEnd.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnStart){
class TimerThread implements Runnable{
public void run() {
isRun=true;
startTime=System.currentTimeMillis();
while(isRun){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double time=(System.currentTimeMillis()-startTime);
lblTime.setText(numberFormat.format(time/1000));
}
}
}
new Thread(new TimerThread()).start();
}
if(e.getSource()==btnEnd){
isRun=false;
}
}
}

Ⅱ 求java经典小程序代码

  • 代码来如下源:

  • public class HelloWorld {

    public static void main(String []args) {

    int a = 3, b = 7 ;

    System.out.println("Hello World!");

    }

    public static int f(int a, int b){

    return a*a + a*b + b*b;

    }

    }

  • 结果如下:

Ⅲ 编写一个java小程序!

publictest_03(){
init();
}
publicvoidinit(){
JLabeljl_1=newJLabel("用户名");
JLabeljl_2=newJLabel("密码");
JTextFieldjtf=newJTextField();
JPasswordFieldjpf=newJPasswordField();
JButtonbutton_1=newJButton("确定");
JPanelpanel=newJPanel();
panel.setLayout(null);
jl_1.setBounds(30,50,50,30);
jl_2.setBounds(30,90,50,30);
jtf.setBounds(100,50,100,30);
jpf.setBounds(100,90,100,30);
button_1.setBounds(110,130,80,30);
panel.add(jl_1);
panel.add(jl_2);
panel.add(jtf);
panel.add(jpf);
panel.add(button_1);
this.add(panel);
this.setSize(300,250);
this.setLocation(400,300);
this.setVisible(true);
}
publicstaticvoidmain(String[]args){
newtest_03();
}