HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Simple Java Game including Thread

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simplejavagamethreadincluding

Problem

I am a Java beginner and have made a simple Compare Game. It has done well but I am not sure about my solution, especially about my thread. Can you give some advice on making it better?

public class CompareGame {
    static short first=0;
    static short second=0;
    static JLabel lblFirst = new JLabel("First");
    static JLabel lblSecond = new JLabel("Second");
    private JFrame frame;
    static String math = null;
    JButton btnReset = new JButton("Reset");
    JButton btnGo = new JButton("Go");

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CompareGame window = new CompareGame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


Main frame:

```
public CompareGame() {
initialize();
}

private void initialize() {
frame = new JFrame("CompareGame");
frame.setBounds(100, 100, 432, 279);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel lblWelcome = new JLabel("Welcome");
lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
lblWelcome.setBounds(161, 30, 81, 14);
frame.getContentPane().add(lblWelcome);

lblFirst.setBounds(88, 75, 46, 14);
frame.getContentPane().add(lblFirst);

JLabel lblMath = new JLabel("Math");
lblMath.setHorizontalAlignment(SwingConstants.CENTER);
lblMath.setBounds(178, 75, 46, 14);
frame.getContentPane().add(lblMath);

lblSecond.setBounds(285, 75, 46, 14);
frame.getContentPane().add(lblSecond);

JButton btnLeft = new JButton("");
btnRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {lblMath.setText(">");
}

Solution

-
Your code formatting looks broken(indentation is inconsistent, opening/closing curly brackets are located in strange places, multiple statements in one line, etc).
It makes your code very hard to read.

-
Operators should be surrounded by whitspaces. For instance,

long future = time + 2 * 1000;


is more readable than

long future=time+2*1000;


-
Variables and methods names are not self-descriptive in your code. For example, a name math() for a method is bad: it does not reflect what this method actually does. You could call it generateNumbers() or something like that. The same goes for variables: for instance, math is pretty a strange name for a variable(and again, it does show the meaning of this variable). Moreover, there is no point in mangling words: buttonLeft is a better name than btnLeft, labelFirst or firstLabel is more comprehensive than lblFirst and so on.

-
Creating your own class that works like a timer(Countdown) is a bad idea. A timer is already present in Java standard library: java.swing.Timer, so you can use it.

-
You can make you code more concise by using lambda-expressions if you have Java8. For example, you can add a listener to a button like this:

button.addActionListener((ActionEvent e) -> {
    // do something
});

Code Snippets

long future = time + 2 * 1000;
long future=time+2*1000;
button.addActionListener((ActionEvent e) -> {
    // do something
});

Context

StackExchange Code Review Q#75805, answer score: 6

Revisions (0)

No revisions yet.