How can I create my imagination?

enter image description here

enter image description here

How can I construct / encode this program stream?

  • I tried to use CardLayout

    it but it doesn't satisfy my needs.
  • I am trying with multiple JPanel

    , when the user clicks a button, I change the setVisible()

    corresponding one JPanel

    . But it doesn't work, or I can't use it.
  • I am planning to use multiple JFframe

    (the left side is always the same), close and open windows according to the user's choice. It seems absurd ...

So can you help me find a solution?

+3


source to share


1 answer


Here's what I did.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.BevelBorder;


public class Imagination extends JFrame {

    private JPanel [] contentPanes ;
    private JPanel buttonsPanel, stuffPanel ;
    private GridLayout layout ;
    private JButton [] consecutiveButtons ;

    Imagination() {

        layout = new GridLayout(0, 2, 0, 1);

        setLayout(new GridLayout(1, 2));

        contentPanes = new JPanel[3];
        for ( int i = 0 ; i < contentPanes.length ; i++ ) {

            contentPanes[i] = new JPanel(layout);
            contentPanes[i].setBorder(new BevelBorder(BevelBorder.RAISED));

        }
        addToAllPanes();

        buttonsPanel = new JPanel(new GridLayout(0, 1, 0, 5));

        consecutiveButtons = new JButton[3];
        for ( int i = 0 ; i < consecutiveButtons.length ; i++) {

            consecutiveButtons[i] = new JButton("Sample Button "+i);
            buttonsPanel.add(consecutiveButtons[i]);
            //We will set the name of the button so that
            //we can change the panels
            consecutiveButtons[i].setName(i+"");
            consecutiveButtons[i].addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    consecutiveButtonActionPerformed(e);

                }

            });

        }

        stuffPanel = new JPanel(new GridLayout(0, 2, 0, 5));
        stuffPanel.add(contentPanes[0]);

        setSize(450, 300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        add(buttonsPanel);
        add(stuffPanel);

        pack();

    }

    public static void main(String [] args) {

        Imagination i = new Imagination();
        i.setVisible(true);

    }

    private void addToAllPanes() {

        for ( int i = 0 ; i <= 10 ; i ++) {

            contentPanes[0].add(new JLabel("A Sample Label"));
            contentPanes[0].add(new JTextField("A Sample Text Field"));
            contentPanes[1].add(new JLabel("Demo Label"));
            contentPanes[1].add(new JTextField("A Sample Text Field"));
            contentPanes[2].add(new JLabel("Stack"));
            contentPanes[2].add(new JTextField("Overflow"));

        }

    }

    private void consecutiveButtonActionPerformed(ActionEvent e) {

        JButton b = (JButton) e.getSource();
        int id = Integer.parseInt(b.getName());
        stuffPanel.removeAll();
        stuffPanel.add(contentPanes[id]);
        stuffPanel.repaint();

    }

}

      



I made three buttons and three panels using arrays and added them to two different panels in the main JFrame. All buttons are added to one panel and a panel containing JTextFields and JTextLabels to another. To control which panels are displayed when a button is clicked, I set the name of the button and add a normal action listener to it. In which I removed all components and added the desired panel. Then I call repaint () to show it at the moment the button is clicked.

I did this as a hint to your answer. If you think you have found your answer, please accept

+1


source







All Articles