这是我的代码
public class ComboBoxDemo extends JFrame {
ArrayList<Common.DescriptionPanel> cartoon = new ArrayList<Common.DescriptionPanel>();
ArrayList<ImageIcon> image = new ArrayList<ImageIcon>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> description = new ArrayList<String>();
JComboBox combo = new JComboBox();
Common.DescriptionPanel panel = new Common.DescriptionPanel();
public static void main(String[] args) {
new Common.SetFrame(new ComboBoxDemo(), "Combo Box");
}
public ComboBoxDemo() {
addCartoon(new ImageIcon("c.jpg"), "Mario", "This is Mario");
addCartoon(new ImageIcon("d.jpg"), "Sonic", "This is Sonic");
addCartoon(new ImageIcon("e.jpg"), "Astro Boy", "This is Astro Boy");
for (int i = 0; i < cartoon.size(); i++) {
cartoon.get(i).setImage(image.get(i));
cartoon.get(i).setTitle(title.get(i));
cartoon.get(i).setDescription(description.get(i));
combo.addItem(title.get(i));
}
combo.setBackground(Color.white);
combo.setForeground(Color.blue);
combo.setSelectedItem(cartoon.get(0));
panel = cartoon.get(0);
add(combo, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel = cartoon.get(combo.getSelectedIndex());
pack();
System.out.println(panel.textArea.getText());
}
});
}
void addCartoon(ImageIcon image, String title, String description) {
cartoon.add(new Common.DescriptionPanel());
this.image.add(image);
this.title.add(title);
this.description.add(description);
}
}
,DescriptionPanel的代码是
public class DescriptionPanel扩展了JPanel {
private JLabel imageTitle = new JLabel();
public JTextArea textArea = new JTextArea();
public DescriptionPanel() {
imageTitle.setHorizontalAlignment(JLabel.CENTER);
imageTitle.setHorizontalTextPosition(JLabel.CENTER);
imageTitle.setVerticalTextPosition(JLabel.BOTTOM);
imageTitle.setFont(Common.SetFont.boldFont);
textArea.setLineWrap(true); //when one line doesn't fit, it will jump to next line automatically
/*
* The wrapStyleWord property is set to true (line 23) so that the line is wrapped
* on words rather than characters.
*/
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setFont(Common.SetFont.boldFont);
textArea.setForeground(Color.blue);
JScrollPane scrollpane = new JScrollPane(textArea);
setLayout(new GridLayout(1, 2));
add(imageTitle);
add(scrollpane);
}
public void setImage(ImageIcon image) {
imageTitle.setIcon(image);
}
public void setTitle(String title) {
imageTitle.setText(title);
}
public void setDescription(String description) {
textArea.setText(description);
}
}
当我重新选择组合框时,JFrame根本不会改变, 所以我替换代码
panel = cartoon.get(combo.getSelectedIndex());
代码
panel.setTitle(title.get(combo.getSelectedIndex()));
panel.setDescription(description.get(combo.getSelectedIndex()));
panel.setImage(image.get(combo.getSelectedIndex()));
它有效。
那么这两个代码的区别是什么? 在第一个代码中,面板显然会发生变化,因为当我打印出textarea时,它与初始面板不同,但JFrame不会改变。
为什么第二个代码可以工作?
答案 0 :(得分:1)
panel = cartoon.get(combo.getSelectedIndex());
这只是将panel
(它在内存中指向的内容)的引用更改为当前组合框位置中存储的内容。它不会影响panel
曾引用的内容。
panel.setTitle(title.get(combo.getSelectedIndex()));
panel.setDescription(description.get(combo.getSelectedIndex()));
panel.setImage(image.get(combo.getSelectedIndex()));
更改变量panel
引用的当前对象的属性。
由于之前已向框架panel
添加add(panel, BorderLayout.CENTER);
,因此会影响屏幕上的组件。
您永远不应该在此类组件中维护基于组件的数据。相反,你的组合框应该填充数据,它可以用来根据UI的当前需求渲染所需的结果,并且可以用来实现其他视图。这是Model-View-Controller范例的基本概念。