0

I got stuck in my code, making a game GUI in Java.

I have two packages gameCore and view. GameCore consists of "main" in which I constructed JFrame object that is imported from view package.

In the view package, I made MainFrame and ControlView classes.

In a constructor of MainFrame, it will call "setControlView" method and then will add "getControlView().getControlPanel()" on the mainFrame.

So finally buttons that I want to link to ActionListener are inside "controlPanel"

I tried many locations to implement ActionListener interface, but I still can't solve the problem.

Should I make implementation on the GameCore main class? or anywhere?

package view;

import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame {

    private MainFrame console;
    private ControlView controlView;
    private PlayView playView;

    public MainFrame(String title) throws HeadlessException {

        super(title);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(350, 350);
        this.setControlView();
        this.add(getControlView().getControlPanel());
        this.setVisible(true);

    }

    public void setControlView() {
        this.controlView = new ControlView();
    }

    public void setPlayView() {
        this.playView = new PlayView();
    }

    public MainFrame getConsole() {
        return console;
    }

    public ControlView getControlView() {
        return controlView;
    }

}

package view;

import javax.swing.*;
import java.awt.*;

/**
 * Created by hyunjung on 09/02/2017.
 */
public class ControlView extends JPanel {

    private JPanel controlPanel;
    private JLabel titleLabel;
    private JLabel creditLabel;
    private JPanel btnPanel;

    private JButton playBtn;
    private JButton recordBtn;


    public ControlView () {

        controlPanel = new JPanel();
        controlPanel.setBackground(new Color(143, 225, 81));
        controlPanel.setLayout(new GridLayout(3, 1));

        titleLabel = new JLabel("BLACK JACK 2017", JLabel.CENTER);
        titleLabel.setFont(new Font("serif", Font.BOLD, 20));

        creditLabel = new JLabel("@pretty_much games", JLabel.CENTER);
        creditLabel.setFont(new Font("serif", Font.PLAIN, 14));

        btnPanel = new JPanel( new GridBagLayout());
        btnPanel.setBackground(new Color(143, 225, 81));
        playBtn = new JButton("Play");
        recordBtn = new JButton("Record");

        btnPanel.add(playBtn);
        btnPanel.add(recordBtn);

        controlPanel.add(titleLabel);
        controlPanel.add(btnPanel);
        controlPanel.add(creditLabel);
    }

    public JPanel getControlPanel() {
        return controlPanel;
    }

    public JButton getPlayBtn() {
        return playBtn;
    }

    public JButton getRecordBtn() {
        return recordBtn;
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Hyun Jung
  • 69
  • 2
  • 10
  • This seems to be an opinion based question... Do it wherever it "makes sense" in terms of available variables and functions that you want to do "onClick" – OneCricketeer Feb 09 '17 at 09:02
  • Personally, the `ActionListener` should be constrained to the container of the buttons, the reason for this is outside observers don't care that a button was pressed on that some action was triggered. You could have a look [at this example](http://stackoverflow.com/questions/26517856/java-and-gui-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274) for more details and for ideas about how you migth handle it – MadProgrammer Feb 09 '17 at 09:16

1 Answers1

0

here you have an example:

playBtn.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub  
    }

});

this has to be implemented after initializing the button. so it makes sense in this code block:

playBtn = new JButton("Play");
recordBtn = new JButton("Record");

btnPanel.add(playBtn);
btnPanel.add(recordBtn);
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • For example, when "Play" clicked, I want to switch the controlPanel to anotherPanel. – Hyun Jung Feb 09 '17 at 09:10
  • 1
    the implementation is up to you. that is also not a point of your question. to get help for that, try something on your own and ask a new question – XtremeBaumer Feb 09 '17 at 09:12
  • @HyunJung Consider that when the button triggers and action, that you have another listener that tells the controller that some action has occurred, you've divorced the physical action (clicking a button) from the concept of needing to do something (move to a new view), this way, it doesn't matter how the action is triggered, only that when it does, the controller is notified. Exposing the `ActionListener` to the controller is not a good idea, as it exposes ascpets of the implementation that it doesn't need to know about – MadProgrammer Feb 09 '17 at 09:20