4
\$\begingroup\$

I'm currently coding for a school project. I'm planning on creating a virtual and simple Launchpad. Here's the first part of my code which treats the graphical interface. I know it's not well organized and that's why I ask for your help and tips!

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

import javax.swing.JPanel;


public class clavier extends JFrame {

private JFrame frame = new JFrame("Clavier");
private JPanel parent = new JPanel(new GridLayout(0, 1));
private JPanel[] panel;
private JButton[][] button;
private static final String[][] key = {
      {"A", "Z", "E", "R", "T", "Y", "U", "I",
        "O", "P"}, {
        "Q", "S", "D", "F", "G", "H", "J", "K", "L", "M"}, {"W", "X", "C", "V", "B",
        "N"},
    };
private JMenuBar menuBar = new JMenuBar();
private JMenu m1 = new JMenu("Affecter les sons");
private JMenu m2 = new JMenu("Relancer");
private JMenu m3 = new JMenu("Aide");
private JButton b1 = new JButton("Play");
private JButton b2 = new JButton("Stop");

public clavier() {
    super("Clavier");
    panel = new JPanel[6];
    for (int ligne = 0; ligne < key.length; ligne++) {
        panel[ligne] = new JPanel();
        button = new JButton[20][20];
        for (int column = 0; column < key[ligne].length; column++) {
            button[ligne][column] = new JButton(key[ligne][column]);
            panel[ligne].add(button[ligne][column]);
        }
        parent.add(panel[ligne]);
    }

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.menuBar.add(m1);
    this.menuBar.add(m2);
    this.menuBar.add(m3);
    this.setJMenuBar(menuBar);
    parent.add(b1);
    parent.add(b2);
    this.setVisible(true);
    add(parent);
    pack();
  }



public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            clavier gui = new clavier();
        }
    });

}
}
\$\endgroup\$
  • \$\begingroup\$ Could you please explain or translate the names used? \$\endgroup\$ – Tamoghna Chowdhury Apr 11 '17 at 7:22
1
\$\begingroup\$

Naming conventions

Classnames start with a capital letter. So it should be public class Clavier.

Consistent English or Frensh. Mixing makes things weird:
ligne => line
Clavier => Keyboard

Give variables meaningful names:
m1 => soundMenu
m2 => restartMenu
m3 => helpMenu
b1 => playButton
b2 => stopButton
button => keyboardButtons / letterButtons
key => keyboardLetters

I'd also rename ligne to row to fit better with the column.

Responsibility

What exactly is a Clavier in your program?
Is it a window with some content?
Is it a main screen that you put inside a window? Is it just the keyboard, or is it a panel that contains a keyboard?

Right now, it's all of those things.

Let's remove all the Frame parts from this class. Some other class will be responsible of putting the keyboard into a frame to show it. Let's also turn the keyboard into a JPanel instead of letting it reference a panel (parent). That way we can just put the Keyboard directly into a frame.

The biggest problem we'll get with this change is that we don't have a frame anymore to ade the menuBar to. So let's also create the new class that creates the frame, adds the main menu bar and a keyboard.

Our 2 new classes look like this:

public class Application {
    private JMenuBar menuBar = new JMenuBar();
    private JMenu soundMenu = new JMenu("Affecter les sons");
    private JMenu restartMenu = new JMenu("Relancer");
    private JMenu helpMenu = new JMenu("Aide");


    public Application() {
        this.menuBar.add(soundMenu);
        this.menuBar.add(restartMenu);
        this.menuBar.add(helpMenu);
    }

    public void run(){
        JFrame frame = new JFrame("Clavier");
        frame.setJMenuBar(menuBar);
        frame.add(new Keyboard());
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws Exception {
        new Application().run();
    }
}

public class Keyboard extends JPanel {

    private JPanel[] keyboardPanel;
    private JButton[][] keyboardButtons;
    private static final String[][] keyboardLetters = {
            {"A", "Z", "E", "R", "T", "Y", "U", "I",
                    "O", "P"}, {
            "Q", "S", "D", "F", "G", "H", "J", "K", "L", "M"}, {"W", "X", "C", "V", "B",
            "N"},
    };

    private JButton playButton = new JButton("Play");
    private JButton stopButton = new JButton("Stop");

    public Keyboard() {
        super(new GridLayout(0, 1));
        keyboardPanel = new JPanel[6];
        for (int row = 0; row < keyboardLetters.length; row++) {
            keyboardPanel[row] = new JPanel();
            keyboardButtons = new JButton[20][20];
            for (int column = 0; column < keyboardLetters[row].length; column++) {
                keyboardButtons[row][column] = new JButton(keyboardLetters[row][column]);
                keyboardPanel[row].add(keyboardButtons[row][column]);
            }
            this.add(keyboardPanel[row]);
        }
        this.add(playButton);
        this.add(stopButton);
        this.setVisible(true);
    }
}

The next steps from here are adding the required listeners to all the buttons and menu items. This is actually the hard part. When doing those you also want to ask yourself: which class is responsible for what? I'd say the keyboard is responsible for deciding what button is pressed. And then to pass it on to some class that knows what to do depending on which button was pressed.

\$\endgroup\$
  • \$\begingroup\$ Hey Imus, Thank you for answering so quick! This school project is made by a group of 3. We divided our work into 3 parts, one is taking care of the function "record", one is focusing on the code which link the keyboard to virtual keyboard (to make a sound) and i'm doing this swing Ui code. Thank you for your answer, it really helped us. \$\endgroup\$ – FERRE Apr 22 '17 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.