怎么用java实现电脑里的时间窗口? 谢谢?

怎么用java实现电脑里的时间窗口? 谢谢?,第1张

public class MainFrame extends JFrame implements Runnable {

JPanel contentPane

JLabel time = new JLabel()

JOptionPane message = new JOptionPane()

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

public MainFrame() {

try {

setDefaultCloseOperation(EXIT_ON_CLOSE)

jbInit()

} catch (Exception exception) {

exception.printStackTrace()

}

}

/**

* Component initialization.

*

* @throws java.lang.Exception

*/

private void jbInit() throws Exception {

contentPane = (JPanel) getContentPane()

contentPane.setLayout(null)

setSize(new Dimension(365,130))

setTitle("动态时间显示")

this.addWindowListener(new MainFrame_this_windowAdapter(this))

time.setFont(new java.awt.Font("新宋体", Font.PLAIN, 26))

time.setForeground(Color.red)

time.setRequestFocusEnabled(false)

time.setBounds(new Rectangle(47, 26, 284, 40))

this.setResizable(false)

Thread th = new Thread(this)

th.start()

contentPane.add(time)

}

public void run() {

while (true) {

time.setText(format.format(new Date()))

try {

Thread.sleep(1000)

} catch (InterruptedException ex) {

ex.printStackTrace()

}

}

}

public void this_windowClosing(WindowEvent e) {

int i = message.showConfirmDialog(this,"你真的要退出吗?","退出",JOptionPane.INFORMATION_MESSAGE)

if(i == 1){

System.exit(0)

}else{

e.getOldState()

}

}

}

import java.awt.BorderLayout

import java.awt.Font

import java.awt.Graphics

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.util.Calendar

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JPanel

public class Cat extends JFrame implements Runnable

{

private static final long serialVersionUID = 1L

Thread thread

public Cat()

{

setSize(200, 100)

setLayout(new BorderLayout())

setResizable(false)

setLocationRelativeTo(null)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

start()

}

private Cat addComponents()

{

JPanel panel = new JPanel()

JButton run = new JButton("Start")

JButton wait = new JButton("Stop")

run.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

start()

}

})

wait.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

stop()

}

})

panel.add(run)

panel.add(wait)

add(panel, BorderLayout.SOUTH)

return this

}

@Override

public void paint(Graphics g)

{

super.paint(g)

Calendar calendar = Calendar.getInstance()

String h = calendar.get(Calendar.HOUR_OF_DAY) + ""

h = h.matches("^\\d$") ? "0" + h : h

String m = calendar.get(Calendar.MINUTE) + ""

m = m.matches("^\\d$") ? "0" + m : m

String s = calendar.get(Calendar.SECOND) + ""

s = s.matches("^\\d$") ? "0" + s : s

String str = h + ":" + m + ":" + s

g.setFont(new Font("normal", Font.BOLD, 22))

g.drawString(str, getWidth() / 2 - 35, getHeight() / 2)

g.dispose()

}

public void start()

{

if(null == thread)

{

thread = new Thread(this)

thread.setPriority(Thread.MIN_PRIORITY)

thread.start()

}

}

public synchronized void stop()

{

if(null != thread)

{

thread.interrupt()

thread = null

notifyAll()

}

}

@Override

public void run()

{

Thread me = Thread.currentThread()

while(me == thread)

{

try

{

Thread.sleep(1000)

}

catch(InterruptedException e)

{}

repaint()

}

}

public static void main(String[] args)

{

new Cat().addComponents().setVisible(true)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8048430.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存