首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将面板放入面板中

将面板放入面板中
EN

Stack Overflow用户
提问于 2019-03-16 18:43:32
回答 1查看 629关注 0票数 0

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

代码语言:javascript
复制
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);}
        }
}

}

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-16 18:47:25

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

代码语言:javascript
复制
leftPanel.setLayout(new BorderLayout());

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55200316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档