2014-10-26 32 views
0

我在嘗試使用組合框時遇到了很大的麻煩。我有一個菜單屏幕迷宮應用程序,我想添加組合框到該菜單屏幕。但是,一旦添加,只有組合框出現,菜單屏幕消失。我很抱歉,我不太瞭解Java swing。任何建議表示讚賞。如何讓Java組合框不重疊菜單面板

下面是代碼:

private void init() { 
    add(maze.getPanel()) ; //The starting menu screen which just shows a picture 

    Integer[] skills = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, (int) 'a', (int) 'b', (int) 'c', (int) 'd', (int) 'e', (int) 'f'}; 
    skillLevelCombo = new JComboBox<Integer>(skills); //Combobox for choosing skill level 
    setLayout(new FlowLayout()); 
    skillLevelCombo.setPreferredSize(new Dimension(300, 20)); 
    add(skillLevelCombo); 


    setSize(400, 400) ; 
    setVisible(true) ; 


    kd = new ManualDriverKeyListener(this, maze, driver); 
    addKeyListener(kd); 

    // focus should be on the JFrame of the MazeApplication and not on the maze panel 
    // such that the SimpleKeyListener kl is used 
    //maze.setFocusable(false) ; // happens internally on MazePanel 
    setFocusable(true) ; 
    maze.init(); 
    robot.setMaze(maze); 
    try { 
     driver.setRobot(robot); 
    } catch (UnsuitableRobotException e) { 
     System.out.println("Unsuitable Robot"); 
    } 
} 

回答

1

我明白了。最好的辦法是一個BorderLayout。

/** 
* Initializes some internals and puts the game on display. 
*/ 
private void init() { 

    Integer[] skills = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, (int) 'a', (int) 'b', (int) 'c', (int) 'd', (int) 'e', (int) 'f'}; 
    String[] mazeAlgorithms = {"Default", "Prim", "Aldous-Broder"}; 
    String[] driverAlgorithms = {"Manual", "Gambler", "WallFollower", "Wizard", "Tremaux"}; 
    skillLevelCombo = new JComboBox<Integer>(skills); 
    mazeAlgorithmCombo = new JComboBox<String>(mazeAlgorithms); 
    driverAlgorithmCombo = new JComboBox<String>(driverAlgorithms); 
    JPanel combosPanel = new JPanel(); 
    combosPanel.setLayout(new FlowLayout(FlowLayout.TRAILING)); 
    combosPanel.add(skillLevelCombo); 
    combosPanel.add(mazeAlgorithmCombo); 
    combosPanel.add(driverAlgorithmCombo); 
    setLayout(new BorderLayout()); 

    add(maze.getPanel(), BorderLayout.CENTER); 
    add(combosPanel, BorderLayout.PAGE_END); 


    setSize(400, 450) ; 
    setVisible(true) ; 


    kd = new ManualDriverKeyListener(this, maze, driver); 
    addKeyListener(kd); 

    // focus should be on the JFrame of the MazeApplication and not on the maze panel 
    // such that the SimpleKeyListener kl is used 
    //maze.setFocusable(false) ; // happens internally on MazePanel 
    setFocusable(true) ; 
    maze.init(); 
    robot.setMaze(maze); 
    try { 
     driver.setRobot(robot); 
    } catch (UnsuitableRobotException e) { 
     System.out.println("Unsuitable Robot"); 
    } 
} 
1

要知道,你並沒有真正問了一個問題。

StackOverflow更好地處理具體問題 - 「我已經完成了X,期待Y,而發生了Z」,這種事情。模糊的點我在一個方向有太多的可變性是有用的。

您將不得不瞭解佈局管理器 - 查找教程 - Oracle有一個 - 並通過它。你沒有說明你的意思是「短時間」來實現這一點,但這不是一個步驟1-2-3的事情。

如果您打算使用代碼,最好有完整的代碼(可以在沒有任何東西的情況下運行,但添加了標準庫),並說明您擁有的具體問題。

+0

嗨我改變了問題,希望現在更清楚。我聽說過Flow佈局管理器,但我不確定這是否正確使用。 – mrQWERTY 2014-10-26 23:29:04

+0

*「你好,我改變了問題..」*有什麼問題?您已經添加了代碼問題的詳細信息以及不可編譯的代碼片段,但仍然沒有*問題。*並且提及「不可編譯的代碼片段」:爲了更快地獲得更好的幫助,請發佈[MCVE](http:// stackoverflow.com/help/mcve)(最小完整可驗證示例)。 – 2014-10-27 02:40:02

0

您實際上不需要將FlowLayout設置爲佈局,因爲它被設置爲默認值。我認爲你應該試試GridLayout,通過加入setLayout(new GridLayout(1,2))或其他方式參數取決於你想要如何佈局。 您也可以將組件放在JSplitPane上。