Ken M
Ken M

Reputation: 21

Gui trouble with button layout

I'm attempting use flow layout to make a menu screen that orients with a single column, but whenever I add a button, it adds it to a single row.

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Proj 
{
    JPanel card1,card2; 
    ActionListener listener;
    JFrame menu;
    JFrame catagories;
    JButton menu1,addOrTake,cata,payd,showd;
    //JButton 
    public Proj(){


        card2=new JPanel();


        menu = new JFrame("Card Layout");
        catagories = new JFrame();

        //final Container contentPane = menu.getContentPane();
        final CardLayout layout = new CardLayout();
        menu.setLayout(layout);
        card1=new JPanel();

        menu1 = new JButton("");
        menu1.setIcon(new ImageIcon("C:/Users/sabar/Menu.jpg"));
        menu1.setSize(60,600);
        menu1.setVisible(true);

        addOrTake = new JButton();
        addOrTake.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample       Pictures/Penguins.jpg"));

        cata = new JButton("");
        cata.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));

        showd = new JButton("");
        showd.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));

        payd = new JButton("");
        payd.setSize(60, 600);
        payd.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
        payd.setVisible(true);

        card1.add(menu1);
        card1.add(addOrTake);
        card1.add(cata);
        card1.add(showd);
        card1.add(payd);

        menu1.addActionListener(listener);
        addOrTake.addActionListener(listener);
        cata.addActionListener(listener);
        showd.addActionListener(listener);
        payd.addActionListener(listener);

        listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {}};
            // TODO Auto-generated method stub



            menu.setSize(60, 600);
            menu.setVisible(true);
            JButton poo=new JButton("Poo");
            poo.setSize(60,600);
            card2.add((poo));
            card2.add(new JButton("Pee"));
            card2.add(new JButton("Per"));
            card2.add(new JButton("POt"));
            card2.setVisible(false);
            menu.add(card2);

            menu.add(card1);
            catagories.pack();
            menu.pack();
            card1.setVisible(false);


    }
    public static void main(String[]args)
    {
        Proj poop =new Proj();
    }
}

Upvotes: 0

Views: 39

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347334

Start by using a layout manager which is capable of achiving your results.

Start by taking a look at Laying Out Components Within a Container, How to Use GridLayout and How to Use GridBagLayout for some ideas.

Layout

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Proj {

    JPanel card1, card2;
    ActionListener listener;
    JButton menu1, addOrTake, cata, payd, showd;
    //JButton 

    JFrame menu;

    public Proj() {

        menu = new JFrame("Card Layout");

        //final Container contentPane = menu.getContentPane();
        final CardLayout layout = new CardLayout();
        menu.setLayout(layout);
        card1 = new JPanel(new GridBagLayout());
        card2 = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        menu1 = new JButton("Mneu");
        menu1.setVisible(true);

        addOrTake = new JButton("Add or take");
        cata = new JButton("Cata");
        showd = new JButton("ShowD");
        payd = new JButton("payd");

        card1.add(menu1, gbc);
        card1.add(addOrTake, gbc);
        card1.add(cata, gbc);
        card1.add(showd, gbc);
        card1.add(payd, gbc);

        menu1.addActionListener(listener);
        addOrTake.addActionListener(listener);
        cata.addActionListener(listener);
        showd.addActionListener(listener);
        payd.addActionListener(listener);

        listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
            }
        };
        // TODO Auto-generated method stub

        JButton poo = new JButton("Poo");
        card2.add((poo), gbc);
        card2.add(new JButton("Pee"), gbc);
        card2.add(new JButton("Per"), gbc);
        card2.add(new JButton("POt"), gbc);

        menu.add(card2, "Card2");
        menu.add(card1, "Card1");

        layout.show(menu.getContentPane(), "Card1");

        menu.pack();
        menu.setLocationRelativeTo(null);
        menu.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Proj poop = new Proj();
            }
        });
    }
}

I'd also have a closer look at How to Use CardLayout for more details about how it works, because you're using it wrong

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Problems:

  • You're adding components to menu, the CardLayout-using container, without use of a 2nd parameter String constant. Without this, it might be hard to easily swap "cards" (components).
  • If you want a single column type of layout, you need to use a layout manager that allows this, and the JPanel default layout isn't it. Better options:
    • GridLayout(0, 1) for variable number of rows, one column
    • BoxLayout(Container, BoxLayout.PAGE_AXIS)
    • GridBagLayout with the right GridBagConstraint gridx and gridy attributes.

Upvotes: 2

Related Questions