Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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 GUI中的面板_Java_Swing_User Interface_Panel - Fatal编程技术网

将面板放入java GUI中的面板

将面板放入java GUI中的面板,java,swing,user-interface,panel,Java,Swing,User Interface,Panel,我正在尝试制作一个三个面板相邻的GUI。然后我想在第一个窗格中放置一个由5 X 2个面板组成的网格。我已经成功地创建了ttop 2,但似乎无法将额外的面板放入其中。任何帮助都将不胜感激 import java.awt.*; import javax.swing.*; import javax.swing.JPanel.*; import java.awt.Color.*; /** * Write a description of class SimpleFrame here. * * @a

我正在尝试制作一个三个面板相邻的GUI。然后我想在第一个窗格中放置一个由5 X 2个面板组成的网格。我已经成功地创建了ttop 2,但似乎无法将额外的面板放入其中。任何帮助都将不胜感激

import java.awt.*;
import javax.swing.*;
import javax.swing.JPanel.*;
import java.awt.Color.*;
/**
 * Write a description of class SimpleFrame here.
 *
 * @author OFJ2
 * @version 
 */
public class Game extends JFrame
{
private final int ROWS = 5;
private final int COLS = 2;
private final int GAP = 2;
private final int NUM = ROWS * COLS;
private int x;
private JPanel leftPanel = new JPanel(new GridLayout(ROWS,COLS, GAP,GAP));
private JPanel [] gridPanel = new JPanel[NUM];
private JPanel middlePanel = new JPanel();    
private JPanel rightPanel = new JPanel();
private Color col1 = Color.WHITE;
private Color col2 = Color.BLUE;
private Color tempColor;


public Game()
{
    super("Chasing Bombs OFJ2");
    setSize(200,200);
    setVisible(true);
    makeFrame();
}


public void makeFrame()
{
    Container contentPane = getContentPane();
    contentPane.setLayout(new GridLayout());
    leftPanel.setLayout(new BorderLayout());

    //JLabel label2 = new JLabel("Pocahontas");

    JButton button1 = new JButton("One");
    JButton button2 = new JButton("Two");


    add(leftPanel);

    add(middlePanel, new FlowLayout());

    add(rightPanel);

    setGrid();
    //middlePanel.add(label2);
    rightPanel.add(button1);
    rightPanel.add(button2);
    leftPanel.setBackground(Color.PINK);
    middlePanel.setBackground(Color.RED);

}

public void setGrid()
{
    for(int x = 0; x < NUM; x++) {
           gridPanel[x] = new JPanel();
           leftPanel.add(gridPanel[x]);
           if (x % COLS == 0) {
              tempColor = col1;
              col1 = col2;
              col2 = tempColor;}
           if (x % 2 == 0) {
              gridPanel[x].setBackground(col1);}
           else {
             gridPanel[x].setBackground(col2);}
        }
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.JPanel.*;
导入java.awt.Color.*;
/**
*在此处编写SimpleName类的描述。
*
*@j2作者
*@版本
*/
公共类游戏扩展JFrame
{
私有最终整数行=5;
私人最终整数=2;
私人最终积分差距=2;
私有最终整数=行*列;
私人INTX;
private JPanel leftPanel=new JPanel(新网格布局(行、列、间隙、间隙));
private JPanel[]gridPanel=new JPanel[NUM];
private JPanel middlePanel=new JPanel();
private JPanel rightPanel=new JPanel();
私有颜色col1=Color.WHITE;
专用颜色col2=Color.BLUE;
私人色彩;
公共游戏()
{
超级(“追击J2炸弹”);
设置大小(200200);
setVisible(真);
makeFrame();
}
公共void makeFrame()
{
容器contentPane=getContentPane();
setLayout(新的GridLayout());
setLayout(新的BorderLayout());
//JLabel label2=新的JLabel(“Pocahontas”);
JButton button1=新JButton(“一”);
JButton button2=新JButton(“两个”);
添加(左面板);
添加(中间面板,新的FlowLayout());
添加(右面板);
setGrid();
//中间面板。添加(标签2);
右面板。添加(按钮1);
右面板。添加(按钮2);
左面板。背景(颜色。粉红色);
中间板。立根背景(颜色。红色);
}
公共void setGrid()
{
对于(int x=0;x
}

这是我到目前为止的代码。我怀疑这与setGrid()方法的定位有关

谢谢

然后我想在第一个窗格中放置一个由5 X 2个面板组成的网格

您将布局设置为BorderLayout,但需要5x2网格,因此应使用
GridLayout

leftPanel.setLayout(new BorderLayout());