Gui按钮布局麻烦

时间:2016-04-13 23:10:09

标签: java swing user-interface layout-manager cardlayout

我正在尝试使用流布局来制作一个单个列的菜单屏幕,但每当我添加一个按钮时,它都会将它添加到一行。

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();
    }
}

2 个答案:

答案 0 :(得分:2)

问题:

  • 您正在向菜单添加组件,即使用CardLayout的容器,而不使用第二个参数String常量。如果没有这个,可能很难轻易交换“卡片”(组件)。
  • 如果您需要单列类型的布局,则需要使用允许此布局管理器,而JPanel默认布局不是这样。更好的选择:
    • GridLayout(0,1)表示可变行数,一列
    • BoxLayout(Container,BoxLayout.PAGE_AXIS)
    • 使用正确的GridBagConstraint gridx和gridy属性的GridBagLayout。

答案 1 :(得分:1)

首先使用能够取得结果的布局管理器。

首先查看Laying Out Components Within a ContainerHow to Use GridLayoutHow to Use GridBagLayout,了解一些想法。

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();
            }
        });
    }
}

我还要仔细查看How to Use CardLayout,了解有关其工作原理的详细信息,因为您使用错了