Java 设置JComponents的对齐方式

Java 设置JComponents的对齐方式,java,swing,alignment,border-layout,Java,Swing,Alignment,Border Layout,我想要一种使用JavaSwing在GUI中表示组件的顺序方式。我目前正在使用中间有一个框的BorderLayout 我希望JFileChooser位于左侧,JButtons位于中间,JList位于垂直框架的右侧。 上图显示了组件的当前对齐方式 如果anbody能告诉我如何根据我们的需要调整组件,那将对我非常有帮助 根据m图,JList在顶部,后面是b复选框,然后是JFileChooser,后面是三个按钮。我的答案不是,但是对于BorderLayout JList至东部(或西部)区域 JFil

我想要一种使用JavaSwing在GUI中表示组件的顺序方式。我目前正在使用中间有一个框的BorderLayout

我希望JFileChooser位于左侧,JButtons位于中间,JList位于垂直框架的右侧。

上图显示了组件的当前对齐方式

如果anbody能告诉我如何根据我们的需要调整组件,那将对我非常有帮助


根据m图,JList在顶部,后面是b复选框,然后是JFileChooser,后面是三个按钮。

我的答案不是,但是对于
BorderLayout

  • JList
    东部
    (或
    西部
    )区域

  • JFileChooser
    到中心区域

  • JPanel
    JButtons
    (或
    )区域

可能使用
GridBagLayout
(非常复杂的
LayoutManager

易于使用的是
MigLayout

编辑

问:你不能或有什么要求或限制吗

我原来的想法怎么了

源代码

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

public class BorderLayoutWithJComponents {

    public BorderLayoutWithJComponents() {
        String[] subItems1 = {"Red", "Blue", "Green", "Circle", "Square",
        "Triangle", "Apple", "Orange", "Banana"};
        JList list = new JList(subItems1);
        JFileChooser myFileChooser= new JFileChooser();
        JButton one = new JButton("One");
        JButton two = new JButton("Two");
        JButton three = new JButton("Three");
        JPanel panel = new JPanel();
        panel.add(one);
        panel.add( two);
        panel.add(three);
        JFrame f = new JFrame("LayoutTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(list, BorderLayout.WEST);
        f.add(myFileChooser, BorderLayout.CENTER);
        f.add(panel, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
              new  BorderLayoutWithJComponents();
            }
        });
    }
}

我的答案不是,但是对于
BorderLayout

  • JList
    东部
    (或
    西部
    )区域

  • JFileChooser
    到中心区域

  • JPanel
    JButtons
    (或
    )区域

可能使用
GridBagLayout
(非常复杂的
LayoutManager

易于使用的是
MigLayout

编辑

问:你不能或有什么要求或限制吗

我原来的想法怎么了

源代码

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

public class BorderLayoutWithJComponents {

    public BorderLayoutWithJComponents() {
        String[] subItems1 = {"Red", "Blue", "Green", "Circle", "Square",
        "Triangle", "Apple", "Orange", "Banana"};
        JList list = new JList(subItems1);
        JFileChooser myFileChooser= new JFileChooser();
        JButton one = new JButton("One");
        JButton two = new JButton("Two");
        JButton three = new JButton("Three");
        JPanel panel = new JPanel();
        panel.add(one);
        panel.add( two);
        panel.add(three);
        JFrame f = new JFrame("LayoutTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(list, BorderLayout.WEST);
        f.add(myFileChooser, BorderLayout.CENTER);
        f.add(panel, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
              new  BorderLayoutWithJComponents();
            }
        });
    }
}


通常,解决方案是使用一个或多个GridBagLayouts的多个JPanel。因此,我应该为每个面板分配一个组件,然后继续吗?或者我应该将当前布局更改为GridBagLayout吗?为了更快地获得更好的帮助,请发布一个。我认为mKorbel已经为您提供了最好的解决方案。如果您使用的不是这种情况,也许SSCCE会解释原因。至少,我尝试使用GridBagLayout,这或多或少解决了我的目的。因此,我目前正在修复对齐。通常情况下,解决方案是使用一个或多个GridBagLayouts的多个JPanel。因此,我应该为每个面板分配一个组件,然后继续或..?或者我应该将当前布局更改为GridBagLayout?要更快获得更好的帮助,请发布一个。我认为mKorbel已经为您提供了最好的解决方案。如果您使用的不是这种情况,也许SSCCE会解释原因。至少,我尝试使用GridBagLayout,这或多或少解决了我的目的。因此,我正在修正我的路线。谢谢我们的建议。!我将尝试一下。我无法将JList添加到EAST或WEST,因为框架的扩展程度不超过JFileChooser。非常感谢您的想法。!我的想法是将JList和按钮(面板)添加到EAST,这意味着JFileChooser应该是第一个,然后是其余的。有可能两者都会向东吗?实际上,我认为在Borderlayout中,它不能顺序对齐,但这只能对齐北、南、东、西侧,就像这个例子,但可能是我可以尝试MIGLayoutThanks的建议。!我将尝试一下。我无法将JList添加到EAST或WEST,因为框架的扩展程度不超过JFileChooser。非常感谢您的想法。!我的想法是将JList和按钮(面板)添加到EAST,这意味着JFileChooser应该是第一个,然后是其余的。有可能两者都会向东吗?实际上我认为在Borderlayout中,它不能顺序对齐,但这只能对齐北、南、东、西侧,就像这个例子,但可能是我可以尝试MIGLayout