Java - Añada un botón de Stop a la aplicación de NumeroPrimos.java para poder detenerla antes de terminar

 
Vista:

Añada un botón de Stop a la aplicación de NumeroPrimos.java para poder detenerla antes de terminar

Publicado por Marcos (5 intervenciones) el 07/03/2020 17:32:08
si me pueden ayudar se lo voy agradeser.
Añada un botón de Stop a la aplicación de NumeroPrimos.java para poder detenerla antes de terminar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.java8talleres;
 
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 
public class NumeroPrimos extends JFrame implements Runnable, ActionListener {
    Thread go;
    JLabel howManyLabel;
    JTextField howMany;
    JButton display;
    JTextArea primes;
 
    public NumeroPrimos() {
        super("Find Prime Numbers");
        setLookAndFeel();
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BorderLayout bord = new BorderLayout();
        setLayout(bord);
 
        howManyLabel = new JLabel("Quantity: ");
        howMany = new JTextField("400", 10);
        display = new JButton("Display primes");
        primes = new JTextArea(8, 40);
 
        display.addActionListener(this);
        JPanel topPanel = new JPanel();
        topPanel.add(howManyLabel);
        topPanel.add(howMany);
        topPanel.add(display);
        add(topPanel, BorderLayout.NORTH);
 
        primes.setLineWrap(true);
        JScrollPane textPane = new JScrollPane(primes);
        add(textPane, BorderLayout.CENTER);
 
        setVisible(true);
    }
 
    public void actionPerformed(ActionEvent event) {
        display.setEnabled(false);
        if (go == null) {
            go = new Thread(this);
            go.start();
        }
    }
 
    public void run() {
        int quantity = Integer.parseInt(howMany.getText());
        int numPrimes = 0;
        // candidate: the number that might be prime
        int candidate = 2;
        primes.append("First " + quantity + " primes:");
        while (numPrimes < quantity) {
            if (isPrime(candidate)) {
                primes.append(candidate + " ");
                numPrimes++;
            }
            candidate++;
        }
    }
 
    public static boolean isPrime(int checkNumber) {
        double root = Math.sqrt(checkNumber);
        for (int i = 2; i <= root; i++) {
            if (checkNumber % i == 0) {
                return false;
            }
        }
        return true;
    }
 
    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            // ignore error
        }
    }
 
    public static void main(String[] arguments) {
        new NumeroPrimos();
    }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Añada un botón de Stop a la aplicación de NumeroPrimos.java para poder detenerla antes de terminar

Publicado por Costeor (148 intervenciones) el 08/03/2020 19:47:22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 
public class NumeroPrimos extends JFrame implements Runnable, ActionListener {
 
    Thread go;
 
    JLabel howManyLabel;
 
    JTextField howMany;
 
    JButton display;
 
    JTextArea primes;
 
    JButton btnStop;
 
    volatile boolean stop = false;
 
    public NumeroPrimos() {
 
        super("Find Prime Numbers");
 
        setLookAndFeel();
 
        setSize(400, 300);
 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        BorderLayout bord = new BorderLayout();
 
        setLayout(bord);
 
 
        howManyLabel = new JLabel("Quantity: ");
 
        howMany = new JTextField("400", 10);
 
        display = new JButton("Display primes");
 
        primes = new JTextArea(8, 40);
 
        btnStop = new JButton("Stop");
 
 
        display.addActionListener(this);
 
        JPanel topPanel = new JPanel();
 
        topPanel.add(howManyLabel);
 
        topPanel.add(howMany);
 
        topPanel.add(display);
        topPanel.add(btnStop);
 
        btnStop.addActionListener(this);
        btnStop.setEnabled(false);
 
        add(topPanel, BorderLayout.NORTH);
 
        primes.setLineWrap(true);
 
        JScrollPane textPane = new JScrollPane(primes);
 
        add(textPane, BorderLayout.CENTER);
 
        setVisible(true);
 
    }
 
 
    public void actionPerformed(ActionEvent event) {
 
        if(event.getActionCommand().equals("Display primes")) {
            display.setEnabled(false);
            btnStop.setEnabled(true);
            stop = false;
            primes.setText("");
            if (go == null) {
                go = new Thread(this);
                go.start();
            }
        } else if(event.getActionCommand().equals("Stop")) {
            this.stop = true;
            display.setEnabled(true);
            btnStop.setEnabled(false);
            go = null;
        }
    }
 
 
    public void run() {
 
        int quantity = Integer.parseInt(howMany.getText());
 
        int numPrimes = 0;
 
        // candidate: the number that might be prime
        int candidate = 2;
 
        primes.append("First " + quantity + " primes:");
 
        while (numPrimes < quantity && !stop) {
            // vamos hacer mas lento esto ...
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (isPrime(candidate)) {
                primes.append(candidate + " ");
                numPrimes++;
            }
            candidate++;
        }
 
    }
 
 
    public static boolean isPrime(int checkNumber) {
 
        double root = Math.sqrt(checkNumber);
 
        for (int i = 2; i <= root; i++) {
 
            if (checkNumber % i == 0) {
 
                return false;
 
            }
 
        }
 
        return true;
 
    }
 
 
    private void setLookAndFeel() {
 
        try {
 
            UIManager.setLookAndFeel(
 
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
 
            );
 
        } catch (Exception exc) {
 
            // ignore error
 
        }
 
    }
 
 
    public static void main(String[] arguments) {
 
        new NumeroPrimos();
 
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar

gracias

Publicado por Marcos (5 intervenciones) el 08/03/2020 21:08:18
muchas gracias por la ayuda
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar