PDA

View Full Version : سوال: نمایش تاریخ و ساعت



maJJJid3065
پنج شنبه 04 شهریور 1389, 17:42 عصر
با سلام.طاعات و عباداتتون قبول دوستان.یه برنامه Application تو NetBean نوشتم.حالا میخام توی فرمم توی دو تا لیبل ساعت و تاریخ هم نمایش داده بشه.کد هاشو میخام.لطفا توضیح هم بدین.ممنون.

javaphantom
پنج شنبه 04 شهریور 1389, 19:36 عصر
بیکار بودم حال کردم جوابتو بدم


import javax.swing.*;
import java.text.SimpleDateFormat;

/**
* Created by IntelliJ IDEA.
* User: zack
* Date: Aug 26, 2010
* Time: 7:45:35 PM
* To change this template use File | Settings | File Templates.
*/
public class MyClock implements Runnable {

private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
private String clock;
private JLabel jLabel;

public MyClock(JLabel jLabel) {
this.jLabel = jLabel;
}

public void run() {
try {
while (true) {

this.jLabel.setText(this.getClock());
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

}

private String getClock() throws Exception {
this.clock = this.simpleDateFormat.format(System.currentTimeMil lis());
return this.clock;
}
}




import javax.swing.*;
import java.awt.*;

/**
* Created by IntelliJ IDEA.
* User: zack
* Date: Aug 26, 2010
* Time: 7:56:14 PM
* To change this template use File | Settings | File Templates.
*/
public class MyForm extends JFrame {


private JPanel jPanel1;
private JLabel jLabel1;


public MyForm() {
this.setBounds(new Rectangle(200,200));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
this.getContentPane().add(this.getjPanel1());
this.startThread();
this.setVisible(true);
}

private void startThread() {
Thread t = new Thread(new MyClock(this.getJLabel1()));
t.start();
}


public JPanel getjPanel1() {
if (this.jPanel1 == null) {
this.jPanel1 = new JPanel();
this.jPanel1.setLayout(new FlowLayout());
this.jPanel1.add(this.getJLabel1());
}
return jPanel1;
}

public JLabel getJLabel1() {
if (this.jLabel1 == null) {
this.jLabel1 = new JLabel();
}
return jLabel1;
}


public static void main(String[] s) {
new MyForm();
}
}

tanha70
جمعه 12 شهریور 1389, 09:24 صبح
سلام دوستان
من میخوام یه برنامه بنویسم که مثلا عدد 6 رو بگیره و تاریخ دقیق 6 ماه دیگه رو بگه.