我无法根据在另一个面板中找到的JList
单击来更新我的一个JPanel
。我尝试使用我在这里找到的代码:
在JList上每次点击都重新粉刷JPanel
但我仍然有问题。我在下面粘贴了我的代码:
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
中。所以就像我说的,上面的代码放弃了链接中找到的示例。相反,我尝试做的是创建面板将它们添加到框架中,并通过更改其名称值来编辑每次单击时想要更改的面板;名称值用于获取正确的文件。
repaint()
在您的情况下没有效果,因为在构造CharacterDetailsPanel
期间会评估CharacterDetailsPanel
中设置的名称,因此您的else
分支永远不会被执行。相反,您可以存储将映像设置为成员的JLabel
,并在调用 setName
时更新映像。