如何使用GridLayout按钮显示多个JPanels

时间:2016-11-24 01:05:55

标签: java swing user-interface

我正在努力创造一些超级基本的东西但是在我感到非常沮丧之后我觉得是时候问这里了。

期望的结果:

https://i.stack.imgur.com/0LfGV.png

现在这是我的代码:

GUI类

    package bookingProject;

import java.awt.Color;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class GUI extends javax.swing.JFrame {

    JFrame frame = new JFrame();
    JPanel silverPanel = new JPanel();
    JPanel goldPanel = new JPanel();
    Button buttons[] = new Button[30];

    public static void main(String args[]) {
        new GUI();
    }

    public GUI() {
        setSize(500, 500);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel bronzePanel = new JPanel();
        bronzePanel.setLayout(new GridLayout(3, 10));
        bronzePanel.setBackground(Color.red);
        for (int i = 0; i < 30; i++) {
            buttons[i] = new Button();
            bronzePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            bronzePanel.add(buttons[i]);
        }
        add(bronzePanel);
        setVisible(true);

        silverPanel.setLayout(new GridLayout(3, 10));
        silverPanel.setBackground(Color.yellow);
        for (int i = 0; i < 30; i++) {
            buttons[i] = new Button();
            silverPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            silverPanel.add(buttons[i]);
        }
        add(silverPanel);
        setVisible(true);

        goldPanel.setLayout(new GridLayout(3, 10));
        goldPanel.setBackground(Color.green);
        for (int i = 0; i < 30; i++) {
            buttons[i] = new Button();
            goldPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            goldPanel.add(buttons[i]);
        }
        add(goldPanel);
        setVisible(true);
    }

}

我想要使用的按钮类

按钮类

    package bookingProject;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Button extends JButton implements ActionListener{
    public Button (){
    this.addActionListener(this);
}

    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        if (source instanceof GUI){
            ((GUI)source).setBackground(Color.YELLOW);
        }
}
}

这就是我得到的:

https://i.stack.imgur.com/uu9SO.png

我对这些东西很陌生,所以请原谅我的完全无知/能力...但我想要的是3个面板从上到下(青铜/银/金),每个面板都有一个GridLayout按钮......但是我所能得到的只是青铜面板贴在其他一切的前面。我想我应该使用BoxLayout将3个面板分成一个订单,但我玩了大约4个小时,觉得我无处可去。

当我按下它们时,我还需要一种使按钮变黄的方法但目前不起作用;虽然我几乎没看过那个。

2 个答案:

答案 0 :(得分:0)

  

这就是我得到的:

默认情况下,JFrame的内容窗格使用BorderLayout。向框架添加组件时,默认情况下组件会添加到BorderLayout.CENTER,因为您没有指定约束。 <{1}}中只能显示一个组件,因此只有最后添加的组件可见。

  

我想我应该使用BoxLayout

这是一种方法(但可能不是最简单的方法),因为您需要手动指定每行组件之间的空间。

How to Use Box Layout上的Swing教程中的部分有一个工作示例,可以帮助您入门。

最简单的方法是使用包含3行和2列的CENTER作为框架的布局管理器。然后,每个单独的面板也可以使用具有3行和5列的GridLayout

如果您希望每个面板中的组件之间有空格,那么您需要查看GridLayout API。它允许您指定组件之间的垂直和水平间隙。

另一种方法是使用GridLayout,虽然这有点复杂,因为您需要为添加的每个组件指定约束。

本教程还包含GridBagLayoutHow to Use GridBag Layout

部分

请注意,本教程中的示例还将向您展示如何更好地构建代码,以便在事件调度线程(EDT)上创建组件。

答案 1 :(得分:0)

您可以尝试使用GridBagLayout

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import java.awt.Insets;
import java.awt.Color;
class Demo{
    public static void main(String[]args){
        SwingUtilities.invokeLater(()->{
            JFrame frame=new JFrame("Grid");
            JPanel panel=(JPanel)frame.getContentPane();
            GridBagConstraints gbc=new GridBagConstraints();
            gbc.insets=new Insets(9,7,5,5);
            panel.setLayout(new GridBagLayout());

            panel.add(newGrid(Color.YELLOW,gbc, 0, 0),gbc);
            panel.add(newGrid(Color.YELLOW,gbc, 1, 0),gbc);

            panel.add(newGrid(Color.LIGHT_GRAY,gbc, 0, 1),gbc);
            panel.add(newGrid(Color.LIGHT_GRAY,gbc, 1, 1),gbc);

            panel.add(newGrid(Color.GREEN,gbc, 0, 2),gbc);
            panel.add(newGrid(Color.GREEN,gbc, 1, 2),gbc);

            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        });
    }
    private static JPanel newGrid(Color color, GridBagConstraints pGbc, int pX, int pY){
        JPanel panel=new JPanel(new GridBagLayout());
        GridBagConstraints gbc=new GridBagConstraints();
        gbc.insets=new Insets(5,5,5,5);
        for(int x=0;5>x;x++){
            for(int y=0;3>y;y++){
                gbc.gridx=x;
                gbc.gridy=y;
                JButton btn=new JButton("<html>&nbsp;</html>");
                btn.setBackground(color);
                panel.add(btn,gbc);
            }
        }
        pGbc.gridx=pX;
        pGbc.gridy=pY;
        return panel;
    }
}

enter image description here

相关问题