gpt4 book ai didi

java - JFrame如何自行刷新?

转载 作者:行者123 更新时间:2023-12-01 11:57:49 25 4
gpt4 key购买 nike

这是我的代码

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)类 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()));

并且它有效。

那么这两段代码有什么区别呢?在第一个代码中,面板显然发生了变化,因为当我打印文本区域时,它与初始面板不同,但 JFrame 没有改变。

为什么第二个代码可以工作?

最佳答案

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 的当前需求使用这些数据呈现所需的结果,并且您可以使用这些数据来影响其他 View 。这就是Model-View-Controller的基本概念。范例。

关于java - JFrame如何自行刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28290391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com