Reputation: 7066
I am designing a countdown timer, and found one here. This one works great...you enter the number of seconds, and it automatically counts down for you. I modifying the code to listen for a radio button selection to set the start time. The countdown would start when the "start" button is clicked. I've got the layout worked out, but am having trouble placing the ButtonListener in the code below.
I'm JUST starting to work with nested classes, so some of the concepts are kinda fuzzy for me. Below, if I understand correctly, the ActionEvent
is the countdown, which is INSIDE the ActionListener
classk, which captures the button click. The countdown is dependent upon and cannot run independently of the button click, and all variables in the Listener
class ALSO available to the Event
class.
So I'm thinking that if I want to add a Listener
and Event
for the radio buttons, those the Listener
would encapsulate the Action
, which would encapsulate the code below. I've marked the class declaration for where I think they should go, but not the closing parens. I need to do some closer analysis to figure out how those are working. Is that a correct way to do things? Or is there a better way? If I declare my startTime variable as public in the radiobuttion event
, it should follow through to the other nested classes, right?
Am I understanding all of this correctly? or am I missing some important points. This is my first project with inner classes, so please be gentle. :)
public Test() {
super("Countdown timer");
final String startTime = "10"
text = new JTextField(startTime, 18);
start = new JButton("Start");
pr = new JRadioButton("30");
sr = new JRadioButton("15");
lr = new JRadioButton("5");
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(pr);
radioPanel.add(sr);
radioPanel.add(lr);
//new button action listener here {
// new button action event here {
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent click) {
final long current = System.currentTimeMillis();
try {
final long limit = Integer.parseInt(text.getText().trim())* 1000; // X seconds
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent event) {
long time = System.currentTimeMillis();
long passed = time - current;
long remaining = limit - passed;
if(remaining <= 0) {
text.setText(startTime);
timer.stop();
} else {
long seconds = remaining/1000;
long minutes = seconds/60;
long hours = minutes/60;
text.setText(String.format("%02d:%02d:%02d remaining", hours, minutes, seconds%60));
}
}
});
timer.start();
} catch(NumberFormatException nfe) {
// debug/report here
nfe.printStackTrace();
}
}});
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(radioPanel);
panel.add(text);
panel.add(start, BorderLayout.CENTER );
add(panel);
}
Upvotes: 1
Views: 177
Reputation: 131515
Your code does not define any inner (= nested) classes in the typical sense of the word. You're using Anonymous (inner) Classes. You might be interested in this SO question:
How are Anonymous (inner) classes used in Java?
For information on bona-fide inner (=nested) classes, try the relevant part of Oracle's Java tutorials. Note there are two kinds of inner classes, static and non-static, discussed in the following question here on SO:
Java inner class and static nested class
Upvotes: 1