-1

我正在尝试创建一个带有一堆 Jbuttons 的 JFrame,。中间有 40 个,每行 4 个,而 6 个 Jbuttons 在左侧向下的垂直线上。问题是,侧面按钮下方的第五个 Jbutton 出现在所有其他按钮的后面,并占据了整个窗口。我不知道这是从哪里来的,因为我从未将 JButton 的尺寸设置为那么大,或者适合屏幕。

import javax.swing.*;
public class Buttons {

static JButton[][] middle = new JButton [10][4]; //middle buttons
static JButton[] colours = new JButton[6]; //colour buttons


public static void main (String arg[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600,700);

    int xcor = 100; //x coordinate
    int ycor = 5; //y coordinate

    //middle buttons
    for (int y = 0; y<10; y++) {
        for (int x = 0; x<4; x++) {
            middle[y][x] = new JButton();
            middle[y][x].setBounds(xcor, ycor, 100, 60);
            middle[y][x].setVisible(true);
            frame.getContentPane().add(middle[y][x]);

            xcor+=100;
        }
        xcor=100;
        ycor+=60;
    }

    //colour buttons 
    ycor=65;
    for (int y = 0; y<5; y++){
        colours[y] = new JButton();
        colours[y].setBounds(5, ycor, 90, 60);

        switch (y) {
        case 0: colours[y].setText("Red");
        break;
        case 1: colours[y].setText("Blue");
        break;
        case 2: colours[y].setText("Green");
        break;
        case 3: colours[y].setText("Yellow");
        break;
        case 4: colours[y].setText("Purple");
        break;
        case 5: colours[y].setText("Black");
        break;
        }
        colours[y].setVisible(true);
        frame.getContentPane().add(colours[y]);

        ycor += 60;
    }
    frame.setVisible(true);
}

在此处输入图像描述

4

2 回答 2

3

这种布局可以通过在 a 中使用两个实例GridLayout(左边的一个是单列网格布局,而右边的可能是 a GridLayout(0,4))来实现JPanel。然后这两个面板可能有一个EmptyBorder集合,并被添加到 a 的LINE_STARTandCENTERBorderLayout

像这样:

在此处输入图像描述

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;

public class Buttons {

    private JButton[][] middle = new JButton[10][4]; //middle buttons
    private JButton[] colours = new JButton[6]; //colour buttons

    Buttons() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // unnecessary, just lay it out and call pack()!
        //frame.setSize(600, 700);

        Insets zeroMargin = new Insets(0, 0, 0, 0);
        BufferedImage bi = new BufferedImage(
                50, 30, BufferedImage.TYPE_INT_ARGB);

        JPanel middlePanel = new JPanel(new GridLayout(0, 4));
        middlePanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        frame.add(middlePanel, BorderLayout.CENTER);

        //middle buttons
        for (int y = 0; y < 10; y++) {
            for (int x = 0; x < 4; x++) {
                JButton b = new JButton();
                middle[y][x] = b;
                b.setMargin(zeroMargin);
                b.setIcon(new ImageIcon(bi));
                middlePanel.add(b);
            }
        }

        JPanel colorButtons = new JPanel(new GridLayout(0, 1));
        JPanel colorButtonsConstrain = new JPanel(new FlowLayout());
        colorButtons.setBorder(new EmptyBorder(65, 5, 5, 0));
        colorButtonsConstrain.add(colorButtons);
        frame.add(colorButtonsConstrain, BorderLayout.LINE_START);
        Color[] colors = {
            Color.RED, Color.BLUE, Color.GREEN,
            Color.YELLOW, Color.MAGENTA.darker(), Color.BLACK
        };
        // colour buttons
        for (int y = 0; y < colors.length; y++) {
            JButton b = new JButton();
            b.setMargin(zeroMargin);
            b.setIcon(new ImageIcon(bi));
            colours[y] = b;
            b.setBackground(colors[y]);
            colorButtons.add(b);
        }

        frame.pack();
        frame.setMinimumSize(frame.getSize());
        frame.setVisible(true);
    }

    public static void main(String arg[]) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new Buttons();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
于 2014-01-13T23:17:04.200 回答
0

将布局设置为null( frame.setLayout(null);) 可以解决这个特定问题,但会引入更多问题。定制LayoutManager将是一个更好的解决方案。

于 2014-01-13T23:11:29.117 回答