Okay, I decided to give up on the above pregenerated code. I learned a little swing, and made a new code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class diceGUI {
public static void main(String[] args) {
JFrame frame = new JFrame("Virtual Dice");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box f = Box.createVerticalBox();
Random generator = new Random(); //initiates the random generator
//titleLabel
JLabel titleLabel = new JLabel("<html><big>Virtual Dice</big></html>");
f.add(titleLabel);
f.add(Box.createVerticalStrut(25));
//resultLabel
JLabel resultLabel = new JLabel("Roll: --");
f.add(resultLabel);
f.add(Box.createVerticalStrut(25));
// rollButton
JButton rollButton = new JButton("Roll!");
rollButton.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent event) {
int diceThrow = generator.nextInt(6) + 1; \\javac says: "local variable generator is accessed from within inner class. Needs to be declared final."
resultLabel.setText("Roll: " +diceThrow); \\javac says: ""local variable resultLabel is accessed from within inner class. Needs to be declared final."
}
});
f.add(rollButton);
frame.add(f, BorderLayout.CENTER);
frame.setSize(150,200);
frame.setVisible(true);
}
}
My problem,as I noted in the code as comments, is that I need to declare local variables as final. So a few questions arise:
What is a local variable?
what does it mean to declare a variable as final.
Why is it necessary to declare it as final?
Thanks, and peace to all!
Byron