GridLayout未在JFrame中显示

时间:2014-08-12 16:52:20

标签: java swing jpanel layout-manager grid-layout

我有一个GridLayout(2,1)由另外两个GridLayouts(8,4)组成。我将(2,1)布局添加到我的主JFrame中,但是当我运行它时,只显示了8,4个布局中的一个。我想要发生的是它们两者叠加在一起。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Pos extends JFrame {

JTextField oneTicks;
JTextField twoTicks;
JTextField threeTicks;
JTextField fiveTicks;
JTextField tenTicks;
JTextField twentyTicks;
JTextField twentyFiveTicks;
JTextField thirtyTicks;

JTextField[] ticks = { oneTicks, twoTicks, threeTicks, fiveTicks, tenTicks,
        twentyTicks, twentyFiveTicks, thirtyTicks };

JTextField oneAmount;
JTextField twoAmount;
JTextField threeAmount;
JTextField fiveAmount;
JTextField tenAmount;
JTextField twentyAmount;
JTextField twentyFiveAmount;
JTextField thirtyAmount;

JTextField[] amounts = { oneAmount, twoAmount, threeAmount, fiveAmount,
        tenAmount, twentyAmount, twentyFiveAmount, thirtyAmount };

JLabel oneLabel = new JLabel("$ 1 Games");
JLabel twoLabel = new JLabel("$ 2 Games");
JLabel threeLabel = new JLabel("$ 3 Games");
JLabel fiveLabel = new JLabel("$ 5 Games");
JLabel tenLabel = new JLabel("$ 10 Games");
JLabel twentyLabel = new JLabel("$ 20 Games");
JLabel twentyFiveLabel = new JLabel("$ 25 Games");
JLabel thirtyLabel = new JLabel("$ 30 Games");

JLabel[] gameLabel = { oneLabel, twoLabel, threeLabel, fiveLabel, tenLabel,
        twentyLabel, twentyFiveLabel, thirtyLabel };

JLabel oneXLabel = new JLabel("X $ 1");
JLabel twoXLabel = new JLabel("X $ 2");
JLabel threeXLabel = new JLabel("X $ 3");
JLabel fiveXLabel = new JLabel("X $ 5");
JLabel tenXLabel = new JLabel("X $ 10");
JLabel twentyXLabel = new JLabel("X $ 20");
JLabel twentyFiveXLabel = new JLabel("X $ 25");
JLabel thirtyXLabel = new JLabel("X $ 30");

JLabel[] multiLabel = { oneXLabel, twoXLabel, threeXLabel, fiveXLabel,
        tenXLabel, twentyXLabel, twentyFiveXLabel, thirtyXLabel };

Pos() {
    initJTextField(ticks);
    initJTextField(amounts);

    JPanel lottoPanel = new JPanel(new GridLayout(2, 1));

    JPanel startGrid = new JPanel(new GridLayout(8, 4));
    JPanel endGrid = new JPanel(new GridLayout(8, 4));

    initLottoPanel(startGrid);
    initLottoPanel(endGrid);

    lottoPanel.add(startGrid);
    lottoPanel.add(endGrid);

    this.add(lottoPanel, BorderLayout.LINE_START);

}

// init all JTextfields
void initJTextField(JTextField[] jArray) {
    for (int i = 0; i < ticks.length; i++) {
        jArray[i] = new JTextField();
    }
}

void initLottoPanel(JPanel panel) {
    for (int i = 0; i < gameLabel.length; i++) {
        panel.add(gameLabel[i]);
        panel.add(ticks[i]);
        panel.add(multiLabel[i]);
        panel.add(amounts[i]);
    }
}

public static void main(String[] args) {
    Pos pos = new Pos();
    pos.setSize(600, 600);
    pos.setLocationRelativeTo(null);
    pos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pos.pack();
    pos.setVisible(true);
}

}

2 个答案:

答案 0 :(得分:1)

两个面板都被添加到JFrame中,但其中一个是完全空的。当您调用initLottoPanel方法时,实际上是将所有子组件(JLabel,JTextFeild等)重新分配给正在传递的新面板。

要解决此问题,您需要有两组子组件,每组子组件只对应一个组件。

答案 1 :(得分:0)

您正在将gameLabelticksmultiLabelamounts中的对象的相同实例添加到startGrid和endGrid中。这就像在JFrame中将1个对象添加到2个不同的位置

如果更改构造函数的这一部分

initLottoPanel(startGrid);
//initLottoPanel(endGrid);

lottoPanel.add(startGrid);
//lottoPanel.add(endGrid);

你会看到startGrid(因为现在这些对象只被加载到JFrame中一次)

相关问题