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;
}
}