0

Could anyone please tell me why nothing happens when i click create button in StartScreen panel? Basically, I have a JFrame, and in that JFrame I have a JPanel that is using CardLayout. I also have two other JPanel which is called StartScreen and CreatePanel. In StartScreen panel, I have two JButton, one of them is called _createBtn, when I click that button, I want the CreatePanel to show up on my _panelContainer that is using CardLayout in my MainFrame class. I just don't understand why it doesn't work, could anyone please show me what I did wrong in my code? Thank you.

Here is my MainFrame class:

package View;

import java.awt.CardLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame{

/**
 * generated serial
 */
private static final long serialVersionUID = 1L;

/**
 * window's width
 */
protected static final int WIDTH = 1200;

/**
 * window's height
 */
protected static final int HEIGHT = 800;

/**
 * panel container
 */
private JPanel _panelContainer = new JPanel();

/**
 * card layout
 */
private CardLayout _cardLayout = new CardLayout();

public MainFrame() {
    setTitle("HighBid");
    setSize(WIDTH,HEIGHT);
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    addComponents();
}

/**
 * Adding components like JPanel here.
 */
private void addComponents() {
    StartScreen start = new StartScreen();
    CreatePanel create = new CreatePanel();
    _panelContainer.setLayout(_cardLayout);
    _panelContainer.add(start, "StartScreen");
    _panelContainer.add(create, "CreatePanel");
    _cardLayout.show(_panelContainer, "StartScreen");
    this.setContentPane(_panelContainer);

}

/**
 * Switching between panels.
 */
public void swapPanel(String next) {
    if("StartScreen".equals(next)) {
        _cardLayout.show(_panelContainer, "StartScreen");
    } else if ("CreatePanel".equals(next)) {
        _cardLayout.show(_panelContainer, "CreatePanel");
    }
}

}

Here is my StartScreen panel:

package View;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
* Start screen JPanel
* 
* @author 
* @version 5/19/2015
*/
public class StartScreen extends JPanel {

/**
 * default serial
 */
private static final long serialVersionUID = 1L;

/**
 * Start screen's label
 */
private JLabel _label;

/**
 * create button
 */
private JButton _createBtn;

/**
 * open button
 */
private JButton _openBtn;


public StartScreen() {
    setSize(MainFrame.WIDTH, MainFrame.HEIGHT);
    setLayout(null);
    setComponents();
    addComponents();
    setListeners();
}

private void setListeners() {

    _createBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            MainFrame mf = new MainFrame();
            JButton src = (JButton) e.getSource();

            if(src.equals(_createBtn)) {
                mf.swapPanel("CreatePanel");
            } else {
                mf.swapPanel("StartScreen");
            }

        }

    });
}

/**
 * Setting for each component in this panel.
 */
private void setComponents() {
    // Set HighBid Label
    _label = new JLabel("Welcome to HighBid");
    _label.setFont(new Font("Tahoma", 0, 70));
    _label.setBounds((int)(MainFrame.WIDTH / 4.5), 100,700,100);
    _label.setForeground(Color.BLUE);

    // Set create button
    _createBtn = new JButton("Create Auction");
    _createBtn.setBounds((int)(MainFrame.WIDTH / 4.5), 350, 270, 100);
    _createBtn.setFont(new Font("Tahoma", 0, 36));

    // Set open button
    _openBtn = new JButton("Open Auction");
    _openBtn.setBounds((int)(MainFrame.WIDTH / 1.9), 350, 270, 100);
    _openBtn.setFont(new Font("Tahoma", 0, 36));

}

/**
 * Add the component to the panel.
 */
private void addComponents() {
    this.add(_label);
    this.add(_createBtn);
    this.add(_openBtn);
}
}

Here is my CreatePanel panel:

package View;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class CreatePanel extends JPanel {

/**
 * Default serial.
 */
private static final long serialVersionUID = 1L;

/**
 * label for create panel's form
 */
private JLabel _createLabel;

/**
 * back button
 */
private JLabel _backBtn;

public CreatePanel() {
    setSize(MainFrame.WIDTH, MainFrame.HEIGHT);
    setLayout(null);
    setListeners();
}

private void setListeners() {

}

}

And here is my main class:

package View;

import java.awt.EventQueue;

public class HighBidMain {

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new MainFrame().setVisible(true);  
        }
    });
}

}
ln206
  • 169
  • 2
  • 6
  • 18

1 Answers1

1

You're creating a new instance of MainFrame in the ActionListener in StartScreen, this instance of the MainFrame has no relationship with the window that is visible to the user.

You should have some kind of controller which is responsible for determining the course of action which should be taken based on the current view and the user input. It would then be responsible for updating the main view.

Each child view would be given the same instance of this controller and would need to communicate with it about what it wanted to do.

For example:

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366