Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按下按钮时更改图像-Java_Java_Swing_User Interface_Awt - Fatal编程技术网

按下按钮时更改图像-Java

按下按钮时更改图像-Java,java,swing,user-interface,awt,Java,Swing,User Interface,Awt,gui有五个按钮和一个默认图像。我想改变图像时,每个按钮被按下。我通过更新路径尝试了“粉红”,但我不知道每次需要做什么 public class PikminGui extends JFrame{ private JPanel buttons; private JLabel title; private JButton grow; private JButton pink; private JButton black; private JButt

gui有五个按钮和一个默认图像。我想改变图像时,每个按钮被按下。我通过更新路径尝试了“粉红”,但我不知道每次需要做什么

public class PikminGui extends JFrame{

    private JPanel buttons;
    private JLabel title;
    private JButton grow;
    private JButton pink;
    private JButton black;
    private JButton blue;
    private JButton red;
    private JButton yellow;
    private JFrame f;
    private JLabel label;
    private JPanel panel;
    public String path;

    public PikminGui() {
        createGui();
    }

    public void createGui() {

        path = "path-to-image1";

        f = new JFrame();
        panel = new JPanel(new BorderLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setLocation(200,200);
        f.setVisible(true);

        title = new JLabel("PIKMIN MAKER", JLabel.CENTER);
        panel.add(title,BorderLayout.NORTH);

        grow = new JButton("Grow");
        grow.setBorder(BorderFactory.createRaisedBevelBorder());
        panel.add(grow,BorderLayout.WEST);

        buttons = new JPanel();
        pink = new JButton("Pink");
        pink.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                path = "path-to-image2";
            }
        });
        black = new JButton("Black");
        yellow = new JButton("Yellow");
        blue = new JButton("Blue");
        red = new JButton("Red");
        buttons.setLayout(new FlowLayout());
        buttons.add(pink);
        buttons.add(black);
        buttons.add(yellow);
        buttons.add(blue);
        buttons.add(red);
        panel.add(buttons,BorderLayout.SOUTH);

        File file = new File(path);
        BufferedImage image;

        try {
            image = ImageIO.read(file);
            label = new JLabel(new ImageIcon(image));
            panel.add(label, BorderLayout.CENTER );
            f.getContentPane().add(panel);  
            f.pack();
            f.add(panel);           
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

实现
ActionListener
使用
actionPerformed
获取单击事件并从中更改图像。在ActionListener中,加载新图像并将其应用于标签,因为它是iconnever,在运行时不要加载新图像