简体   繁体   English

JFrame如何自我刷新?

[英]how does JFrame refresh itself?

this is my code 这是我的代码

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

}

} }

and the code of DescriptionPanel is 并且DescriptionPanel的代码是

public class DescriptionPanel extends JPanel { 公共类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);
}

} }

when I reselect the combobox, JFrame won't change at all, so i replace the code 当我重新选择组合框时,JFrame完全不会更改,因此我替换了代码

                panel = cartoon.get(combo.getSelectedIndex());

to the code 到代码

panel.setTitle(title.get(combo.getSelectedIndex()));
            panel.setDescription(description.get(combo.getSelectedIndex()));
            panel.setImage(image.get(combo.getSelectedIndex()));

and it works. 而且有效。

so what is the difference of these two code? 那么这两个代码有什么区别? In the first code, the panel apparently change, because when I print the textarea out, it is different from the initial panel, but JFrame doesn't change. 在第一个代码中,面板显然发生了变化,因为当我打印出textarea时,它与初始面板不同,但是JFrame却没有变化。

why the second code can work? 为什么第二个代码可以工作?

panel = cartoon.get(combo.getSelectedIndex());

This simply changes the reference of panel (what it's pointing to in memory) to what ever is stored within the current combobox position. 这只是将panel的引用(它指向内存中的内容)更改为当前组合框位置中存储的内容。 It does not effect what panel was once referencing. 它不会影响panel曾经引用的内容。

panel.setTitle(title.get(combo.getSelectedIndex()));
panel.setDescription(description.get(combo.getSelectedIndex()));
panel.setImage(image.get(combo.getSelectedIndex()));

Changes the properties of the current object that the variable panel is referencing. 更改变量panel正在引用的当前对象的属性。

Because you have previous added panel to the frame ( add(panel, BorderLayout.CENTER); , it will effect the component that is on the screen. 因为您先前已将panel添加到框架( add(panel, BorderLayout.CENTER);所以它将影响屏幕上的组件。

You should NEVER maintain component based data within this type of component. 您永远不要在这种类型的组件中维护基于组件的数据。 Instead, your combobox should be filled with data, which it can use to renderer a desired result, based on the current needs of the UI and which you can use to effect some other view. 相反,您的组合框应填充数据,可根据UI的当前需求将其用于呈现所需的结果,并可用于实现其他视图。 This is the basic concept of the Model-View-Controller paradigm. 这是“ 模型-视图-控制器”范例的基本概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM