java - 初学者 GUI 幻灯片代码无法正常工作(可能是循环或按钮问题,不确定)

标签 java user-interface

我正在创建一个图片幻灯片,其中有一个按钮可以切换到下一张图像,并且应该循环播放它们。

我尝试为我的循环切换到 case 语句和嵌套 boolean 语句。

/*
 *
 */


import java.util.Scanner;
import javax.swing.*;

import java.awt.Color;
import java.awt.event.*;

public class SlideShow implements ActionListener {

    JFrame frame;
    JPanel picture;
    JLabel label, picture1;
    JButton button;
    JPanel contentPane;
    int x = 0;




    public SlideShow() {
        /* Create and set up the frame */
        frame = new JFrame("SlideShow");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* Create a content pane with a BoxLayout and empty borders */
        contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        contentPane.setBackground(Color.white);

        /* Create a label that shows a die face */
        picture1 = new JLabel(new ImageIcon("attachment_142650738.jpg"));
        picture1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        picture1.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        contentPane.add(picture1);

        button = new JButton("Next photo");
        button.setAlignmentX(JButton.CENTER_ALIGNMENT);
        button.addActionListener(this);
        contentPane.add(button);

        /* Add content pane to frame */
        frame.setContentPane(contentPane);

        /* Size and then display the frame. */
        frame.pack();
        frame.setVisible(true);
    }








    //ActionPerformed//
    public void actionPerformed(ActionEvent event) {
        String[] array = {
            "attachment_142650738.jpg", // 0
            "attachment_142650739.jpg", // 1
            "attachment_142650741.jpg", // 2
            "attachment_142650742.jpg" // 3     
        };


        x += 0;
        if (x == 1) {
            picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
        } else if (x == 2) {
            picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
        } else if (x == 3) {
            picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
        } else if (x == 4) {
            picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
        } else if (x == 5) {
            x = 0;
            picture1.setIcon(new ImageIcon("attachment_142650738.jpg"));
        }


    }




    //GUI//
    private static void runGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);

        SlideShow ok = new SlideShow();


    }


    public static void main(String[] args) {
        /* Methods that create and show a GUI should be
           run from an event-dispatching thread */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runGUI();
            }
        });
    }
}

当前没有错误,但按钮没有结果。

最佳答案

您将 SlideShow 类的成员 x 初始化为零 (0) - 这是不必要的,因为无论如何它都是隐式初始化的。

在方法 actionPerformed 中,您有这行代码

x += 0;

您没有更改x的值。因此它保持为零,因此不会执行任何条件语句。

关于java - 初学者 GUI 幻灯片代码无法正常工作(可能是循环或按钮问题,不确定),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56424631/

相关文章:

java - java中带有静态字段的接口(interface),用于共享 'constants'

java - setImageBitmap() 比 image.setImageDrawable()、image.setImageResource() 慢

java - GlassFish 3.1.1 损坏资源名称,然后提示找不到它

java - 如何将 jtextfield 中的输入严格限制为 double 值?

prototype - 使用 Tcl/Tk 对桌面应用程序进行原型(prototype)设计的优缺点

java - 在 Android 中使用谷歌地图在自定义信息窗口上动画

java:如何修复未经检查的转换警告

user-interface - 如何在 xamarin 表单中创建与给定 UX 相同的 UI?

multithreading - 如何从我自己的线程安全地修改 JavaFX GUI 节点?

java - 制作可滚动表格