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.BorderLayoutimport 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)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)