Tartalomjegyzék

< Java

Java Swing megoldások

Óra

Ora.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.text.SimpleDateFormat;
 
public class Ora extends JFrame {
	final static long serialVersionUID = 1;
 
	OraFelirat of = new OraFelirat();
	public Ora() {
		of.setLocation(50, 0);		
		this.getContentPane().setBackground(Color.BLACK);
		this.add(of);
		this.setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(370, 150);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new Ora();
	}
}
 
class OraFelirat extends JLabel implements ActionListener {
	final static long serialVersionUID=1;
	SimpleDateFormat sdf;
	public OraFelirat() {
 
		this.setForeground(Color.GREEN);
		this.setSize(350, 120);
		this.setFont(new Font("sans-serif", Font.PLAIN, 60));
		sdf = new SimpleDateFormat("HH:mm:ss");
		Timer t = new Timer(1000, this);
		t.start();
	}
	public void actionPerformed(ActionEvent e) {
		Date d = new Date();
		this.setText(sdf.format(d));
	}
}

Óra dátummal

Ora.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.text.SimpleDateFormat;
 
public class Ora extends JFrame {
	final static long serialVersionUID = 1;
 
	OraFelirat ofi = new OraFelirat("ido");
	OraFelirat ofd = new OraFelirat("datum");
	public Ora() {
		ofi.setLocation(50, 0);	
		ofd.setLocation(120, 70);
 
		this.getContentPane().setBackground(Color.BLACK);
		this.add(ofi);
		this.add(ofd);
		this.setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(370, 150);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new Ora();
	}
}
 
class OraFelirat extends JLabel implements ActionListener {
	final static long serialVersionUID=1;
	SimpleDateFormat sdf;
	public OraFelirat(String tipus) {		
		this.setForeground(Color.GREEN);
 
		if(tipus.equals("ido")) {					
			this.setSize(350, 120);
			this.setFont(new Font("sans-serif", Font.PLAIN, 60));
			sdf = new SimpleDateFormat("HH:mm:ss");
		}else if(tipus.equals("datum")) {
			this.setSize(200, 70);
			this.setFont(new Font("sans-serif", Font.PLAIN, 20));
			sdf = new SimpleDateFormat("yyyy:MM:dd");
		}
		Timer t = new Timer(1000, this);
		t.start();
	}
 
	public void actionPerformed(ActionEvent e) {
		Date d = new Date();
		this.setText(sdf.format(d));
	}
}