簡體   English   中英

框架中添加了2個面板

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

我試圖將JPanels添加到JFrame中,但是第二個只是在第一個之上,而我似乎無法理解為什么或如何修復它。

以下是我嘗試添加的UI。 非常感謝,

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

代替

f.setLayout(new BorderLayout());

嘗試

f.setLayout(new GridLayout());

我想你需要喜歡

f.add(firstRow, BorderLayout.WEST)

f.add(secondRow, BorderLayout.EAST)

或類似的東西。 您沒有指定如何使用添加的BorderLayout方案。

嘗試使用以下內容替換您的f.add([something]); 線。

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

這應該將firstRow面板放在左側,將secondRow面板放在右側。

這是有效的,因為BorderLayout可以容納最多五個面板,如此處所述: http : //docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html

您現有的代碼只是將新面板添加到f中,而沒有指定它們應該去哪里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM