简体   繁体   English

框架中添加了2个面板

[英]2 Panels added to Frame are one on top of the other

I am trying to add to JPanels to JFrame but the second one just go on top of the first one and I can't seem to understand why or how to fix it. 我试图将JPanels添加到JFrame中,但是第二个只是在第一个之上,而我似乎无法理解为什么或如何修复它。

Below is the UI that I try to add. 以下是我尝试添加的UI。 Thanks a lot, 非常感谢,

Rotem ROTEM

private static void createAndShowGUI() {
    JFrame f = new JFrame("Maman 13 - Part 2");
    f.setLayout(new BorderLayout());
    //f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout alignLeftLayout = new FlowLayout(FlowLayout.LEFT, 5, 5);
    // first row
    Hashtable<Integer, JButton> hashTable = new Hashtable<Integer, JButton>();
    LinkedHashMap<String, Integer> buttons = new LinkedHashMap<String, Integer>();
    addFirstRow(buttons);
    JPanel firstRow = new KeyboardRow(hashTable, buttons);
    firstRow.setLayout(alignLeftLayout);
    f.add(firstRow);
    // second row
    buttons = new LinkedHashMap<String, Integer>();
    addSecondRow(buttons);
    JPanel secondRow = new KeyboardRow(hashTable, buttons);
    secondRow.setLayout(alignLeftLayout);
    f.add(secondRow);

    f.pack();
    f.setVisible(true);
}

Instead of 代替

f.setLayout(new BorderLayout());

try 尝试

f.setLayout(new GridLayout());

I think you need to do like 我想你需要喜欢

f.add(firstRow, BorderLayout.WEST)

and

f.add(secondRow, BorderLayout.EAST)

or something like that. 或类似的东西。 You are not specifying how to use the BorderLayout scheme you added. 您没有指定如何使用添加的BorderLayout方案。

Try using the following to replace your f.add([something]); 尝试使用以下内容替换您的f.add([something]); lines. 线。

f.add(firstRow, BorderPanel.LINE_START);
f.add(secondRow, BorderPanel.LINE_END);

This should put the firstRow panel on the left, and the secondRow panel on the right. 这应该将firstRow面板放在左侧,将secondRow面板放在右侧。

This works because a BorderLayout is built to hold up to five panels, as explained here: http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html 这是有效的,因为BorderLayout可以容纳最多五个面板,如此处所述: http : //docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html

Your existing code just adds the new panels to f, without specifying where they should go. 您现有的代码只是将新面板添加到f中,而没有指定它们应该去哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM