当我在运行时添加元素时,似乎不能正确更新包裹。 当框架被重新验证时,它将继续在最后一行渲染新项目(甚至保留旧的换行符),但它不会继续并换行到新一行。
仅当框架被重新调整大小时才能重新包装列表(无效,验证和重绘不起作用)。
final JFrame frame = new JFrame();
final Vector<String> strings = new Vector<String>();
JList list = new JList(strings);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);
strings.add(" ------------------ add ------------------");
strings.add(" ------------------ add ------------------");
JButton button = new JButton("add");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
strings.add(" ------------------ add ------------------");
//frame.invalidate();
//frame.validate();
//frame.repaint();
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(list, BorderLayout.CENTER);
panel.add(button,BorderLayout.SOUTH);
frame.setSize(400,400);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);