0

Trying to make flashcard application. The JButton b_switchmode will be used to switch between panels (an edit mode and a test mode, both have panels created for separate use). Not quite sure how to go from the GUI I've created. Ideally I would click the JButton b_switchmode and I would be able to switch from "Edit Mode" to "Test Mode".

I'm aware that a CardLayout may be needed but I'm unable to implement the correct syntax to make the program executable.

So I want to know how to:

  • insert CardLayout to be able to switch between JPanels

  • allow JButton b_switchmode to execute the switch between JPanels


public class App2 {
    private static JFrame frame;
    private static JPanel editpanel;
    private static JPanel testpanel; 
    private static JLabel l_appmode;
    private static JLabel l_number;
    private static JLabel l_question;
    private static JLabel l_answer;
    public  static JTextField t_questions;
    public  static JTextField t_answer;
    public  static JButton    b_switchmode;
    public  static JButton    b_previous;
    public  static JButton    b_next;
    public  static JButton    b_add;
    public  static JButton    b_save;
    public  static JButton    b_question;
    public  static JButton    b_answer;
    static int width  = 600;
    static int height = 450;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    gui();
}

private static void gui(){
  frame = new JFrame("Assignment2");
  frame.setTitle("Flash Cards");
  frame.setSize(width,height);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  l_appmode  = new JLabel("Current Mode: Edit");  
  l_number = new JLabel ("");
  l_question  = new JLabel("Question:");
  l_answer    = new JLabel("Answer:");

  t_questions = new JTextField("");
  t_answer    = new JTextField("");

  b_switchmode = new JButton("Switch Mode");
  b_previous = new JButton("Previous");
  b_next = new JButton("Next");
  b_add = new JButton("Add");
  b_save = new JButton("Save");
  b_question = new JButton("Question");
  b_answer = new JButton("Answer");


  editpanel = new JPanel();
  int rows = 6, columns = 1;
  int hgap = 20; int vgap = 20;
  editpanel.setLayout(new GridLayout(rows, columns, hgap, 
  vgap));

  editpanel.add(l_appmode);
  editpanel.add(l_number);

  editpanel.add(l_question);
  editpanel.add(t_questions);

  editpanel.add(l_answer);
  editpanel.add(t_answer);

  editpanel.add(b_switchmode);
  editpanel.add(b_previous);
  editpanel.add(b_next);
  editpanel.add(b_add);
  editpanel.add(b_save);

  //testpanel.add(b_question);
  //testpanel.add(t_questions);
  //testpanel.add(b_answer);
  //testpanel.add(t_answer);

  frame.add(editpanel);
  //frame.add(testpanel);
  frame.setVisible(true);
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • You do not have any actions associated with your buttons ? – Sanjeev Apr 29 '19 at 16:07
  • Not yet, I'm still trying to figure out how to set up the layout. – javanoob0304 Apr 29 '19 at 16:10
  • For [example](https://stackoverflow.com/questions/49517392/how-to-view-another-jpanel-containing-many-sub-jpanels-from-same-jframe-in-netbe/49519023#49519023), [example](https://stackoverflow.com/questions/54042979/how-do-i-draw-on-a-jpanel-from-multiple-outside-classes/54043710#54043710), [example](https://stackoverflow.com/questions/34633999/how-to-add-action-to-a-button/34637817#34637817). – Frakcool Apr 29 '19 at 16:43
  • If you only want to implement `CardLayout` then create a [mcve] that shows that, a single label is enough. And please follow [Java naming conventions](https://www.oracle.com/technetwork/java/codeconventions-135099.html) (i.e. use `camelCase` for variable naming instead of `snake_case` and please indent your code correctly – Frakcool Apr 29 '19 at 16:43
  • Possible duplicate of [How to add action to a button?](https://stackoverflow.com/questions/34633999/how-to-add-action-to-a-button) – Frakcool Apr 29 '19 at 16:45
  • *I'm aware that a CardLayout may be needed* - yes that is the proper solution. *but I'm unable to implement the correct syntax to make the program executable.* - The current design of your class is completely wrong. You should NOT be using static variables and methods. I suggest you start from a working example for a better design. Read the section from the Swing tutorial on [How to Use CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). Download the working example and make changes one at a time to add more of your required functionality. – camickr Apr 29 '19 at 17:21

0 Answers0