簡體   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