根据在其他面板中找到的JPanel
点击,我无法更新我的JList
之一。我尝试过使用我在这里找到的代码:
repaint JPanel with every click at JList
但我还有问题。我在下面粘贴了我的代码:
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class SelectionFrame extends JFrame {
private BufferedImage backgroundImage;
private JList<Object> superClassList;
private JPanel contentPane;
private CharacterListPanel clp;
private CharacterDetailsPanel cdp;
private CardLayout card = new CardLayout();
protected SelectionFrame() {
// Create the background image
try {
backgroundImage = ImageIO.read(new File(GUIConfig.DEFAULT_GAME_BACKGROUND));
} catch (Exception e) {
e.printStackTrace();
}
// Set the title to the default
setTitle(GUIConfig.DEFAULT_GAME_TITLE);
// Set the frame background
contentPane = new JPanel(){
public void paintComponent(Graphics g) {
Image img = Toolkit.getDefaultToolkit().getImage(Gui.class.getResource("/others/background_1.png"));
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
};
contentPane.setBackground(new Color(238, 238, 238));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
// Add Character List Panel to JFrame
clp = new CharacterListPanel();
cdp = new CharacterDetailsPanel();
add(clp, BorderLayout.LINE_START);
add(cdp, BorderLayout.LINE_END);
// Grab its list and store it in super class list
superClassList = clp.getCharacterList();
// List for change in list
superClassList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
String selectedVal = superClassList.getSelectedValue().toString();
System.out.println(selectedVal);
cdp.setName(selectedVal);
repaint();
}
}
});
// Set the size of the frame to the default, center frame, hide top level and do not allow resizing
setSize(GUIConfig.DEFAULT_SELECTION_MENU_WIDTH, GUIConfig.DEFAULT_SELECTION_MENU_HEIGHT);
setLocation(GUIConfig.HALF_DIMENSION_WIDTH - (int) this.getSize().getWidth()/2, GUIConfig.HALF_DIMENSION_HEIGHT - (int) this.getSize().getHeight()/2);
setUndecorated(true);
setResizable(false);
pack();
// Show frame
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
// This sub class is used to display the list of playable characters. it will include the character name and character icon
class CharacterListPanel extends JPanel {
private JList<Object> characterList;
protected CharacterListPanel() {
// Create list to hold name and icon for characters
characterList = new JList<Object>();
// Set list details
characterList.setBorder(new LineBorder(Color.BLACK));
characterList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Map of characters
Map<Object, ImageIcon> characters = new HashMap<Object, ImageIcon>();
// Loop through default character names and retrieve character icon file paths
for(int i = 0; i < GUIConfig.DEFAULT_CHARACTER_NAMES.size(); i+=1) {
ImageIcon img = new ImageIcon(GUIConfig.DEFAULT_CHARACTER_SYMBOLS.get(GUIConfig.DEFAULT_CHARACTER_NAMES.get(i)));
characters.put(GUIConfig.DEFAULT_CHARACTER_NAMES.get(i), img);
}
// Add items to the list
characterList.setListData(GUIConfig.DEFAULT_CHARACTER_NAMES.toArray());
characterList.setCellRenderer(new IconListRenderer(characters));
// Add list to JPanel
add(characterList);
// Remove grey background of JPanel
setOpaque(false);
}
protected JList<Object> getCharacterList() {
return characterList;
}
}
// This sub class is used to display the character details card based on what the user has selected in the list
class CharacterDetailsPanel extends JPanel {
private String name = null;
protected CharacterDetailsPanel() {
System.out.println(getName());
if(getName() == null || getName().isEmpty()){
try {
BufferedImage img = ImageIO.read(new File(GUIConfig.DEFAULT_CHARACTER_DETAILS.get("Default")));
JLabel picLabel = new JLabel(new ImageIcon(img));
add(picLabel);
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
BufferedImage img = ImageIO.read(new File(GUIConfig.DEFAULT_CHARACTER_DETAILS.get(getName())));
JLabel picLabel = new JLabel(new ImageIcon(img));
add(picLabel);
} catch (IOException e) {
e.printStackTrace();
}
}
// Remove grey background of JPanel
setOpaque(false);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) {
SelectionFrame sf = new SelectionFrame();
}
}
在我上面的迭代中,代码与链接中的代码不同。原因是我在那里尝试了代码;我完全复制并粘贴并通过它工作,没有任何反应。我认为这是因为在他的情况下,它是一个直接添加到框架和面板的列表。在我的情况下,它是一个面板中的列表,另一个面板添加到JFrame
。就像我说的那样,上面的代码放弃了链接中找到的样本。相反,我尝试做的是创建面板将它们添加到框架,并通过更改其名称值编辑每次发生单击时我想要更改的面板; name值用于获取正确的文件。
答案 0 :(得分:0)
调用repaint()
对您的情况没有影响,因为在CharacterDetailsPanel
的构造期间评估CharacterDetailsPanel
中设置的名称,因此永远不会执行您的else
分支。
相反,您可以将JLabel
存储在您将图像设置为成员的位置,并在调用setName
时更新图像。