java - 如何将 JPanel 中的组件左对齐?

标签 java swing layout jpanel

我已经为基本的文本编辑器创建了一个布局,我希望顶部栏的按钮与左侧对齐,顶部栏是一个 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,因为它有自己的布局管理器,基本上可以以任何方式执行此操作。

Tool Bar Tool bar

来自以下教程的图片...

查看 How to use Tool Bars了解更多详情

关于java - 如何将 JPanel 中的组件左对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778470/

相关文章:

java - 如何在 Java 中创建通用数据类型值的多重映射

java - GUI FlowLayout,锁定所有组件位置

jquery - 滚动时移动的固定位置侧边栏

android - 将单击设置为单个布局中的两个不同的三角形图像

wordpress 中的 CSS 主题

java - Hibernate Criteria Query like CLOB 上的运算符(Postgres 8.4)

java - 如何隐藏TOMCAT webapp文件夹中的JSP文件

java - Java 的序列化文件与 C 语言(JNI)中的 `sizeof` 运算符不相等

Java - 如何使图像消失

java - 停止在 BoxLayout 中扩展 JScrollPane