0

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

我希望 JFileChooser 在左侧,JButtons 在中间,JList 在垂直框架的右侧。 结盟

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

如果有人能告诉我如何根据我们的需要对齐组件,那对我来说真的很有帮助。

根据m图,JList在顶部,然后是b checkbox,然后是JFileChooser,然后是三个按钮。

4

1 回答 1

3

我的答案不是...,但应该更好BorderLayout

  • JListEAST(或WEST)区域

  • JFileChooser到中心区域

  • JPanelJButtonsSOUTHNORTH)区域

也许使用GridBagLayout(非常复杂LayoutManager

易于使用是MigLayout

编辑

I am unable to add JList to EAST or WEST since the frame is not getting 
extended more than the JFileChooser.

问:您不能或是否有一些要求或限制

A:我原来的想法有什么问题

在此处输入图像描述

从代码

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();
            }
        });
    }
}
于 2012-04-17T14:52:38.373 回答