java - JFrame如何自行刷新?

标签 java swing jframe copy

这是我的代码

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/

相关文章:

java - 如何从数据库生成用于 Hibernate 4 的 Java 类

java - 从数据库表中将值添加到 JTable 中

java swing : display file system in jtree, 以及如何打开该文件

java - 动态设置缓冲图像的大小

java - setVisible() 方法不适用于放置在 JPanel 上的 JButton

java - JBoss实例可以通信吗?

java - 从 Eclipse 使用资源文件夹部署 AWS Lambda 函数时出现问题

java - 将 ImageView 中的图像对齐到右上角

java - 以编程方式调用 JLabel 上的单击

java - 你在Java中什么时候用Frame或JFrame?