繁体   English   中英

显示多个JList到JFrame

[英]Displaying multiple JList to JFrame

我试图将许多JList放在仅显示2个名称的不同滚动窗格中。 我将mt列表存储在vectors的向量中,该结构表示一周中的每一天,每天都有多个“班次”。

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;

public class AutoScheduel extends JFrame{
private JList leftlist;
private JList leftlist1;
private JList tempList;
private int frameX = 50;
private int frameY = 50;
//private JList rightlist;
//private JButton movebutton;
static Employee Amanda = new Employee("Amanda",0,false); static Employee      Austin = new Employee("Austin",0,false); static Employee Conner = new Employee("Conner",0,false);
static Employee Faith = new Employee("Faith",0,false); static Employee Jospeh = new Employee("Jospeh",0,false); static Employee Lexi = new Employee("Lexi",0,false);
static Employee Matthew = new Employee("Matthew",0,false); static Employee     Samie = new Employee("Samie",0,false); static Employee Tanner = new     Employee("Tanner",0,false);
static Employee Valerie = new Employee("Valeire",0,false); static Employee     Will = new Employee("Will",0,false); static Employee Zack = new     Employee("Zack",0,false);
private static String[] employeesNames= {Amanda.getName(),Austin.getName(),Conner.getName(),Faith.getName(),Jospeh.getName(),
        Lexi.getName(),Matthew.getName(),Samie.getName(),Tanner.getName(),Valerie.getName(),Will.getName(),Zack.getName()};
private Vector<JList> weekday;
private Vector<JList> weekend;
private Vector<Vector<JList>> v;
public AutoScheduel(){
    super("Cambridge AutoSchedueler");
    setLayout(new FlowLayout());
    pack();
    setLocationRelativeTo(null);
    leftlist = new JList(employeesNames);
    leftlist.setVisibleRowCount(2);
    leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    //add(new JScrollPane(leftlist));

    leftlist1 = new JList(employeesNames);
    leftlist1.setVisibleRowCount(2);
    leftlist1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    //add(new JScrollPane(leftlist));

    v = new Vector<Vector<JList>>();
    weekday = new Vector<JList>();
    weekend = new Vector<JList>();
    for(int i=0;i<6;i++){
        weekday.addElement(leftlist);
    }
    for(int i=0;i<5;i++){
        weekend.add(leftlist1);
    }
    for(int i=0;i<5;i++){
        v.add(weekday);
    }
    v.add(weekend);
    v.add(weekend);

    tempList = new JList();
    leftlist.setPreferredSize(new Dimension(50,20));
    //leftlist.setLocation(50, 50);
    //add(new JScrollPane(leftlist));
    //add(new JScrollPane(v.get(1).get(3)));


    for(int i = 0; i<v.size();i++){
        for(int j = 0 ; i<v.get(i).size();i++){
            tempList = v.get(i).get(j);
            frameY += 40;
            tempList.setLocation(frameX,frameY);
            add(new JScrollPane(tempList));
        }
        frameX += 50;
    }




}
public static void main(String[] args){
   AutoScheduel go = new  AutoScheduel();
   go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   go.setSize(600, 600);

   go.setVisible(true);

}

}

目前,我得到了一个列表,其中显示了我想要的正确列表,但是在该列表之前只有点,表示彼此堆叠了许多列表。 我认为这是因为我正在使用流布局。 当我尝试使用不同的列表,然后将其添加到Jframe时,它可以工作,但是由于我需要34个列表,所以我只想使用for循环将其放入向量中。 如果有人可以帮助实现很棒的JFrame格式,我需要将其保存在一个数据结构中,在这里我仍然可以访问元素并设置为看起来像一周的可选列表。 提前致谢

您需要为要显示的每个JList创建新的JList和JList的新模型。 您当前的代码仅创建3个JList,然后尝试将其重复放置在GUI中,但这将不起作用。

例如,假设您要在一周的5个工作日中显示5个JList,其中一个显示员工姓名,则可以创建JList的集合,并在for循环中创建5个JList,每个JList都有模型。 另外,不要从Employee对象中提取名称,而是用实际的Employee对象填充JList并使用渲染器告诉JList仅显示员工名称。 例如:

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

@SuppressWarnings("serial")
public class Scheduler2 extends JPanel {
    private static final Employee2[] employees = { 
            new Employee2("Amanda", 0, false), 
            new Employee2("Austin", 0, false),
            new Employee2("Conner", 0, false), 
            new Employee2("Faith", 0, false), 
            new Employee2("Jospeh", 0, false),
            new Employee2("Lexi", 0, false), 
            new Employee2("Matthew", 0, false), 
            new Employee2("Samie", 0, false),
            new Employee2("Tanner", 0, false), 
            new Employee2("Valeire", 0, false), 
            new Employee2("Will", 0, false),
            new Employee2("Zack", 0, false) };
    private static final String[] WEEK_DAYS = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

    // associates each list with the day of the week
    private Map<String, JList<Employee2>> weekDayListMap = new HashMap<>();

    public Scheduler2() {
        // layout to hold 1 row, multiple columns
        setLayout(new GridLayout(1, 0));
        // renderer that shows employee names only
        EmployeeCellRenderer cellRenderer = new EmployeeCellRenderer();
        for (int i = 0; i < WEEK_DAYS.length; i++) {
            // new model for each week
            DefaultListModel<Employee2> listModel = new DefaultListModel<>();
            for (Employee2 employee : employees) {
                // fill the model
                listModel.addElement(employee);
            }
            // new list for each day of the week with model
            JList<Employee2> list = new JList<>(listModel);
            list.setVisibleRowCount(4);
            list.setCellRenderer(cellRenderer); // so shows name only
            String prototypeName = "aaaaaaaaaaaaaaaaaaa";
            list.setPrototypeCellValue(new Employee2(prototypeName, 0, false));
            weekDayListMap.put(WEEK_DAYS[i], list); // put in collection (if needed)
            JScrollPane scrollPane = new JScrollPane(list);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

            // if we want a border around the jscrollpane showing the day of the week
            JPanel container = new JPanel(new BorderLayout());
            container.add(scrollPane);
            container.setBorder(BorderFactory.createTitledBorder(WEEK_DAYS[i]));
            add(container);
        }
    }

    // JList renderer to display only names
    private class EmployeeCellRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            if (value == null) {
                value = "";
            } else {
                Employee2 empl = (Employee2) value;
                value = empl.getName();
            }
            return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        }
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Scheduler2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new Scheduler2());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

// you never gave us the employee class, so I had to create one of my own
class Employee2 {
    private String name;
    private int value;
    private boolean bool;

    public Employee2(String name, int value, boolean bool) {
        this.name = name;
        this.value = value;
        this.bool = bool;
    }

    public String getName() {
        return name;
    }

    public int getValue() {
        return value;
    }

    public boolean isBool() {
        return bool;
    }
}

关于,

您可以解释一下实际将列表添加到框架中的什么位置吗?

我将JList添加到JScrollPane。 然后,我可以添加JScrollPane的到主的JPanel中, this因为它是,但因为我想提出一个边框JScrollPane的为期一周的一天,我创建另一个JPanel的,所谓的容器,给它一个BorderLayout的,加JScrollPane到它,BorderLayout.CENTER(默认情况下),然后给这个外部的“包装器” JPanel一个带有星期几String的标题边框。 然后,将其添加到主JPanel中。 全部在这里:

// create JList w/ model
JList<Employee2> list = new JList<>(listModel);

// .....

// add JList to JScrollpane
JScrollPane scrollPane = new JScrollPane(list);


// .....

// create "wrapper" JPanel 
JPanel container = new JPanel(new BorderLayout());
container.add(scrollPane);  // and place JScrollPane into it
container.setBorder(BorderFactory.createTitledBorder(WEEK_DAYS[i]));

// add wrapper JPanel to the GUI
add(container);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM