簡體   English   中英

填充JPanel

[英]Filling a JPanel

我在Jpanel中有一些項目,然后將其推到頂部並用作基本搜索引擎的工具欄。 我的問題是我的最后一個組合框沒有顯示,因為沒有足夠的空間。 但是,左側有很多空白,我需要移動所有內容以填充JPanel,這樣才能顯示出來。 所以我的問題是,我將如何使這些項目從最左側開始並向右移動,謝謝。

    //Labels for combo boxes
    JLabel Bookmarklbl = new JLabel("Bookmarks:");
    JLabel Historylbl = new JLabel("History:");

    FlowLayout flowLayout = new FlowLayout();
    MainBrowser.toolBar.setLayout(flowLayout);

    //Adding items to Panel
    MainBrowser.toolBar.add(Bookmarklbl);
    MainBrowser.toolBar.add(BookmarkList);
    MainBrowser.toolBar.add(bookmarkbtn);
    MainBrowser.toolBar.add(back);
    MainBrowser.toolBar.add(forward);
    MainBrowser.toolBar.add(MainBrowser.addressbar);
    MainBrowser.toolBar.add(home);
    MainBrowser.toolBar.add(reload);
    MainBrowser.toolBar.add(Historylbl);
    MainBrowser.toolBar.add(historyList);

    //Set the things added from left to right
    MainBrowser.main.setComponentOrientation(
            ComponentOrientation.LEFT_TO_RIGHT);

    //Add Panel to main frame 
    MainBrowser.main.add(MainBrowser.toolBar,BorderLayout.NORTH);

酒吧的外觀: http//postimg.org/image/l314iw6eh/

假設toolbarJPanel並且正在使用FlowLayout ,那么這段代碼可能會幫助您,

    JPanel panel = new JPanel(); // your toolbar panel
    FlowLayout flowLayout = (FlowLayout) panel.getLayout(); // flowlayout
    flowLayout.setAlignment(FlowLayout.LEFT); // alignment to left
    contentPane.add(panel, BorderLayout.NORTH); // adding this panel to original frame

希望這可以幫助

FlowLayout的默認值為CENTER。 如果沒有足夠的空間來顯示所有組件,則將組件包裝到下一行。 將對齊方式更改為LEFT不會解決此問題(只是組件的默認對齊方式)。

顯示組合框欄很大,是否可以限制寬度?

您可以使用以下方法限制組合框的寬度:

comboBox.setPrototypeDisplayValue( "XXXXXXXXXX" );

這將限制組合框的首選大小,以便它可以顯示在工具欄上。

但是,您仍然希望在顯示彈出窗口時查看項目的全文。 為此,您可以使用組合框彈出窗口

也許這會工作。

MainBrowser.toolbar.set(new FlowLayout(FlowLayout.LEFT));

編輯

抱歉是MainBrowser.toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

您可以嘗試使用BoxLayout,例如:

toolBar.setLayout(new BoxLayout(toolBar,BoxLayout.X_AXIS)).

暫無
暫無

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

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