Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何显示内部带有GridLayout按钮的多个JPanel_Java_Swing_User Interface - Fatal编程技术网

Java 如何显示内部带有GridLayout按钮的多个JPanel

Java 如何显示内部带有GridLayout按钮的多个JPanel,java,swing,user-interface,Java,Swing,User Interface,我正试图创造一些超基本的东西,但在极度沮丧之后,我想是时候在这里提问了 预期结果: 现在这是我的代码: 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.JB

我正试图创造一些超基本的东西,但在极度沮丧之后,我想是时候在这里提问了

预期结果:

现在这是我的代码:

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

}
这就是我得到的:

我对这种东西是全新的,所以请原谅我的无知/能力。。。但我想要的是3个面板从上到下(青铜/银/金),每个面板都有一个按钮的网格布局。。。但我所能得到的只是那块青铜板贴在了所有其他东西的前面。我想我应该使用BoxLayout将3个面板排序成一个顺序,但我玩了大约4个小时,感觉我没有什么进展

我还需要一种方法,使按钮变黄时,我按下他们,但目前这是不工作;虽然我几乎没看那个

这就是我得到的:

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

我想我应该使用BoxLayout

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

上的Swing教程部分提供了一个工作示例,可以帮助您入门

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

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

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

本教程还有关于
如何使用GridBag布局
如何使用GridBag布局
的章节


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

您可以尝试
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;
    }
}
import javax.swing.JFrame;
导入javax.swing.JButton;
导入java.awt.GridBagLayout;
导入java.awt.GridBagConstraints;
导入javax.swing.SwingUtilities;
导入javax.swing.JPanel;
导入java.awt.Insets;
导入java.awt.Color;
课堂演示{
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(()->{
JFrame=新JFrame(“网格”);
JPanel面板=(JPanel)frame.getContentPane();
GridBagConstraints gbc=新的GridBagConstraints();
gbc.插图=新插图(9,7,5,5);
panel.setLayout(新的GridBagLayout());
面板。添加(新网格(颜色。黄色,gbc,0,0),gbc);
面板。添加(新网格(颜色。黄色,gbc,1,0),gbc);
面板。添加(新网格(颜色。浅灰色,gbc,0,1),gbc);
面板。添加(新网格(颜色。浅灰色,gbc,1,1),gbc);
面板。添加(新网格(颜色。绿色,gbc,0,2),gbc);
面板。添加(新网格(颜色。绿色,gbc,1,2),gbc);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
});
}
私有静态JPanel newGrid(颜色、gridbagg、pGbc、intpx、intpy){
JPanel panel=newjpanel(newgridbagloayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.插图=新插图(5,5,5,5);
对于(int x=0;5>x;x++){
对于(int y=0;3>y;y++){
gbc.gridx=x;
gbc.gridy=y;
JButton btn=新JButton(“”);
背景(颜色);
面板。添加(btn、gbc);
}
}
pGbc.gridx=pX;
pGbc.gridy=pY;
返回面板;
}
}

psst你觉得我的帖子有用吗?
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;
    }
}