在JButton按下时尝试更改图像

时间:2016-02-08 13:16:34

标签: java image jpanel jbutton imageicon

我正在尝试在按下任何JButton时更改面板上的图像。我已经设置了一个图像数组,需要它在按下后更改为数组中的下一个图像。这是我的代码:

public class SimpleGui implements ActionListener {
    JButton button = new JButton("Very Happy");
    JButton buttonTwo = new JButton("Happy");
    JButton buttonThree = new JButton("Neutral");
    JButton buttonFour = new JButton("Sad");
    JButton buttonFive = new JButton("Very Sad");
    static int[] ButtonArray = new int[5];
    private static String[] imageList = { "res/snow.jpg", "res/test-gm.jpg" };

    public int i = 0;

    public static void main(String[] args) throws FileNotFoundException {

        SimpleGui gui = new SimpleGui();
        gui.go();

        File file = new File("out.txt");
        FileOutputStream fos = new FileOutputStream(file);
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);

        ButtonArray[0] = 0;
        ButtonArray[1] = 0;
        ButtonArray[2] = 0;
        ButtonArray[3] = 0;
        ButtonArray[4] = 0;

    }

    public void go() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setBackground(Color.darkGray);

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        button.addActionListener(this);
        buttonTwo.addActionListener(this);
        buttonThree.addActionListener(this);
        buttonFour.addActionListener(this);
        buttonFive.addActionListener(this);
        panel.add(button);
        panel.add(buttonTwo);
        panel.add(buttonThree);
        panel.add(buttonFour);
        panel.add(buttonFive);

        frame.getContentPane().add(BorderLayout.EAST, panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(650, 600);
        frame.setVisible(true);

        ImageIcon image = new ImageIcon(imageList[i]);
        ImageIcon image1 = new ImageIcon(imageList[i + 1]);
        JLabel label = new JLabel("", image, JLabel.CENTER);
        JPanel panel2 = new JPanel(new BorderLayout());
        panel2.add(label, BorderLayout.CENTER);
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
        frame.add(panel2, BorderLayout.CENTER);

        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {

        if (event.getSource() == button) {
            ButtonArray[0] = 1;
            ButtonArray[1] = 0;
            ButtonArray[2] = 0;
            ButtonArray[3] = 0;
            ButtonArray[4] = 0;
            System.out.println("Very Happy");

        }
        // buttonTwo = (JButton) event.getSource();
        if (event.getSource() == buttonTwo) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 1;
            ButtonArray[2] = 0;
            ButtonArray[3] = 0;
            ButtonArray[4] = 0;
            System.out.println("Happy");

        }
        // buttonThree = (JButton) event.getSource();
        if (event.getSource() == buttonThree) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 0;
            ButtonArray[2] = 1;
            ButtonArray[3] = 0;
            ButtonArray[4] = 0;
            System.out.println("Neutral");

        }
        // buttonFour = (JButton) event.getSource();
        if (event.getSource() == buttonFour) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 0;
            ButtonArray[2] = 0;
            ButtonArray[3] = 1;
            ButtonArray[4] = 0;
            System.out.println("Sad");

        }

        // buttonFive = (JButton) event.getSource();
        if (event.getSource() == buttonFive) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 0;
            ButtonArray[2] = 0;
            ButtonArray[3] = 0;
            ButtonArray[4] = 1;
            System.out.println("Very Sad");

        }

        // System.out.println(Arrays.toString(ButtonArray));
        // ImageIcon image = (imageList[i]);

    }

}

1 个答案:

答案 0 :(得分:1)

我真的不知道你的大部分代码应该做什么。所以相反,这是一个最小的例子,应该做你要问的事情:一个标签,两个按钮设置不同的图像到该标签。

$first_special_

请注意,这是使用Lambda函数(Java 8),但您可以对一个或多个“真正的”ImageIcon[] images = new ImageIcon[] { new ImageIcon("foo.gif"), new ImageIcon("bar.gif"), new ImageIcon("blub.gif") }; JFrame frame = new JFrame("Test"); frame.getContentPane().setLayout(new FlowLayout()); JLabel label = new JLabel(images[0]); frame.getContentPane().add(label); JButton button1 = new JButton("Image 1"); button1.addActionListener(e -> label.setIcon(images[0])); frame.getContentPane().add(button1); JButton button2 = new JButton("Image 2"); button2.addActionListener(e -> label.setIcon(images[1])); frame.getContentPane().add(button2); frame.pack(); frame.setVisible(true); 类执行相同的操作。重要的是你打电话给ActionListener;您的代码中似乎缺少此部分。

如果您希望循环浏览列表或图片数组,可以这样做:

label.setIcon(theImage)

这里使用了AtomicInteger index = new AtomicInteger(0); JButton buttonCycle = new JButton("Cycle"); buttonCycle.addActionListener(e -> label.setIcon(images[index.getAndIncrement() % images.length])); frame.getContentPane().add(buttonCycle); 所以我可以将它声明为局部变量并在lambda中使用它。如果你把它作为周围类的成员变量,你也可以使用常规AtomicInteger

int

要点是:创建一个计数器变量,每次调用该按钮时都将其递增,并将标签的图标设置为具有该计数的元素,模块是数组的大小。