我有一个使用BorderLayout Jlists EAST,WEST,CENTER和SOUTH的JPanel。当我从SOUTH JList删除所有元素(现在使其为空)并重新绘制UI时...从手动调用repaint(),将焦点更改为另一个窗口然后返回,或者最小化/最大化窗口,等...... JList从视图中消失。
因此,我无法将显示JLists的元素拖放到SOUTH JList中,因为它没有显示......非常烦人。
我该如何解决这个问题?
代码:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
private ExampleList westItems;
private ExampleList eastItems;
private ExampleList southItems;
private ExampleList centerItems;
public Main(){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel(new BorderLayout());
JPanel mainPane = new JPanel(new BorderLayout());
frame.setContentPane(contentPane);
westItems = new ExampleList();
eastItems = new ExampleList();
southItems = new ExampleList();
centerItems = new ExampleList();
mainPane.add(westItems, BorderLayout.WEST);
mainPane.add(eastItems, BorderLayout.EAST);
mainPane.add(southItems, BorderLayout.SOUTH);
mainPane.add(centerItems, BorderLayout.CENTER);
JButton addButton = new JButton("Add Items");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
for(int i = 0; i < 5; i++){
westItems.addItem(new String("West "+i));
eastItems.addItem(new String("East "+i));
southItems.addItem(new String("South "+i));
centerItems.addItem(new String("Center "+i));
}
}
});
JButton removeFromSouthButton = new JButton("Remove South Item");
removeFromSouthButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
// Remove the last element
int count = southItems.getModel().getSize();
if( count > 0 )
southItems.removeItemAt(0);
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(addButton);
buttonsPanel.add(removeFromSouthButton);
contentPane.add(mainPane, BorderLayout.CENTER);
contentPane.add(buttonsPanel, BorderLayout.SOUTH);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.repaint();
}
});
}
}
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
@SuppressWarnings("rawtypes")
public class ExampleList extends JList {
private static final long serialVersionUID = 1L;
private DefaultListModel<String> model;
@SuppressWarnings("unchecked")
public ExampleList() {
model = new DefaultListModel<String>();
setModel(model);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setVisibleRowCount(1);
setBorder(BorderFactory.createTitledBorder("Example List"));
setFixedCellWidth(150);
setFixedCellHeight(150);
}
public void removeAllElements(){
model.removeAllElements();
}
public void removeItem(String item) {
model.removeElement(item);
}
public void removeItemAt(int index) {
model.remove(index);
}
public void addItem(String item) {
model.addElement(item);
}
// End Mouse Listener Stuff
}
答案 0 :(得分:0)
由于没有人正确回答,我想出来了。如果您没有执行“setPreferredSize(new Dimension(width,height))”,那么一旦列表中没有其他项,它将只是width = 0和height = 0。如果您使用任何非零值调用setPreferredSize(),那么它将显示。
我做到了这一点并且有效:
southItems.setPreferredSize(new Dimension(150,150));