如何正确使用repaint()和revalidate()?

时间:2016-10-06 00:33:00

标签: java jpanel

我是JPanel的新手,并不太了解如何重置它。一旦选择了图片并且用户想要选择新图片,我希望它擦除旧的所选图片并粘贴新图片。但我不确定如何。我看过repaint()revalidate()使用了,但我显然不知道如何使用它。那我该怎么做呢?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;

public class Menu extends JFrame {
    JMenuBar menuBar;
    ButtonGroup pictureGroup, problemsGroup;
    BufferedImage picture1img, picture2img, picture3img;
    JMenu choiceOfThreePictures, numberOfProblems;
    JRadioButtonMenuItem picture1, picture2, picture3, fourProblems, nineProblems, sixteenProblems;
    // /Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture1.jpg
    // /Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture2.png
    // /Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture3.jpg

    public Menu() {
        // Create the menu bar.
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        // Create Picture choices on Menu Bar
        choiceOfThreePictures = new JMenu("Picture Choices");
        // Add Picture choices on Menu Bar
        menuBar.add(choiceOfThreePictures);
        // Create MenuItems onto Picture choices
        pictureGroup = new ButtonGroup();
        picture1 = new JRadioButtonMenuItem("Picture 1");
        picture2 = new JRadioButtonMenuItem("Picture 2");
        picture3 = new JRadioButtonMenuItem("Picture 3");
        // Add Picture Choices to Picutre choices menu
        choiceOfThreePictures.add(picture1);
        pictureGroup.add(picture1);
        choiceOfThreePictures.add(picture2);
        pictureGroup.add(picture2);
        choiceOfThreePictures.add(picture3);
        pictureGroup.add(picture3);

        // Create Number Of Problems on Menu Bar
        numberOfProblems = new JMenu("Number Of Problems");
        // Add Number Of problems on Menu Bar
        menuBar.add(numberOfProblems);
        // Create Menu Items onto Number Of problems
        problemsGroup = new ButtonGroup();
        fourProblems = new JRadioButtonMenuItem("4");
        nineProblems = new JRadioButtonMenuItem("9");
        sixteenProblems = new JRadioButtonMenuItem("16");
        // Add Number Of problems onto menu
        numberOfProblems.add(fourProblems);
        problemsGroup.add(fourProblems);
        numberOfProblems.add(nineProblems);
        problemsGroup.add(nineProblems);
        numberOfProblems.add(sixteenProblems);
        problemsGroup.add(sixteenProblems);

        // Start creating ActionListeners
        picture1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Working");
                try {
                    picture1img = ImageIO.read(new File("/Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture1.jpg"));
                    getContentPane().add(new JLabel(new ImageIcon(picture1img)));
                    revalidate();
                    repaint();
                    setVisible(true);

                } catch (IOException e) {
                    System.out.println("Couldn't find image.");
                }
            }
        });
        picture2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Working");
                try {
                    picture2img = ImageIO.read(new File("/Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture2.jpg"));
                    getContentPane().add(new JLabel(new ImageIcon(picture2img)));
                    revalidate();
                    repaint();
                    setVisible(true);
                } catch (IOException e) {
                    System.out.println("Couldn't find image.");
                }
            }
        });
        picture3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Working");
                try {
                    picture3img = ImageIO.read(new File("/Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture3.jpg"));
                    getContentPane().add(new JLabel(new ImageIcon(picture3img)));
                    revalidate();
                    repaint();
                    setVisible(true);

                } catch (IOException e) {
                    System.out.println("Couldn't find image.");
                }
            }
        });
    }

    public static void main(String[] args) {
        Menu mb = new Menu();
        mb.setSize(900, 700);
        mb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mb.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

你正在使用它很好,你甚至可以远程repaint()调用,因为重新验证更多。问题是,您添加新图片未从contentPane删除以前添加的图片,这就是它不切换的原因。

所以,如果你这样做

                    picture3img = ImageIO.read(new File("/Users/Administrator/Dropbox/Java/HW6Wilson/HW6Wilson/Picture3.jpg"));
                    getContentPane().removeAll(); // HERE IS IMPORTANT PART
                    getContentPane().add(new JLabel(new ImageIcon(picture3img)));
                    revalidate();

它会起作用

更好的方法是将单个标签保持为"图像显示"并在setIcon(icon)上使用JLabel来保证更改整个标签。