创建JButtons的行和列

时间:2016-11-25 21:52:33

标签: java swing button layout-manager

我已经完成了创建3套座椅的任务,每套座椅有3排10列座位。首先,我开始为三组创建一个席位,我现在尝试使用布局管理器以3x10样式显示席位(JButtons)。这是我的代码:

public class ConcertPanel extends JPanel
{
    private JPanel Panel1;
    private JButton goldSeat;
    private JButton silverSeat;
    private JButton bronzeSeat;


   ConcertPanel()
   {   
        Panel1 = new JPanel();
        Panel1.setSize(500,500);

                setLayout(new GridLayout(rows, cols));
                for(int i = 0; i < rows; i++)
                for(int j = 0; j < cols; j++)
            {
                goldSeat = new JButton();
                silverSeat = new JButton();
                bronzeSeat = new JButton();
                add(new JButton());
            }
                this.add(Panel1);

       }
}

当我在jpanel中获得30个按钮时,它们不在行或列中?帮助:(

1 个答案:

答案 0 :(得分:5)

请再看一下GridLayout tutorial

  • 您想将GridLayout设置为容器一次。
  • 添加GridLayout时,添加没有第二个参数的组件(JButton)。
  • 如果布局应为3 x 10,则您需要向此容器添加30个组件。
  • 您正在使用JPanel将另一个JPanel,panel1添加到GridLayout,它将成为网格的一部分。你好像不想这样做。

例如:

Example GUI

import java.awt.GridLayout;

import javax.swing.*;

public class GridLayoutEg extends JPanel {
    private static final int ROWS = 3;
    private static final int COLS = 10;

    public GridLayoutEg() {
        setLayout(new GridLayout(ROWS, COLS)); // set JPanel's layout
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                String text = String.format("[%d, %d]", j + 1, i + 1);
                add(new JButton(text)); // add component w/o 2nd parameter
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        GridLayoutEg mainPanel = new GridLayoutEg();
        JFrame frame = new JFrame("GridLayoutEg");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

修改

请参阅有关此代码问题的评论:

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++) {
            goldSeat = new JButton();   // button created but never added
            silverSeat = new JButton(); // button created but never added
            bronzeSeat = new JButton(); // button created but never added
            add(new JButton()); // only button added
        }
    this.add(Panel1); // this throws off the whole GridLayout since it's added to the grid

因此,在我们的嵌套for循环中,您创建了3个JButton,但由于它们从未被添加到GUI中,因此它们没有任何用途,实际上将被垃圾收集,除了最后3个创建之外。然后创建一个按钮并添加它。最后,你创建了一个JPanel,但是使用JPanel将它添加到GridLayout,这会抛出网格。

使用此代码获得更多乐趣:

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

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

public class GridLayoutEg extends JPanel {
    private static final long serialVersionUID = 1L;
    private static final int ROWS = 3;
    private static final int COLS = 10;

    public GridLayoutEg() {
        int gap = 1;
        int gap2 = 2;
        setBorder(BorderFactory.createEmptyBorder(gap2, gap2, gap2, gap2));
        setLayout(new GridLayout(ROWS, COLS, gap, gap)); // set JPanel's layout
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                JButton button = createSeat(row, col);
                add(button); // add component w/o a 2nd parameter
            }
        }
    }

    private JButton createSeat(int row, int col) {
        SeatColor seatColor = SeatColor.values()[row];
        SeatAction seatAction = new SeatAction(seatColor, col);
        JButton button = new JButton(seatAction);
        button.setBackground(seatColor.getColor());
        int topGap = 8;
        int sideGap = 25;
        Border innerBorder = BorderFactory.createEmptyBorder(topGap, sideGap, topGap, sideGap);
        Border outerBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, seatColor.getColor().brighter(),
                seatColor.getColor().darker());
        Border border = BorderFactory.createCompoundBorder(outerBorder, innerBorder);
        button.setBorder(border);
        return button;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        GridLayoutEg mainPanel = new GridLayoutEg();
        JFrame frame = new JFrame("GridLayoutEg");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

class SeatAction extends AbstractAction {
    private static final long serialVersionUID = 1L;
    private SeatColor seatColor;
    private int column;

    public SeatAction(SeatColor seatColor, int column) {
        super(String.format("[%d]", column));
        this.seatColor = seatColor;
        this.column = column;
        // putValue(LARGE_ICON_KEY, createIcon(seatColor, column));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        AbstractButton btn = (AbstractButton) e.getSource();
        btn.setBackground(seatColor.getColor().brighter());
        setEnabled(false);

        String text = String.format("Selected Seat Color: %s; Column: %d", seatColor, column);
        System.out.println(text );
    }
}

enum SeatColor {
    GOLD("Gold", new Color(255, 215, 0)), 
    SILVER("Silver", new Color(192, 192, 192)), 
    BRONZE("Bronze", new Color(205, 127, 50));

    private String text;
    private Color color;

    private SeatColor(String text, Color color) {
        this.text = text;
        this.color = color;
    }

    public String getText() {
        return text;
    }

    public Color getColor() {
        return color;
    }

    @Override
    public String toString() {
        return getText();
    }

}