0
What this application does: the user has to choose the right answer to the question. I have only put a question for now. After save the answer (is inside a . txt) he can see result.
And there’s the problem: by clicking on the result, it shows everything right (The first time, it shows null, I still can’t fix it). then I close the results window with the button close result (that appears and disappears with the setVisible) , only if I click see result again, he duplicate the values on the screen , but only on the screen, txt is saved all right. I wish the user could click as many times as he wanted on boot see result and only errors and correct questions appeared, without duplicating the values on the screen.
if I click see result 3 times:
how is saved in txt (all right)
package visao;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class TelaPainel extends JFrame {
private JPanel contentPane;
private JButton btVerResultado;
private JButton btFecharResultado;
private JButton btSalvarRespostas;
private ButtonGroup grupo;
private JRadioButton radioDois;
private JRadioButton radioUm;
public TelaPainel() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(null);
setContentPane(contentPane);
btVerResultado = new JButton("ver resultado");
btVerResultado.setBounds(236, 156, 121, 23);
contentPane.add(btVerResultado);
btFecharResultado = new JButton("fechar resultado");
btFecharResultado.setBounds(236, 156, 121, 23);
btFecharResultado.setVisible(false);
contentPane.add(btFecharResultado);
btSalvarRespostas = new JButton("Salvar respostas");
btSalvarRespostas.setBounds(22, 156, 131, 23);
contentPane.add(btSalvarRespostas);
JLabel lblQueAnimal = new JLabel("Que animal \u00E9 esse?");
lblQueAnimal.setBounds(22, 45, 113, 14);
contentPane.add(lblQueAnimal);
radioUm = new JRadioButton("Cachorro");
radioUm.setBounds(26, 75, 109, 23);
contentPane.add(radioUm);
radioDois = new JRadioButton("Gato");
radioDois.setBounds(26, 113, 109, 23);
contentPane.add(radioDois);
grupo = new ButtonGroup();
grupo.add(radioUm);
grupo.add(radioDois);
setVisible(true);
}
public JButton getBtVerResultado() {
return btVerResultado;
}
public void setBtVerResultado(JButton brVerResultado) {
this.btVerResultado = brVerResultado;
}
public JButton getBtFecharResultado() {
return btFecharResultado;
}
public void setBtFecharResultado(JButton btFecharResultado) {
this.btFecharResultado = btFecharResultado;
}
public JButton getBtSalvarRespostas() {
return btSalvarRespostas;
}
public JRadioButton getRadioDois() {
return radioDois;
}
public void setRadioDois(JRadioButton radioDois) {
this.radioDois = radioDois;
}
public JRadioButton getRadioUm() {
return radioUm;
}
public void setRadioUm(JRadioButton radioUm) {
this.radioUm = radioUm;
}
public void setBtSalvarRespostas(JButton btSalvarRespostas) {
this.btSalvarRespostas = btSalvarRespostas;
}
}
package visao;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class TelaExibir extends JFrame {
private JPanel contentPane;
private JTextArea textArea;
public TelaExibir() {
setFocusable(true);
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 285, 164);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textArea = new JTextArea();
textArea.setLayout(null);
textArea.setBounds(0, 0, 200, 100);
JScrollPane scroll = new JScrollPane(textArea);
scroll.setBounds(0, 0, 285, 164);
contentPane.add(scroll);
setLocationRelativeTo(null);
setLocationRelativeTo(null);
setResizable(false);
}
public JTextArea getTextArea() {
return textArea;
}
public void setTextArea(JTextArea textArea) {
this.textArea = textArea;
}
}
package modelo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class AlunoArquivo {
public static File arquivo = new File("exibir.txt");
static DadoResultadoAluno dra = new DadoResultadoAluno();
private static String c1;
private static String c2;
public static List<String> Read(String Caminho) {
List<String> conteudo = new ArrayList<>();
try {
FileReader arq = new FileReader(arquivo);
BufferedReader lerArq = new BufferedReader(arq);
String linha = "";
try {
linha = lerArq.readLine();
while (linha != null) {
conteudo.add(linha);
linha = lerArq.readLine();
}
arq.close();
return conteudo;
} catch (IOException ex) {
System.out.println("Erro: Não foi possível ler o arquivo!");
}
} catch (FileNotFoundException ex) {
System.out.println("Erro: Arquivo não encontrado!");
}
return null;
}
public static boolean Write(String Caminho,String Texto){
try {
if (!arquivo.exists()) {
arquivo.createNewFile();
System.out.println("not");
} else {
System.out.println("Yes");
FileWriter arq = new FileWriter(arquivo, true);
PrintWriter gravarArq = new PrintWriter(arq);
gravarArq.println(Texto);
gravarArq.close();
}
return true;
}catch(IOException e){
System.out.println(e.getMessage());
return false;
}
}
public static void salvar(int countQtdAcertoPlanificacao, int countQtdErroPlanificacao, String ArqConfig){
dra.setQtdAcertoPlanificacao(countQtdAcertoPlanificacao);
dra.setQtdErroPlanificacao(countQtdErroPlanificacao);
String print = dra.getQtdAcertoPlanificacao() + ";" + dra.getQtdErroPlanificacao();
System.out.println("ultimo cont: "+countQtdErroPlanificacao + "");
if (AlunoArquivo.Write(ArqConfig, print)) {
System.out.println("Arquivo salvo com sucesso!");
} else {
System.out.println("Erro ao salvar o arquivo!");
}
}
public static void mostrar(String ArqConfig){
List<String> conteudo = AlunoArquivo.Read(ArqConfig);
for(String linha : conteudo) {
c1 = linha.split(";")[0];
c2 = linha.split(";")[1];
}
}
public static String getC1() {
return c1;
}
public static String getC2() {
return c2;
}
}
package modelo;
public class DadoResultadoAluno {
private int qtdAcertoPlanificacao = 0;
private int qtdErroPlanificacao = 0;
public int getQtdAcertoPlanificacao() {
return qtdAcertoPlanificacao;
}
public void setQtdAcertoPlanificacao(int qtdAcertoPlanificacao) {
this.qtdAcertoPlanificacao = qtdAcertoPlanificacao;
}
public int getQtdErroPlanificacao() {
return qtdErroPlanificacao;
}
public void setQtdErroPlanificacao(int qtdErroPlanificacao) {
this.qtdErroPlanificacao = qtdErroPlanificacao;
}
}
package controle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import modelo.AlunoArquivo;
import modelo.DadoResultadoAluno;
import visao.TelaExibir;
import visao.TelaPainel;
public class ControleTelaPainel implements ActionListener{
private static Integer countQtdErroPlanificacao = 0;
private static Integer countQtdAcertoPlanificacao = 0;
private String ArqConfig = "exibir.txt";
private DadoResultadoAluno dra;
private AlunoArquivo aa = new AlunoArquivo();
TelaExibir exibir = new TelaExibir();
private TelaPainel tp;
public ControleTelaPainel(TelaPainel tp) {
this.tp = tp;
this.tp.getBtFecharResultado().addActionListener(this);
this.tp.getBtVerResultado().addActionListener(this);
this.tp.getBtSalvarRespostas().addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()== tp.getBtFecharResultado()){
this.tp.getBtVerResultado().setVisible(true);
this.tp.getBtFecharResultado().setVisible(false);
exibir.setVisible(false);
}
else if(e.getSource() == tp.getBtSalvarRespostas()){
if(tp.getRadioUm().isSelected()){
countQtdAcertoPlanificacao++;
}else if(tp.getRadioDois().isSelected()){
countQtdErroPlanificacao++;
}
AlunoArquivo.salvar(countQtdAcertoPlanificacao, countQtdErroPlanificacao, ArqConfig);
}
else if(e.getSource()== tp.getBtVerResultado()){
AlunoArquivo.mostrar(ArqConfig);
exibir.getTextArea().append("Você fez " + AlunoArquivo.getC1() + " acertos" + " e " + AlunoArquivo.getC2() + " tentativas erradas");
exibir.getTextArea().append("\n\n");
exibir.setVisible(true);
this.tp.getBtFecharResultado().setVisible(true);
this.tp.getBtVerResultado().setVisible(false);
}
}
}
package controle;
import visao.TelaPainel;
public class Main {
public static void main(String[] args) {
ControleTelaPainel ctp = new ControleTelaPainel(new TelaPainel());
}
}
Where the class Imports??
– user28595
format the classes as well, by clicking on
{}
– user28595
I have placed the Imports!
– Gabriella
Gabriella, you did not find any errors to suggest in the other answer, I suggest you go back to it and read carefully, because you keep keeping the wrong codes that I already corrected in the answer: https://answall.com/a/263720/28595
– user28595
And I remember, but now it’s different. when I try to do it that way, the messages duplicate
– Gabriella
The null error you mention I have already corrected in the other code, reread the answer and you will see that I comment on it.
– user28595
ok, but in the other answer, by double-clicking the display button, it duplicates the same values that are happening in this code. there was also not corrected
– Gabriella
Because the problem there was another.
– user28595
but null, I just mentioned so you wouldn’t think that the main fluff was this. The main focus is the fact of duplicating the messages in the display. null I try to solve later
– Gabriella
Without resolving null, the code is not executable.
– user28595
the first time in fact it does not but when it rotates the second time, already it is
– Gabriella
Here it is not running at all. I suggest you edit because something must have been missing.
– user28595
I’ll take a look here, but these prints I took of it working. I’ll delete the txt and see from scratch
– Gabriella
error here, if I try to see answer first, after I saved twice, ie I answer twice , good wheel
– Gabriella
So your code has more errors than the question, which prevent you from properly simulating the problem.
– user28595
Let’s go continue this discussion in chat.
– user28595