2
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;




public class Library {
JFrame frame = new JFrame("Library Management - MENU");
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
JButton button7 = new JButton();

/**
 */
public Library()
{



        JPanel panel = new JPanel();

        panel.setLayout(new FlowLayout());



        JLabel label = new JLabel("MENU");

        panel.add(label);



        button1.setText("ISSUE a BOOK");
            button1.setBounds(100,100,200,30);
        panel.add(button1);







        button2.setText("RETURN a BOOK");
            button2.setBounds(200,200,200,30);
            panel.add(button2);



        button3.setText("UPDATE/SEARCH RECORD");
            button3.setBounds(300,300,200,30);
            panel.add(button3);



        frame.add(panel);
            button1.addActionListener((ActionEvent e) -> {
                frame.setTitle("ISSUE");

                JPanel panel1 = new JPanel();

                panel1.setLayout(new FlowLayout());

                button6.setText("ISSUE a BOOK on CARD1");
                button6.setBounds(100,100,200,30);
                panel1.add(button6);

                button7.setText("ISSUE a BOOK on CARD2");
                button7.setBounds(100,100,200,30);
                panel1.add(button7);
                frame.add(panel1);

                frame.setVisible(true);
            });
            button2.addActionListener((ActionEvent e) -> {
                frame.setTitle("RETRUN");

                JPanel panel1 = new JPanel();

                panel1.setLayout(new FlowLayout());

                button4.setText("RETURN a BOOK on CARD1");
                button4.setBounds(100,100,200,30);
                panel1.add(button4);

                button5.setText("RETURN a BOOK on CARD2");
                button5.setBounds(100,100,200,30);
                panel1.add(button5);
                frame.add(panel1);

                frame.setVisible(true);
            });
        frame.setSize(500,500);

        frame.setLocationRelativeTo(null);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        frame.setVisible(true);
  }



 public static void main(String[] args) {


    Library obj=new Library();
 }




}

I am creating a library management app and i have created multiple jpanels in a frame but when i move from panel to another it fluctuates and a previously used buttons overlap current buttons. And even buttons are not moving at proper postions even after changing the setBounds parameters.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
diwinger
  • 49
  • 1
  • 11
  • 1
    1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 3) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Apr 15 '15 at 10:36
  • 1
    Setting components' bounds is the job of the layout manager. Don't call `setBounds()` unless you're implementing one. – kiheru Apr 15 '15 at 10:37
  • could you just edit the code and show how to do it? @Andrew – diwinger Apr 15 '15 at 10:54

1 Answers1

3

Try using CardLayout, here is your code with the card layout being used... Note: You're missing some actions on your buttons to return to the MAIN screen, I'll leave that to you! :-)

import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Library {

    JFrame frame = new JFrame("Library Management - MENU");
    JButton button1 = new JButton();
    JButton button2 = new JButton();
    JButton button3 = new JButton();
    JButton button4 = new JButton();
    JButton button5 = new JButton();
    JButton button6 = new JButton();
    JButton button7 = new JButton();

    public Library() {

        JPanel cards = new JPanel(new CardLayout());

        JPanel firstPanel = new JPanel();
        JPanel secondPanel = new JPanel();
        JPanel thirdPanel = new JPanel();

        //Init some components...
        JLabel label = new JLabel("MENU");
        button1.setText("ISSUE a BOOK");
        button2.setText("RETURN a BOOK");
        button3.setText("UPDATE/SEARCH RECORD");
        button4.setText("RETURN a BOOK on CARD1");
        button5.setText("RETURN a BOOK on CARD2");
        button6.setText("ISSUE a BOOK on CARD1");
        button7.setText("ISSUE a BOOK on CARD2");

        //First panel setup
        firstPanel.setLayout(new FlowLayout());
        firstPanel.add(label);
        firstPanel.add(button1);
        firstPanel.add(button2);
        firstPanel.add(button3);

        //Second panel setup
        secondPanel.setLayout(new FlowLayout());
        secondPanel.add(button6);
        secondPanel.add(button7);

        //Third panel setup
        thirdPanel.setLayout(new FlowLayout());
        thirdPanel.add(button4);
        thirdPanel.add(button5);

        //Show ISSUE on click of button1 
        button1.addActionListener((ActionEvent e) -> {
            //Change cards to ISSUE panel
            frame.setTitle("ISSUE");
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, "ISSUE");
        });

        //Show RETURN on click of button2
        button2.addActionListener((ActionEvent e) -> {
            frame.setTitle("RETRUN");
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, "RETRUN");

        });

        //Add content to cardlayout JPanel
        cards.add(firstPanel, "MENU");
        cards.add(secondPanel, "ISSUE");
        cards.add(thirdPanel, "RETURN");

        frame.add(cards);

        //Initial card to show...
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, "MENU");

        //Frame constraints
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        Library obj = new Library();
    }

}
Gready
  • 1,134
  • 8
  • 15