1+ import java .awt .event .ActionEvent ;
2+ import java .awt .event .ActionListener ;
3+ import javax .swing .JButton ;
4+ import javax .swing .JCheckBox ;
5+ import javax .swing .JFrame ;
6+ import javax .swing .JLabel ;
7+
8+ public class UserInterface extends JFrame {
9+ private class NextWordListener implements ActionListener {
10+ @ Override
11+ public void actionPerformed (ActionEvent e ) {
12+ String [] rateWoerter = Main .getRateWoerter ();
13+ if (Main .wordIndex < rateWoerter .length - 1 ) {
14+ Main .wordIndex += 1 ;
15+ Main .rateWort = rateWoerter [Main .wordIndex ];
16+ output .setText ("Bitte erkl\u00e4 re: " + Main .rateWort );
17+ helpOutput .setText ("" );
18+ helpIndex = 0 ;
19+ help .setEnabled (true );
20+ startTime = System .nanoTime ();
21+ if (Main .rateWort == rateWoerter [rateWoerter .length - 1 ]) {
22+ helpOutput .setVisible (false );
23+ timerOnOff .setEnabled (false );
24+ timerOnOff .setSelected (false );
25+ time .setVisible (false );
26+ nextWord .setEnabled (false );
27+ help .setEnabled (false );
28+ output .setText (Main .rateWort );
29+ }
30+ }
31+ }
32+ }
33+
34+ private class HelpListener implements ActionListener {
35+ @ Override
36+ public void actionPerformed (ActionEvent e ) {
37+ String [] helpWords = PythonBackground .hilfsWoerter (Main .rateWort );
38+ if (helpIndex < helpWords .length ) {
39+ String helpWord = helpWords [helpIndex ];
40+ helpOutput .setText ("Tipp: " + helpWord );
41+ helpIndex ++;
42+ } else {
43+ help .setEnabled (false );
44+ helpOutput .setText ("Kein Tipp vorhanden!" );
45+ }
46+ }
47+ }
48+
49+ private static final long serialVersionUID = 541391875104762428L ;
50+ private static JButton nextWord ;
51+ private static JButton help ;
52+ private static JLabel output ;
53+ private static JLabel helpOutput ;
54+ private static JLabel time ;
55+ private static JCheckBox timerOnOff ;
56+ private static int helpIndex = 0 ;
57+ private static long startTime ;
58+
59+ public UserInterface () {
60+ getContentPane ().setLayout (null );
61+ setupGUI ();
62+ setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
63+ startTime = System .nanoTime ();
64+ }
65+
66+ private void setupGUI () {
67+ nextWord = new JButton ();
68+ nextWord .setLocation (50 , 300 );
69+ nextWord .setSize (150 , 50 );
70+ nextWord .setText ("N\u00e4 chstes Wort" );
71+ nextWord .setToolTipText ("Zeigt das n\u00e4 chste Wort an!" );
72+ nextWord .addActionListener (new NextWordListener ());
73+ getContentPane ().add (nextWord );
74+
75+ help = new JButton ();
76+ help .setLocation (245 , 300 );
77+ help .setSize (100 , 50 );
78+ help .setText ("Hilfe" );
79+ help .setToolTipText ("Zeigt einen Tipp an!" );
80+ help .addActionListener (new HelpListener ());
81+ getContentPane ().add (help );
82+
83+ output = new JLabel ();
84+ output .setLocation (90 , 150 );
85+ output .setSize (200 , 50 );
86+ output .setText ("Bitte erkl\u00e4 re: " + Main .rateWort );
87+ getContentPane ().add (output );
88+
89+ helpOutput = new JLabel ();
90+ helpOutput .setLocation (90 , 205 );
91+ helpOutput .setSize (200 , 50 );
92+ helpOutput .setText ("" );
93+ getContentPane ().add (helpOutput );
94+
95+ time = new JLabel ();
96+ time .setLocation (90 , 80 );
97+ time .setSize (200 , 50 );
98+ time .setVisible (false );
99+ time .setText ("Ben\u00f6 tigte Zeit: " );
100+ getContentPane ().add (time );
101+
102+ timerOnOff = new JCheckBox ();
103+ timerOnOff .setLocation (90 , 25 );
104+ timerOnOff .setSize (200 , 50 );
105+ timerOnOff .setText ("Ben\u00f6 tigte Zeit anzeigen?" );
106+ timerOnOff .setSelected (false );
107+ getContentPane ().add (timerOnOff );
108+
109+ setTitle ("Informatrix Random Word Generator" );
110+ setSize (400 , 400 );
111+ setVisible (true );
112+ setResizable (false );
113+ }
114+
115+ public static void switchTimerState () {
116+ try {
117+ Thread .sleep (1L );
118+ } catch (Exception exception ) {}
119+
120+ if (timerOnOff .isSelected ()) {
121+ time .setVisible (true );
122+ } else {
123+ time .setVisible (false );
124+ }
125+ }
126+
127+ public static void timer () {
128+ String timeStr = String .valueOf ((System .nanoTime () - startTime ) * 0.000000001 );
129+ time .setText (timeStr .substring (0 , timeStr .indexOf ("." , 0 )) + " Sekunden" );
130+ }
131+ }
0 commit comments