Java 如何在JFrame/JPanel中显示列表并在单击按钮后刷新它?
我不知道如何在JFrame中显示对象列表,而不考虑参数。我想用一个循环来做这件事。我想在点击按钮后显示前10个元素和下10个元素。有什么想法吗?我想这对我不起作用。。。我已经在JList中向JPanel添加了一个列表,单击按钮后它会刷新,但当我单击实际显示的JList元素时,它们会返回到第一个元素-从一开始。。。repaint命令会被注释,因为使用它我看不到我的JList有任何更改。当remove和revalidate命令也被注释时,这些效果与我的效果相同。。。所以我不知道问题出在哪里Java 如何在JFrame/JPanel中显示列表并在单击按钮后刷新它?,java,arraylist,jframe,jpanel,jbutton,Java,Arraylist,Jframe,Jpanel,Jbutton,我不知道如何在JFrame中显示对象列表,而不考虑参数。我想用一个循环来做这件事。我想在点击按钮后显示前10个元素和下10个元素。有什么想法吗?我想这对我不起作用。。。我已经在JList中向JPanel添加了一个列表,单击按钮后它会刷新,但当我单击实际显示的JList元素时,它们会返回到第一个元素-从一开始。。。repaint命令会被注释,因为使用它我看不到我的JList有任何更改。当remove和revalidate命令也被注释时,这些效果与我的效果相同。。。所以我不知道问题出在哪里 publ
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;
}
ActionListener和重新验证。。。