-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightsPanel.java
More file actions
80 lines (69 loc) · 1.98 KB
/
Copy pathLightsPanel.java
File metadata and controls
80 lines (69 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
public class LightsPanel extends JPanel{
private JButton brighten, darken;
int val;
private JLabel currentVal;
Scanner infile = null;
public LightsPanel(){
try{
infile = new Scanner(new File("lights"));
}catch(FileNotFoundException e){
System.err.println("File not found!");
System.exit(0);
}
val = Integer.parseInt(infile.next());
brighten = new JButton("Brighten the Lights 10%");
darken = new JButton("Darken the Lights 10%");
setLayout(new FlowLayout());
JPanel subpanel = new JPanel();
subpanel.setLayout(new GridLayout(1,3));
JLabel m = new JLabel("Lights: ");
Font f = new Font("Calibri", Font.BOLD, 18);
m.setFont((f));
m.setForeground(Color.RED);
add(m);
JPanel buttons = new JPanel(new GridLayout(2,1));
buttons.add(brighten);
buttons.add(darken);
add(buttons);
currentVal = new JLabel(""+val);
currentVal.setFont(f);
add(currentVal);
brighten.addActionListener(new Listener(true));
darken.addActionListener(new Listener(false));
}
public class Listener implements ActionListener {
boolean b;
public Listener(boolean b){
b=b;
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(b){
if(val==100){
return;
}else{
val+=10;
}
}else{
if(val==0){
return;
}else{
val-=10;
}
}
try{
System.setOut(new PrintStream(new FileOutputStream("lights")));
}catch(FileNotFoundException e){
System.err.println("File not found!");
System.exit(0);
}
System.out.println(val);
currentVal.setText(""+val);
}
}
}