【发布时间】:2016-12-28 21:27:45
【问题描述】:
我不知道如何在 JFrame 中一一显示我的对象列表(无论参数如何)。我想用一个循环来做这件事。我想在单击按钮等后显示前 10 个元素和后 10 个元素。有什么想法吗?
【问题讨论】:
-
ActionListener 和 revalidate()...
标签: java arraylist jframe jpanel jbutton
我不知道如何在 JFrame 中一一显示我的对象列表(无论参数如何)。我想用一个循环来做这件事。我想在单击按钮等后显示前 10 个元素和后 10 个元素。有什么想法吗?
【问题讨论】:
标签: java arraylist jframe jpanel jbutton
我认为这对我不起作用...我已将 JList 中的列表添加到 JPanel 并在单击按钮后它会刷新,但是当我单击实际显示的 JList 元素时,它们将返回到第一个那些 - 从头开始......(repaint() 命令被注释了,因为我没有看到我的 JList 有任何变化)。当 remove() 和 revalidate() 命令也被注释时,我也有同样的效果......所以我不知道问题出在哪里......
public Window()
{
setTitle("Window");
setSize(600,600);
setResizable(false);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
JList<?> list = new JList<Object>(tmp_list.toArray());
listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
listPanel.add(new JScrollPane(list));
add(listPanel, BorderLayout.CENTER);
nextButton = new JButton("Next");
buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout());
buttonsPanel.add(nextButton);
add(buttonsPanel, BorderLayout.SOUTH);
nextButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == nextButton)
{
if(current_page < last_page)
{
current_page++;
refreshListPanel();
}
}
}
private void refreshListPanel()
{
listPanel.removeAll();
tmp_list = showCurrentPage(N, cars1);
JList<?> list = new JList<Object>(tmp_list.toArray());
listPanel.add(new JScrollPane(list));
listPanel.revalidate();
listPanel.repaint();
}
private List<Car> showCurrentPage(int n, List<Car> main_list)
{
List<Car> list = new ArrayList<Car>();
int counter = n*(current_page);
int size;
if(current_page == last_page && C%N != 0)
size = main_list.size()%n;
else
size = n;
for(int i = 0; i < size; i++)
{
list.add(main_list.get(counter + i));
}
return list;
}
【讨论】: