java,JList位置

时间:2018-03-09 09:43:50

标签: java jlist

我停留在这里,但是什么dosnt我不能将布局中的JList移动​​到left-top-corner.Im尝试使用list.setLocation();将列表移动到左上角但似乎这不好用。谢谢!

public class JList_ extends JFrame {
    private JList list;
    private static String[] colorname = {"black", "blue", "red", "white"};
    private static Color[] colors = {Color.BLACK, Color.BLUE, Color.RED,
            Color.WHITE};

    public JList_() {
        super("titile");
        setLayout(new FlowLayout());

        list = new JList(colorname);
        list.setVisibleRowCount(10);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setLocation();

        add(new JScrollPane(list));


        list.addListSelectionListener(
                new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent event) {

                        getContentPane().setBackground(colors[list.getSelectedIndex()]);

                    }

                }

        );
    }
}    

3 个答案:

答案 0 :(得分:1)

只需将布局中的路线设置为LEFT。

 layout.setAlignment(FlowLayout.LEFT);

使用您的代码的完整工作示例:

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ListLocation extends JFrame {

    private JList<String> list;
    private static String[] colorname = {"black", "blue", "red", "white"};
    private static Color[] colors = {Color.BLACK, Color.BLUE, Color.RED, Color.WHITE};

    public ListLocation() {
        super("title");

        FlowLayout layout = new FlowLayout();
        layout.setAlignment(FlowLayout.LEFT);

        list = new JList<String>(colorname);
        list.setVisibleRowCount(10);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        setLayout(layout);
        add(list);

        list.addListSelectionListener(
                new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                getContentPane().setBackground(colors[list.getSelectedIndex()]);
            }
        }
        );

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 800);
        setLocationRelativeTo(null);

        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    new ListLocation();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

有关How to Use FlowLayout

FlowLayout的更多信息

答案 1 :(得分:0)

setLocation()函数将两个整数作为参数。 try list.setLocation(0,0)另一个重载函数将Point对象作为参数。它将组件放在父组件的指定位置。

此外,您将JList放在JScrollPane中并将其添加到框架中。因此,请尝试为JScrollPane创建单独的对象,然后将其位置设置为0,0。

JScrollPane myScrollPane = new JScrollPane(list);
add(myScrollPane);
myScrollPane.setLocation(0,0);

以上代码段应该适合您。

答案 2 :(得分:0)

您可以对JFrame使用setLayout(null)。然后,您可以使用setBounds()方法根据需要在JFrame上设置列表位置。