gpt4 book ai didi

java - 将多个 JList 显示到 JFrame

转载 作者:行者123 更新时间:2023-12-02 02:57:29 26 4
gpt4 key购买 nike

我试图将许多 JList 放在仅显示 2 个名称的不同滚动 Pane 中。我将 mt 列表存储在 vector vector 中,该结构代表一周中的几天,每天有多个“类次”。

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 循环将它们放入我的 vector 中。如果任何人都可以帮助进行 JFrame 中的格式设置,那就太棒了,我需要将其保存在一个数据结构中,在该数据结构中我仍然可以访问元素并将其设置为具有可选列表的一周。提前致谢

最佳答案

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

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

enter image description here

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;
}
}
<小时/>

关于,

Can you expalin where do you actually add the list into the frame?

我将 JList 添加到 JScrollPane。然后,我可以将 JScrollPane 添加到主 JPanel,即 this,但由于我想在 JScrollPane 周围添加星期几的边框,所以我创建了另一个 JPanel,称为容器,给它一个 BorderLayout,向其中添加 JScrollPane,BorderLayout.CENTER(默认情况下),然后给这个外部“包装器”JPanel 一个带有星期字符串的标题边框。然后我将其添加到主 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);

关于java - 将多个 JList 显示到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42867271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com