java swing jpanel jbutton jlabel

java - ¿Por qué mi JLabel no aparece?



swing jpanel (2)

El problema que tiene es que está bloqueando el subproceso de envío de eventos, evita que la interfaz de usuario se actualice o que se procesen eventos nuevos ...

Empieza aquí ...

for(int i = 0; i < 15; i++) { //... //Check to see if user has enetered anything // And is compounded here while(!answered) { Thread.sleep(duration); //... }

Claramente está pensando de manera procesal (como lo haría para un programa de consola), pero no es así como funcionan las GUI, las GUI están controladas por eventos, algo sucede en algún momento y usted responde.

Mi sugerencia es investigar Swing Timer , que le permitirá programar una llamada en algún momento, señalar en el futuro y realizar alguna acción cuando se active, que se puede utilizar para modificar la interfaz de usuario, ya que se ejecuta en el contexto del EDT.

Consulte Concurrencia en Swing y Cómo usar los temporizadores Swing para obtener más detalles.

También te recomiendo que CardLayout un vistazo a CardLayout , ya que podría ser más fácil cambiar entre diferentes vistas

Vea Cómo usar CardLayout para más detalles

Lo esencial

Trabajo mucho con el principio de "Código para interfaz no implementación" y el Model-View-Controller . Básicamente, esto alienta a separar y aislar la responsabilidad, por lo que un cambio en una parte no afectará negativamente a otra.

También significa que puede implementar implementaciones plug''n''play, desacoplando el código y haciéndolo más flexible.

Comience con lo básico, necesita algo que tenga texto (la pregunta), una respuesta correcta y algunas "opciones" (o respuestas incorrectas)

public interface Question { public String getPrompt(); public String getCorrectAnswer(); public String[] getOptions(); public String getUserResponse(); public void setUserResponse(String response); public boolean isCorrect(); }

Entonces, bastante básico. La pregunta tiene un aviso, una respuesta correcta, algunas respuestas incorrectas y puede administrar la respuesta del usuario. Para facilitar su uso, también tiene un método isCorrect

Ahora, necesitamos una implementación real. Este es un ejemplo bastante básico, pero es posible que tenga varias implementaciones diferentes e incluso podría incluir genéricos para el tipo de respuestas (que he asumido como String por razones de argumento)

public class DefaultQuestion implements Question { private final String prompt; private final String correctAnswer; private final String[] options; private String userResponse; public DefaultQuestion(String prompt, String correctAnswer, String... options) { this.prompt = prompt; this.correctAnswer = correctAnswer; this.options = options; } @Override public String getPrompt() { return prompt; } @Override public String getCorrectAnswer() { return correctAnswer; } @Override public String[] getOptions() { return options; } @Override public String getUserResponse() { return userResponse; } @Override public void setUserResponse(String response) { userResponse = response; } @Override public boolean isCorrect() { return getCorrectAnswer().equals(getUserResponse()); } }

Está bien, todo está bien, pero ¿qué hace esto realmente por nosotros? Bueno, sepa que puede crear un componente simple cuyo único trabajo es presentar la pregunta al usuario y manejar su respuesta ...

public class QuestionPane extends JPanel { private Question question; public QuestionPane(Question question) { this.question = question; setLayout(new BorderLayout()); JLabel prompt = new JLabel("<html><b>" + question.getPrompt() + "</b></html>"); prompt.setHorizontalAlignment(JLabel.LEFT); add(prompt, BorderLayout.NORTH); JPanel guesses = new JPanel(new GridBagLayout()); guesses.setBorder(new EmptyBorder(5, 5, 5, 5)); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 1; gbc.anchor = GridBagConstraints.WEST; List<String> options = new ArrayList<>(Arrays.asList(question.getOptions())); options.add(question.getCorrectAnswer()); Collections.sort(options); ButtonGroup bg = new ButtonGroup(); for (String option : options) { JRadioButton btn = new JRadioButton(option); bg.add(btn); guesses.add(btn, gbc); } add(guesses); } public Question getQuestion() { return question; } public class ActionHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { getQuestion().setUserResponse(e.getActionCommand()); } } }

Esto lo convierte en un buen componente reutilizable, uno que puede manejar un montón de preguntas y no importarle.

Ahora, necesitamos alguna forma de gestionar múltiples preguntas, ¡un cuestionario!

public class QuizPane extends JPanel { private List<Question> quiz; private long timeOut = 5; private Timer timer; private JButton next; private CardLayout cardLayout; private int currentQuestion; private JPanel panelOfQuestions; private Long startTime; public QuizPane(List<Question> quiz) { this.quiz = quiz; cardLayout = new CardLayout(); panelOfQuestions = new JPanel(cardLayout); JButton start = new JButton("Start"); start.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { currentQuestion = -1; nextQuestion(); timer.start(); } }); JPanel filler = new JPanel(new GridBagLayout()); filler.add(start); panelOfQuestions.add(filler, "start"); for (int index = 0; index < quiz.size(); index++) { Question question = quiz.get(index); QuestionPane pane = new QuestionPane(question); panelOfQuestions.add(pane, Integer.toString(index)); } panelOfQuestions.add(new JLabel("The quiz is over"), "last"); currentQuestion = 0; cardLayout.show(panelOfQuestions, "start"); setLayout(new BorderLayout()); add(panelOfQuestions); JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT)); next = new JButton("Next"); buttonPane.add(next); next.setEnabled(false); add(buttonPane, BorderLayout.SOUTH); next.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { nextQuestion(); } }); timer = new Timer(250, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (startTime == null) { startTime = System.currentTimeMillis(); } long duration = (System.currentTimeMillis() - startTime) / 1000; if (duration >= timeOut) { nextQuestion(); } else { long timeLeft = timeOut - duration; next.setText("Next (" + timeLeft + ")"); next.repaint(); } } }); } protected void nextQuestion() { timer.stop(); currentQuestion++; if (currentQuestion >= quiz.size()) { cardLayout.show(panelOfQuestions, "last"); next.setEnabled(false); // You could could loop through all the questions and tally // the correct answers here } else { cardLayout.show(panelOfQuestions, Integer.toString(currentQuestion)); startTime = null; next.setText("Next"); next.setEnabled(true); timer.start(); } } }

De acuerdo, esto es un poco más complicado, pero lo básico es: administra qué pregunta se le presenta actualmente al usuario, administra el tiempo y le permite al usuario navegar a la siguiente pregunta si así lo desea.

Ahora, es fácil perderse en los detalles ...

Esta parte del código en realidad configura la "vista" principal, usando un CardLayout

panelOfQuestions.add(filler, "start"); for (int index = 0; index < quiz.size(); index++) { Question question = quiz.get(index); QuestionPane pane = new QuestionPane(question); panelOfQuestions.add(pane, Integer.toString(index)); } panelOfQuestions.add(new JLabel("The quiz is over"), "last"); currentQuestion = 0; cardLayout.show(panelOfQuestions, "start");

El botón de start , la "pantalla de finalización" y cada QuestionPane individual se agregan al panelOfQuestions , que es administrado por un CardLayout , esto hace que sea fácil "voltear" las vistas según sea necesario.

Utilizo un método simple para pasar a la siguiente pregunta.

protected void nextQuestion() { timer.stop(); currentQuestion++; if (currentQuestion >= quiz.size()) { cardLayout.show(panelOfQuestions, "last"); next.setEnabled(false); // You could could loop through all the questions and tally // the correct answers here } else { cardLayout.show(panelOfQuestions, Integer.toString(currentQuestion)); startTime = null; next.setText("Next"); next.setEnabled(true); timer.start(); } }

Básicamente, esto incrementa un contador y verifica si nos hemos quedado sin preguntas o no. Si es así, deshabilita el botón Siguiente y muestra la "última" vista al usuario; de lo contrario, pasa a la vista de la siguiente pregunta y reinicia el temporizador de tiempo de espera.

Ahora, aquí viene la "magia" ...

timer = new Timer(250, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (startTime == null) { startTime = System.currentTimeMillis(); } long duration = (System.currentTimeMillis() - startTime) / 1000; if (duration >= timeOut) { nextQuestion(); } else { long timeLeft = timeOut - duration; next.setText("Next (" + timeLeft + ")"); } } });

El Swing Timer actúa como un pseudo loop, lo que significa que llamará al método actionPerformed de forma regular, tal como lo haría for o while, pero lo hace tan lejos que no bloquea el EDT.

Este ejemplo agrega un poco más de "magia", ya que actúa como un temporizador de cuenta regresiva, comprueba cuánto tiempo ha estado visible la pregunta para el usuario y presenta una cuenta regresiva hasta que pase automáticamente a la siguiente pregunta, cuando la duration es mayor o igual que timeOut (5 segundos en este ejemplo), llama al método nextQuestion

¿Pero cómo lo usas, preguntas? Crea una List de Question , crea una instancia del QuizPane de QuizPane y la agrega a otro contenedor que se muestra en la pantalla, por ejemplo ...

public class QuizMaster { public static void main(String[] args) { new QuizMaster(); } public QuizMaster() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } List<Question> quiz = new ArrayList<>(5); quiz.add(new DefaultQuestion("Bananas are:", "Yellow", "Green", "Blue", "Ping", "Round")); quiz.add(new DefaultQuestion("1 + 1:", "2", "5", "3", "An artificial construct")); quiz.add(new DefaultQuestion("In the UK, it is illegal to eat...", "Mince pies on Christmas Day", "Your cousin", "Bananas")); quiz.add(new DefaultQuestion("If you lift a kangaroo’s tail off the ground...", "It can’t hop", "It will kick you in the face", "Act as a jack")); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new QuizPane(quiz)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }

Y finalmente, porque sé que querrás uno, un ejemplo completamente ejecutable

import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.Timer; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.EmptyBorder; public class QuizMaster { public static void main(String[] args) { new QuizMaster(); } public QuizMaster() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } List<Question> quiz = new ArrayList<>(5); quiz.add(new DefaultQuestion("Bananas are:", "Yellow", "Green", "Blue", "Ping", "Round")); quiz.add(new DefaultQuestion("1 + 1:", "2", "5", "3", "An artificial construct")); quiz.add(new DefaultQuestion("In the UK, it is illegal to eat...", "Mince pies on Christmas Day", "Your cousin", "Bananas")); quiz.add(new DefaultQuestion("If you lift a kangaroo’s tail off the ground...", "It can’t hop", "It will kick you in the face", "Act as a jack")); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new QuizPane(quiz)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class QuizPane extends JPanel { private List<Question> quiz; private long timeOut = 5; private Timer timer; private JButton next; private CardLayout cardLayout; private int currentQuestion; private JPanel panelOfQuestions; private Long startTime; public QuizPane(List<Question> quiz) { this.quiz = quiz; cardLayout = new CardLayout(); panelOfQuestions = new JPanel(cardLayout); JButton start = new JButton("Start"); start.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { currentQuestion = -1; nextQuestion(); timer.start(); } }); JPanel filler = new JPanel(new GridBagLayout()); filler.add(start); panelOfQuestions.add(filler, "start"); for (int index = 0; index < quiz.size(); index++) { Question question = quiz.get(index); QuestionPane pane = new QuestionPane(question); panelOfQuestions.add(pane, Integer.toString(index)); } panelOfQuestions.add(new JLabel("The quiz is over"), "last"); currentQuestion = 0; cardLayout.show(panelOfQuestions, "start"); setLayout(new BorderLayout()); add(panelOfQuestions); JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT)); next = new JButton("Next"); buttonPane.add(next); next.setEnabled(false); add(buttonPane, BorderLayout.SOUTH); next.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { nextQuestion(); } }); timer = new Timer(250, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (startTime == null) { startTime = System.currentTimeMillis(); } long duration = (System.currentTimeMillis() - startTime) / 1000; if (duration >= timeOut) { nextQuestion(); } else { long timeLeft = timeOut - duration; next.setText("Next (" + timeLeft + ")"); next.repaint(); } } }); } protected void nextQuestion() { timer.stop(); currentQuestion++; if (currentQuestion >= quiz.size()) { cardLayout.show(panelOfQuestions, "last"); next.setEnabled(false); // You could could loop through all the questions and tally // the correct answers here } else { cardLayout.show(panelOfQuestions, Integer.toString(currentQuestion)); startTime = null; next.setText("Next"); next.setEnabled(true); timer.start(); } } } public interface Question { public String getPrompt(); public String getCorrectAnswer(); public String[] getOptions(); public String getUserResponse(); public void setUserResponse(String response); public boolean isCorrect(); } public class DefaultQuestion implements Question { private final String prompt; private final String correctAnswer; private final String[] options; private String userResponse; public DefaultQuestion(String prompt, String correctAnswer, String... options) { this.prompt = prompt; this.correctAnswer = correctAnswer; this.options = options; } @Override public String getPrompt() { return prompt; } @Override public String getCorrectAnswer() { return correctAnswer; } @Override public String[] getOptions() { return options; } @Override public String getUserResponse() { return userResponse; } @Override public void setUserResponse(String response) { userResponse = response; } @Override public boolean isCorrect() { return getCorrectAnswer().equals(getUserResponse()); } } public class QuestionPane extends JPanel { private Question question; public QuestionPane(Question question) { this.question = question; setLayout(new BorderLayout()); JLabel prompt = new JLabel("<html><b>" + question.getPrompt() + "</b></html>"); prompt.setHorizontalAlignment(JLabel.LEFT); add(prompt, BorderLayout.NORTH); JPanel guesses = new JPanel(new GridBagLayout()); guesses.setBorder(new EmptyBorder(5, 5, 5, 5)); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 1; gbc.anchor = GridBagConstraints.WEST; List<String> options = new ArrayList<>(Arrays.asList(question.getOptions())); options.add(question.getCorrectAnswer()); Collections.sort(options); ButtonGroup bg = new ButtonGroup(); for (String option : options) { JRadioButton btn = new JRadioButton(option); bg.add(btn); guesses.add(btn, gbc); } add(guesses); } public Question getQuestion() { return question; } public class ActionHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { getQuestion().setUserResponse(e.getActionCommand()); } } } }

Llamo a este método llamado check en una de mis clases abstractas, pero por alguna razón el JLabel (problema) que estoy agregando al JPanel (panel) no aparece. ¿Por qué está ocurriendo esto? Cualquier explicación, estoy usando los métodos de volver a pintar y validar, pero todavía no aparece nada.


Si usó Jframe para esta aplicación, simplemente verifique si agregó el panel al marco, acaba de agregar la etiqueta al panel, solo verifique si agregó el panel al Jframe, de lo contrario ganó no aparezca