Java 为什么当我试图无休止地更新JTextField时,程序会冻结?
我正在试着做一个计时器,但当我点击“Iniciar(start)”时,程序冻结了。这是给我带来问题的部分:Java 为什么当我试图无休止地更新JTextField时,程序会冻结?,java,swing,while-loop,Java,Swing,While Loop,我正在试着做一个计时器,但当我点击“Iniciar(start)”时,程序冻结了。这是给我带来问题的部分: ActionListener escucha = new ActionListener() { public void actionPerformed(ActionEvent e) { iniciar.setEnabled(false); pausar.setEnabled(true); reinici
ActionListener escucha = new ActionListener() {
public void actionPerformed(ActionEvent e) {
iniciar.setEnabled(false);
pausar.setEnabled(true);
reiniciar.setEnabled(true);
rb1.setEnabled(false);
rb2.setEnabled(false);
try {
while (true) {
milisegundos++;
if (milisegundos > 999) {
milisegundos = 0;
segundos++;
if (segundos > 59) {
segundos = 0;
minutos++;
if (minutos > 59) {
minutos = 0;
horas++;
}
}
}
if (milisegundos < 10) {
MS = "00"+milisegundos;
} else if (milisegundos < 100) {
MS = "0"+milisegundos;
} else {
MS = Integer.toString(milisegundos);
}
if (segundos < 10) {
S = "0"+segundos;
} else {
S = Integer.toString(segundos);
}
if (minutos < 10) {
M = "0"+minutos;
} else {
M = Integer.toString(minutos);
}
if (horas < 10) {
H = "0"+horas;
} else {
H = Integer.toString(horas);
}
cadena = H+":"+M+":"+":"+S+":"+MS;
tiempo.setText(cadena);
panel.repaint();
}
} catch (Exception w) {
w.printStackTrace();
}
}
};
iniciar.setText("Iniciar");
iniciar.addActionListener(escucha);
小编辑:发布所有程序代码(不完整):
公共课校长{
public static int horas = 0, minutos = 0, segundos = 0, milisegundos = 0;
public static String cadena = "00:00:00:00", MS, S, M, H;
public static boolean condición = true, alredySelected = false;
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel panel_botones = new JPanel();
JPanel panel_opciones = new JPanel();
JRadioButton rb1 = new JRadioButton();
JRadioButton rb2 = new JRadioButton();
ButtonGroup bg = new ButtonGroup();
JButton iniciar = new JButton();
JButton pausar = new JButton();
JButton reiniciar = new JButton();
Dimension minimumSize = new Dimension(400, 200);
JTextField tiempo = new JTextField(cadena);
JCheckBox siempreArriba = new JCheckBox();
tiempo.setFont(new Font("Arial", Font.BOLD, 45));
tiempo.setHorizontalAlignment(SwingConstants.CENTER);
tiempo.setEditable(false);
pausar.setEnabled(false);
reiniciar.setEnabled(false);
frame.setTitle("Cronómetro");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(minimumSize);
frame.setMinimumSize(minimumSize);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(principal.class.getResource("/icons/icono.png")));
panel.setLayout(new BorderLayout());
panel.add(panel_botones, BorderLayout.PAGE_END);
panel.add(panel_opciones, BorderLayout.PAGE_START);
panel.add(tiempo, BorderLayout.CENTER);
panel.add(siempreArriba, BorderLayout.EAST);
panel_botones.setLayout(new FlowLayout());
panel_botones.add(iniciar);
panel_botones.add(pausar);
panel_botones.add(reiniciar);
siempreArriba.setText("Siempre arriba");
siempreArriba.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if (siempreArriba.isSelected()) {
frame.setAlwaysOnTop(true);
}else{
frame.setAlwaysOnTop(false);
}
}
});
ActionListener escucha = new ActionListener() {
public void actionPerformed(ActionEvent e) {
iniciar.setEnabled(false);
pausar.setEnabled(true);
reiniciar.setEnabled(true);
rb1.setEnabled(false);
rb2.setEnabled(false);
try {
while (true) {
milisegundos++;
if (milisegundos > 999) {
milisegundos = 0;
segundos++;
if (segundos > 59) {
segundos = 0;
minutos++;
if (minutos > 59) {
minutos = 0;
horas++;
}
}
}
if (milisegundos < 10) {
MS = "00"+milisegundos;
} else if (milisegundos < 100) {
MS = "0"+milisegundos;
} else {
MS = Integer.toString(milisegundos);
}
if (segundos < 10) {
S = "0"+segundos;
} else {
S = Integer.toString(segundos);
}
if (minutos < 10) {
M = "0"+minutos;
} else {
M = Integer.toString(minutos);
}
if (horas < 10) {
H = "0"+horas;
} else {
H = Integer.toString(horas);
}
cadena = H+":"+M+":"+":"+S+":"+MS;
tiempo.setText(cadena);
panel.repaint();
}
} catch (Exception w) {
w.printStackTrace();
}
}
};
iniciar.setText("Iniciar");
iniciar.addActionListener(escucha);
pausar.setText("Pausar");
pausar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
condición = false;
pausar.setEnabled(false);
iniciar.setEnabled(true);
iniciar.setText("Reanudar");
}
});
reiniciar.setText("Reiniciar");
reiniciar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
rb1.setText("Cronómetro");
rb1.setSelected(true);
rb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alredySelected = false;
}
});
rb2.setText("Cuenta regresiva");
rb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (rb2.isSelected() && alredySelected==false) {
alredySelected = true;
String temp = JOptionPane.showInputDialog(frame, "Ingrese el número en segundos:");
while (temp.equals("")) {
JOptionPane.showMessageDialog(frame, "¡Debe ingresar un valor!", "Error", JOptionPane.ERROR_MESSAGE);
temp = JOptionPane.showInputDialog(frame, "Ingrese el número en segundos:");
}
horas = Integer.parseInt(temp);
}
} catch (java.lang.NumberFormatException x) {
JOptionPane.showMessageDialog(frame, "¡Solo debe ingresar números!", "Error", JOptionPane.ERROR_MESSAGE);
rb1.setSelected(true);
alredySelected = false;
} catch (java.lang.NullPointerException x) {
rb1.setSelected(true);
alredySelected = false;
}
}
});
bg.add(rb1);
bg.add(rb2);
panel_opciones.setLayout(new FlowLayout());
panel_opciones.add(rb1);
panel_opciones.add(rb2);
}
}
publicstaticinthoras=0,minutos=0,segudos=0,milisegudos=0;
公共静态字符串cadena=“00:00:00:00”,MS、S、M、H;
公共静态布尔condición=true,alredySelected=false;
公共静态void main(字符串[]args){
JFrame=新JFrame();
JPanel面板=新的JPanel();
JPanel panel_botones=新的JPanel();
JPanel panel_opciones=新的JPanel();
JRadioButton rb1=新的JRadioButton();
JRadioButton rb2=新的JRadioButton();
ButtonGroup bg=新建ButtonGroup();
JButton iniciar=新JButton();
JButton pausar=新JButton();
JButton reiniciar=新JButton();
尺寸最小尺寸=新尺寸(400200);
JTextField tiempo=新的JTextField(cadena);
JCheckBox siempreariba=新的JCheckBox();
tiempo.setFont(新字体(“Arial”,Font.BOLD,45));
tiempo.setHorizontalAlignment(旋转约束中心);
tiempo.setEditable(假);
pausar.setEnabled(false);
reiniciar.setEnabled(假);
frame.setTitle(“Cronómetro”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架设置尺寸(最小尺寸);
框架。设置最小尺寸(最小尺寸);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(principal.class.getResource(“/icons/icono.png”));
panel.setLayout(新的BorderLayout());
panel.add(panel\u botone,BorderLayout.PAGE\u END);
panel.add(panel_option,BorderLayout.PAGE_START);
panel.add(tiempo,BorderLayout.CENTER);
面板。添加(Siempreariba,BorderLayout.EAST);
panel_botones.setLayout(新的FlowLayout());
面板\u botones.添加(iniciar);
面板\u botones.add(pausar);
面板\u botones.add(reiniciar);
Siempreariba.setText(“Siempre arriba”);
siempreariba.addItemListener(新的ItemListener(){
公共无效itemStateChanged(ItemEvent e){
if(siempreArriba.isSelected()){
frame.setAlwaysOnTop(真);
}否则{
frame.setAlwaysOnTop(假);
}
}
});
ActionListener escucha=新ActionListener(){
已执行的公共无效操作(操作事件e){
INCIIAR.setEnabled(假);
pausar.setEnabled(真);
reiniciar.setEnabled(真);
rb1.setEnabled(false);
rb2.setEnabled(false);
试一试{
while(true){
milisegundos++;
如果(milisegundos>999){
milisegundos=0;
segundos++;
如果(segudos>59){
segundos=0;
minutos++;
如果(分钟数>59){
分钟数=0;
horas++;
}
}
}
如果(milisegudos<10){
MS=“00”+milisegudos;
}否则如果(米利塞贡多斯<100){
MS=“0”+milisegudos;
}否则{
MS=整数.toString(milisegudos);
}
如果(segudos<10){
S=“0”+segudos;
}否则{
S=整数.toString(segundos);
}
如果(分钟数<10){
M=“0”+分钟;
}否则{
M=整数.toString(分钟);
}
如果(horas<10){
H=“0”+小时;
}否则{
H=整数。toString(horas);
}
cadena=H+“:“+M+”:“+”:“+S+”:“+MS;
蒂恩波·塞特克斯(卡德纳);
panel.repaint();
}
}捕获(例外情况w){
w、 printStackTrace();
}
}
};
iniciar.setText(“iniciar”);
iniciar.addActionListener(escucha);
pausar.setText(“pausar”);
pausar.addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
condición=假;
pausar.setEnabled(false);
iniciar.setEnabled(真);
iniciar.setText(“重新命名”);
}
});
reiniciar.setText(“reiniciar”);
addActionListener(新ActionListener()){
已执行的公共无效操作(操作事件e){
}
});
rb1.setText(“Cronómetro”);
rb1.选择的值(真);
rb1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
alredySelected=false;
}
});
rb2.setText(“Cuenta regresiva”);
rb2.addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
试一试{
if(rb2.isSelected()&&alredySelected==false){
alredySelected=true;
String temp=JOptionPane.showInputDialog(框架,“Ingree el número en segundos:”);
while(温度等于(“”){
显示消息对话框(框架“Debe Ingrear un valor!”,“Error”,JOptionPane.Error\u消息);
temp=JOptionPane.showInputDialog(框架,“Ingree el número en segudos:”);
}
horas=整数.parseInt(temp);
}
}catch(java.lang.NumberFormatException x){
showMessageDialog(框架“Solo debe Ingrear números!”,“Error”,JOptionPane.Error\u消息)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.Toolkit;
import javax.swing.SwingConstants;
import java.awt.Font;
public static int horas = 0, minutos = 0, segundos = 0, milisegundos = 0;
public static String cadena = "00:00:00:00", MS, S, M, H;
public static boolean condición = true, alredySelected = false;
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel panel_botones = new JPanel();
JPanel panel_opciones = new JPanel();
JRadioButton rb1 = new JRadioButton();
JRadioButton rb2 = new JRadioButton();
ButtonGroup bg = new ButtonGroup();
JButton iniciar = new JButton();
JButton pausar = new JButton();
JButton reiniciar = new JButton();
Dimension minimumSize = new Dimension(400, 200);
JTextField tiempo = new JTextField(cadena);
JCheckBox siempreArriba = new JCheckBox();
tiempo.setFont(new Font("Arial", Font.BOLD, 45));
tiempo.setHorizontalAlignment(SwingConstants.CENTER);
tiempo.setEditable(false);
pausar.setEnabled(false);
reiniciar.setEnabled(false);
frame.setTitle("Cronómetro");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(minimumSize);
frame.setMinimumSize(minimumSize);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(principal.class.getResource("/icons/icono.png")));
panel.setLayout(new BorderLayout());
panel.add(panel_botones, BorderLayout.PAGE_END);
panel.add(panel_opciones, BorderLayout.PAGE_START);
panel.add(tiempo, BorderLayout.CENTER);
panel.add(siempreArriba, BorderLayout.EAST);
panel_botones.setLayout(new FlowLayout());
panel_botones.add(iniciar);
panel_botones.add(pausar);
panel_botones.add(reiniciar);
siempreArriba.setText("Siempre arriba");
siempreArriba.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if (siempreArriba.isSelected()) {
frame.setAlwaysOnTop(true);
}else{
frame.setAlwaysOnTop(false);
}
}
});
ActionListener escucha = new ActionListener() {
public void actionPerformed(ActionEvent e) {
iniciar.setEnabled(false);
pausar.setEnabled(true);
reiniciar.setEnabled(true);
rb1.setEnabled(false);
rb2.setEnabled(false);
try {
while (true) {
milisegundos++;
if (milisegundos > 999) {
milisegundos = 0;
segundos++;
if (segundos > 59) {
segundos = 0;
minutos++;
if (minutos > 59) {
minutos = 0;
horas++;
}
}
}
if (milisegundos < 10) {
MS = "00"+milisegundos;
} else if (milisegundos < 100) {
MS = "0"+milisegundos;
} else {
MS = Integer.toString(milisegundos);
}
if (segundos < 10) {
S = "0"+segundos;
} else {
S = Integer.toString(segundos);
}
if (minutos < 10) {
M = "0"+minutos;
} else {
M = Integer.toString(minutos);
}
if (horas < 10) {
H = "0"+horas;
} else {
H = Integer.toString(horas);
}
cadena = H+":"+M+":"+":"+S+":"+MS;
tiempo.setText(cadena);
panel.repaint();
}
} catch (Exception w) {
w.printStackTrace();
}
}
};
iniciar.setText("Iniciar");
iniciar.addActionListener(escucha);
pausar.setText("Pausar");
pausar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
condición = false;
pausar.setEnabled(false);
iniciar.setEnabled(true);
iniciar.setText("Reanudar");
}
});
reiniciar.setText("Reiniciar");
reiniciar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
rb1.setText("Cronómetro");
rb1.setSelected(true);
rb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alredySelected = false;
}
});
rb2.setText("Cuenta regresiva");
rb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (rb2.isSelected() && alredySelected==false) {
alredySelected = true;
String temp = JOptionPane.showInputDialog(frame, "Ingrese el número en segundos:");
while (temp.equals("")) {
JOptionPane.showMessageDialog(frame, "¡Debe ingresar un valor!", "Error", JOptionPane.ERROR_MESSAGE);
temp = JOptionPane.showInputDialog(frame, "Ingrese el número en segundos:");
}
horas = Integer.parseInt(temp);
}
} catch (java.lang.NumberFormatException x) {
JOptionPane.showMessageDialog(frame, "¡Solo debe ingresar números!", "Error", JOptionPane.ERROR_MESSAGE);
rb1.setSelected(true);
alredySelected = false;
} catch (java.lang.NullPointerException x) {
rb1.setSelected(true);
alredySelected = false;
}
}
});
bg.add(rb1);
bg.add(rb2);
panel_opciones.setLayout(new FlowLayout());
panel_opciones.add(rb1);
panel_opciones.add(rb2);
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.Toolkit;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Principal {
public int horas = 0, minutos = 0, segundos = 0, milisegundos = 0;
public String cadena = "00:00:00:00", MS, S, M, H;
public boolean condición = true, alredySelected = false;
private Timer timer;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Principal();
}
});
}
public Principal() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel panel_botones = new JPanel();
JPanel panel_opciones = new JPanel();
JRadioButton rb1 = new JRadioButton();
JRadioButton rb2 = new JRadioButton();
ButtonGroup bg = new ButtonGroup();
JButton iniciar = new JButton();
JButton pausar = new JButton();
JButton reiniciar = new JButton();
Dimension minimumSize = new Dimension(400, 200);
JTextField tiempo = new JTextField(cadena);
JCheckBox siempreArriba = new JCheckBox();
tiempo.setFont(new Font("Arial", Font.BOLD, 45));
tiempo.setHorizontalAlignment(SwingConstants.CENTER);
tiempo.setEditable(false);
tiempo.setColumns(11);
pausar.setEnabled(false);
reiniciar.setEnabled(false);
frame.setTitle("Cronómetro");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Principal.class.getResource("/icons/icono.png")));
panel.setLayout(new BorderLayout());
panel.add(panel_botones, BorderLayout.PAGE_END);
panel.add(panel_opciones, BorderLayout.PAGE_START);
panel.add(tiempo, BorderLayout.CENTER);
panel.add(siempreArriba, BorderLayout.EAST);
panel_botones.setLayout(new FlowLayout());
panel_botones.add(iniciar);
panel_botones.add(pausar);
panel_botones.add(reiniciar);
siempreArriba.setText("Siempre arriba");
siempreArriba.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (siempreArriba.isSelected()) {
frame.setAlwaysOnTop(true);
} else {
frame.setAlwaysOnTop(false);
}
}
});
timer = new Timer(1, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
milisegundos++;
if (milisegundos > 999) {
milisegundos = 0;
segundos++;
if (segundos > 59) {
segundos = 0;
minutos++;
if (minutos > 59) {
minutos = 0;
horas++;
}
}
}
if (milisegundos < 10) {
MS = "00" + milisegundos;
} else if (milisegundos < 100) {
MS = "0" + milisegundos;
} else {
MS = Integer.toString(milisegundos);
}
if (segundos < 10) {
S = "0" + segundos;
} else {
S = Integer.toString(segundos);
}
if (minutos < 10) {
M = "0" + minutos;
} else {
M = Integer.toString(minutos);
}
if (horas < 10) {
H = "0" + horas;
} else {
H = Integer.toString(horas);
}
cadena = H + ":" + M + ":" + S + ":" + MS;
tiempo.setText(cadena);
panel.repaint();
}
});
ActionListener escucha = new ActionListener() {
public void actionPerformed(ActionEvent e) {
milisegundos = 0;
iniciar.setEnabled(false);
pausar.setEnabled(true);
reiniciar.setEnabled(true);
rb1.setEnabled(false);
rb2.setEnabled(false);
timer.start();
// try {
// while (true) {
// milisegundos++;
// if (milisegundos > 999) {
// milisegundos = 0;
// segundos++;
// if (segundos > 59) {
// segundos = 0;
// minutos++;
// if (minutos > 59) {
// minutos = 0;
// horas++;
// }
// }
// }
//
// if (milisegundos < 10) {
// MS = "00" + milisegundos;
// } else if (milisegundos < 100) {
// MS = "0" + milisegundos;
// } else {
// MS = Integer.toString(milisegundos);
// }
//
// if (segundos < 10) {
// S = "0" + segundos;
// } else {
// S = Integer.toString(segundos);
// }
//
// if (minutos < 10) {
// M = "0" + minutos;
// } else {
// M = Integer.toString(minutos);
// }
//
// if (horas < 10) {
// H = "0" + horas;
// } else {
// H = Integer.toString(horas);
// }
//
// cadena = H + ":" + M + ":" + ":" + S + ":" + MS;
// tiempo.setText(cadena);
// panel.repaint();
//
// }
// } catch (Exception w) {
// w.printStackTrace();
// }
}
};
iniciar.setText("Iniciar");
iniciar.addActionListener(escucha);
pausar.setText("Pausar");
pausar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
condición = false;
pausar.setEnabled(false);
iniciar.setEnabled(true);
iniciar.setText("Reanudar");
timer.stop();
}
});
reiniciar.setText("Reiniciar");
reiniciar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.start();
}
});
rb1.setText("Cronómetro");
rb1.setSelected(true);
rb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alredySelected = false;
}
});
rb2.setText("Cuenta regresiva");
rb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (rb2.isSelected() && alredySelected == false) {
alredySelected = true;
String temp = JOptionPane.showInputDialog(frame, "Ingrese el número en segundos:");
while (temp.equals("")) {
JOptionPane.showMessageDialog(frame, "¡Debe ingresar un valor!", "Error", JOptionPane.ERROR_MESSAGE);
temp = JOptionPane.showInputDialog(frame, "Ingrese el número en segundos:");
}
horas = Integer.parseInt(temp);
}
} catch (java.lang.NumberFormatException x) {
JOptionPane.showMessageDialog(frame, "¡Solo debe ingresar números!", "Error", JOptionPane.ERROR_MESSAGE);
rb1.setSelected(true);
alredySelected = false;
} catch (java.lang.NullPointerException x) {
rb1.setSelected(true);
alredySelected = false;
}
}
});
bg.add(rb1);
bg.add(rb2);
panel_opciones.setLayout(new FlowLayout());
panel_opciones.add(rb1);
panel_opciones.add(rb2);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}