简体   繁体   中英

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

public class DescriptionPanel extends 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

                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.

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. It does not effect what panel was once referencing.

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.

Because you have previous added panel to the frame ( add(panel, BorderLayout.CENTER); , it will effect the component that is on the screen.

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. This is the basic concept of the Model-View-Controller paradigm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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