如何找到下一个素数在java中提出的gui标签

cu6pst1q  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(148)

我想找到下一个(更大的)素数将显示在标签上的图形用户界面,但下面的代码是不好的。也许问题出在d++,但是我想不出其他方法让循环继续下去。有人能帮忙吗?非常感谢。

public class PrimeNumber extends JFrame implements ActionListener {

    private JLabel label = new JLabel("2");
    private JButton button = new JButton("Next Prime");

    public PrimeNumber() {
        FlowLayout layout = new FlowLayout();
        this.setLayout(layout);
        label = new JLabel("2");
        button = new JButton("Next Prime");
        this.add(label);
        this.add(button);

        button.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int d = 2;

        do {
            String t = String.valueOf(d);
            label.setText(t);
            d++;

        }
        while (d % 2 != 0);
    }
}
public class TestPrimeNumber {
    public static void main(String args[]) {
        PrimeNumber frame = new PrimeNumber();
        frame.setVisible(true);
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题