如何 display/print 以逗号分隔的数组值中的字符串?

How do I display/print a string in a comma-separated array value?

我正在使用 JFrame 实现 ActionListener。我的目标是每次用户单击 JButton 时,我都必须删除引号并将其替换为下一个引号(每个逗号)。最后一个引号然后 return 回到第一个引号。每次单击按钮时都会显示报价。我一直在思考如何在 actionPerformed.

下做到这一点

这是我目前拥有的:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener {
    FlowLayout flow = new FlowLayout();
    JButton b = new JButton("Press to change fact");
    JLabel fact = new JLabel();
    String[] quotes = new String[]{"Quote1 Goes Here",
            "Quote2 Goes Here",
            "Quote3 Goes Here",
            "Quote 4 Goes Here",
            "Quote 5 Goes Here",
            "Quote 6 Goes Here",
    };

    int counter = 0;
    int MAX = 6;

    public JFacts() {
        super("RANDOM QUOTES");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(flow);
        add(b);
        // STRING QUOTES WILL DISPLAY HERE
        b.addActionListener(this);
    }

    public static void main(String[] args) {
        JFacts rFrame = new JFacts();
        rFrame.setSize(440, 100);
        rFrame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        counter++;
        if(counter == MAX) {
            counter = 0;
        }
    }
}

基本上用这个if条件逻辑就可以实现

              if (counter >= quotes.length) {
                    counter = 0;
                }

我们每次单击按钮都会增加索引并显示数组元素,直到 counter >= quotes.length 然后 return 计数器为 0,以便它可以重新开始

代码

b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                
                System.out.println(quotes[counter++]);
                //or
                fact.setText(quotes[counter++]);

                if (counter >= quotes.length) {
                    counter = 0;
                }


            }
        });
    }

完整代码

public class Main extends JFrame implements ActionListener {
    FlowLayout flow = new FlowLayout();
    JButton b = new JButton("Press to change fact");
    JLabel fact = new JLabel();
    String[] quotes = new String[]{"Quote1 Goes Here",
            "Quote2 Goes Here",
            "Quote3 Goes Here",
            "Quote 4 Goes Here",
            "Quote 5 Goes Here",
            "Quote 6 Goes Here",
    };

    int counter = 0;
    int MAX = 6;

    public Main() {
        super("RANDOM QUOTES");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(flow);
        add(b);
        add(fact);
        fact.setText(quotes[0]);
        // STRING QUOTES WILL DISPLAY HERE
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {




                fact.setText(quotes[counter++]);
//                System.out.println(quotes[counter++]);

                if (counter >= quotes.length) {
                    counter = 0;
                }


            }
        });
    }

    public static void main(String[] args) {
        Main rFrame = new Main();
        rFrame.setSize(440, 100);
        rFrame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}

一个简单的 output 来测试程序是否正常运行,您可以在 JLabel fact

上进行测试
Quote1 Goes Here
Quote2 Goes Here
Quote3 Goes Here
Quote 4 Goes Here
Quote 5 Goes Here
Quote 6 Goes Here
Quote1 Goes Here
Quote2 Goes Here
Quote3 Goes Here
Quote 4 Goes Here