簡體   English   中英

如何將JPanel中的組件對齊到左側?

[英]How do I align components in a JPanel to the left?

我已經為基本的文本編輯器創建了一個布局,我希望頂部欄的左邊是按鈕,頂欄是JPanel,它使用的是FlowLayout管理器。 它使用GridBag布局管理器在網格內部。 有什么建議么?

import java.awt.*;
import javax.swing.*;

public class GridTest {
private static JButton firstButton, secondButton, thirdButton;
private static JPanel panel, sidebar, infoPanel;
private static JTextArea textArea;

public static void addComponentsToPane(Container container) {
    container.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    firstButton = new JButton("Button 1");
    secondButton = new JButton("Button 2");
    thirdButton = new JButton("Button 3");

    panel = new JPanel(new FlowLayout());
    sidebar = new JPanel(new FlowLayout());
    infoPanel = new JPanel(new FlowLayout());

    textArea = new JTextArea("This is some generic text!");

    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0; 
    c.weighty = 0;
    c.weightx = 1;
    c.gridx = 0;
    c.gridwidth = 3;
    c.gridy = 0;
    c.anchor = GridBagConstraints.LINE_START; 
    container.add(panel, c);

    panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    panel.setBorder(BorderFactory.createLineBorder(Color.black));
    panel.add(firstButton);
    panel.add(secondButton);
    panel.add(thirdButton);

    c.fill = GridBagConstraints.BOTH;
    c.ipady = 300; 
    c.ipadx = 100;
    c.weighty = 1;
    c.weightx = 0;
    c.gridx = 0;
    c.gridheight = 2;
    c.gridwidth = 1;
    c.gridy = 1;
    container.add(sidebar, c);

    sidebar.setBorder(BorderFactory.createLineBorder(Color.black));

    c.fill = GridBagConstraints.BOTH;
    c.ipady = 40;      
    c.weightx = 1;
    c.weighty = 1;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.gridx = 1;
    c.gridy = 1;
    container.add(textArea, c);

    textArea.setFont(new Font("Serif", Font.ITALIC, 16));
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 20; 
    c.weighty = 0;
    c.weightx = 0;
    c.anchor = GridBagConstraints.PAGE_END; 
    c.gridx = 1;
    c.gridwidth = 2;
    c.gridy = 2;
    container.add(infoPanel, c);

    infoPanel.setBorder(BorderFactory.createLineBorder(Color.black));
}


private static void createAndShowGUI() {
    JFrame frame = new JFrame("Grid test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addComponentsToPane(frame.getContentPane());
    frame.setSize(800,600);
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

考慮使用JToolBar而不是JPanel ,因為它擁有自己的布局管理器,基本上它可以這樣做。

工具欄工具欄

來自以下教程的圖片......

有關更多詳細信息,請查看如何使用工具欄

一種方法:將FlowLayout更改為更喜歡將其組件放在左側的FlowLayout:

panel = new JPanel(new FlowLayout(FlowLayout.LEADING));

暫無
暫無

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

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