簡體   English   中英

在boxlayout中是否有“從上到下”和“從右到左”?

[英]Is there any 'top to bottom' and 'right to left' in boxlayout?

我有一些桌子應該從框架的右到左和從上到下繪制。 現在我使用絕對布局和協調工作。 是否有任何BoxLayout或任何其他Java布局可以做到這一點? 我應該提到表的數量是動態的。

我的第二個問題是如何將這些表格停靠在框架上? 我的意思是我想在幀調整大小時,表格保持在屏幕上的位置。

大多數布局管理器都會尊重組件的方向:

panel.setComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT );
panel.add(...);

或者,您始終只需將組件添加到容器的開頭即可

panel.add(component1, 0);
panel.add(component2, 0);

如果要將事物排列到表中,則可能需要使用網格。 網格中的所有元素都應該是相同的大小。

要垂直排列某些項目,每行的大小可能會有所不同,請嘗試以下方法:

static JPanel buildPanel() {
    JPanel vPanel = new JPanel();
    BoxLayout layout = new BoxLayout(vPanel, BoxLayout.Y_AXIS);
    vPanel.setLayout(layout);
    JPanel[] rowPanels = new JPanel[5];
    int counter=1;
    for (int i = 0; i < rowPanels.length; i++) {
        rowPanels[i] = new JPanel();
        rowPanels[i].setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
        rowPanels[i].add(new JButton("button " + counter++));
        rowPanels[i].add(new JButton("Your button " + counter++));
        rowPanels[i].add(new JButton("Shabutton"));
        vPanel.add(rowPanels[i]);
    }
    return vPanel;
}

public static void main(String[] args) {
    JFrame gridFrame = new JFrame();
    gridFrame.add(buildPanel() );
    gridFrame.pack();
    gridFrame.setVisible(true);
}

您可以使用gridFrame.setResizable(false);阻止整個JFrame調整大小gridFrame.setResizable(false); 使用如下方法調用調整窗口大小時,可以防止在行之間添加sapce:

rowPanels[i].setMaximumSize(new Dimentsion(400,32));

暫無
暫無

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

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