public class Wecker extends JFrame {
private static final long serialVersionUID = 1L;
public static int minClicks = 0;
public static Timer timer;
private JButton plus, minus, start, reset;
JLabel zeit;
public Wecker() {
bauen();
addButton();
methoden();
}
public void bauen() {
ImageIcon imageIcon = new ImageIcon("d:/eclipse/workspace/Timer/src/TimerO/timer.png");
this.setIconImage(imageIcon.getImage());
this.setSize(280, 230);
this.setLocationRelativeTo(null);
this.setTitle("It's teatime 1.0");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setAlwaysOnTop(false);
this.setFocusable(true);
this.requestFocus();
plus = new JButton("+");
plus.setFont(new Font("Verdana", Font.BOLD, 18));
plus.addActionListener(new plusAction());
minus = new JButton("-");
minus.setFont(new Font("Verdana", Font.BOLD, 24));
minus.addActionListener(new minusAction());
start = new JButton("Start");
start.setFont(new Font("Verdana", Font.BOLD, 14));
start.addActionListener(new startAction());
reset = new JButton("Reset");
reset.setFont(new Font("Verdana", Font.BOLD, 14));
reset.addActionListener(new resetAction());
zeit = new JLabel("0");
zeit.setHorizontalAlignment(JLabel.CENTER);
zeit.setFont(new Font("Verdana", Font.PLAIN, 42));
zeit.setOpaque(true);
zeit.setBackground(null);
}
public void addButton() {
getContentPane().add(start, BorderLayout.PAGE_START);
getContentPane().add(plus, BorderLayout.LINE_START);
getContentPane().add(minus, BorderLayout.LINE_END);
getContentPane().add(reset, BorderLayout.PAGE_END);
getContentPane().add(zeit, BorderLayout.CENTER);
}
private class plusAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
minClicks += 60;
zeit.setText("" + minClicks);
if (minClicks > 0) {
start.setBackground(Color.green);
}
}
};
private class minusAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (minClicks >= 1) {
minClicks -= 60;
}
if (minClicks == 0) {
start.setBackground(null);
}
zeit.setText("" + minClicks);
}
};
private class resetAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
Wecker.timer.stop();
minus.setEnabled(true);
minClicks = 0;
start.setBackground(null);
zeit.setText("" + minClicks);
}
};
private class startAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (minClicks > 0) {
timer.start();
minus.setEnabled(false);
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
};
public void methoden() {
// Tastatursteuerung
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ADD)
minClicks += 60;
zeit.setText("" + minClicks);
if (minClicks > 0) {
start.setBackground(Color.green);
}
if (e.getKeyCode() == KeyEvent.VK_SUBTRACT)
if (minClicks >= 1) {
minClicks -= 60;
}
if (minClicks == 0) {
start.setBackground(null);
}
if (e.getKeyCode() == KeyEvent.VK_ENTER)
if (minClicks > 0) {
timer.start();
minus.setEnabled(false);
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
});
Wecker.timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Wecker.minClicks--;
zeit.setText("" + minClicks);
if (minClicks == 10) { // Fenster ist die letzten 10
setAlwaysOnTop(true); // sec. immer im Vordergrund
}
if (minClicks == 5) {
java.awt.Toolkit.getDefaultToolkit().beep();
}
if (minClicks == 4) {
java.awt.Toolkit.getDefaultToolkit().beep();
}
if (minClicks == 3) {
java.awt.Toolkit.getDefaultToolkit().beep();
}
if (minClicks == 2) {
java.awt.Toolkit.getDefaultToolkit().beep();
}
if (minClicks == 1) {
java.awt.Toolkit.getDefaultToolkit().beep();
}
if (minClicks == 0) {
Wecker.timer.stop();
start.setBackground(null);
minus.setEnabled(true);
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
});
}
}
public class WeckerProgramm {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new Wecker();
frame.setVisible(true);
}
}