【问题标题】:How do I display/print a string in a comma-separated array value?如何以逗号分隔的数组值显示/打印字符串?
【发布时间】:2021-06-18 15:28:56
【问题描述】:

我正在使用 JFrame 实现 ActionListener。我的目标是每次用户单击JButton 时,我都必须删除引号并将其替换为下一个引号(每个逗号)。然后最后一个报价将返回到第一个报价。每次单击按钮时都会显示一个报价。我被困在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;
        }
    }
}

【问题讨论】:

  • 你真的需要在这里做更多的工作。您甚至没有正确设置 JFrame。首先尝试让 JLabel 显示,然后您将可以更好地更改文本。
  • @markspace 遗憾的是,我必须使用 JFrame 作为我正在尝试做的分配提示的一部分。

标签: java arrays swing actionlistener jlabel


【解决方案1】:

基本上,这可以通过这个if条件逻辑来实现

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

每次点击按钮时,我们都会增加索引并显示数组元素,直到 counter >= quotes.length 然后将计数器返回到 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2017-01-09
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    相关资源
    最近更新 更多