cat_fich 0 Denunciar post Postado Junho 11, 2010 Boa tarde. Tenho umas dúvidas num código que escrevi. Supostamente deveria mostrar todos os radio buttons inseridos através do JOptionPane antes de ter sido escrita a palavra Sair. O programa faz isso, mas se o fechar, voltar a executar e escrever Sair na primeira iteração do JOptionPane todos os radio buttons inseridos anteriormente desaparecem. Penso que seja algo a ver com a serialização dos objectos. Se me puderem dizer o que estou a fazer mal agradecia muito. Já tentei descobrir por mim mas não consegui. Agradeço desde já toda a ajuda que me possam dar Código: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Escolha_multipla2.java * * Created on 11/Jun/2010, 17:45:47 */ package backoffice; import java.awt.Container; import java.awt.FlowLayout; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import javax.swing.ButtonGroup; import javax.swing.JOptionPane; import javax.swing.JRadioButton; /** * * @author */ public class Escolha_multipla2 extends javax.swing.JFrame implements Serializable { /** Creates new form Escolha_multipla2 */ public Escolha_multipla2() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jSeparator1 = new javax.swing.JSeparator(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); jLabel1.setText("Escolha múltipla"); jLabel2.setText("Modelo nº"); jTextField1.setColumns(5); jButton1.setText("Adicionar Nova Pergunta"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Anterior"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Cancelar"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addGap(18, 18, 18) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton3))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 184, Short.MAX_VALUE) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2) .addComponent(jButton3)) .addContainerGap()) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: ObjectOutputStream out; ObjectInputStream in; String resp = ""; JRadioButton rb1; Container c = getContentPane(); c.setLayout(new FlowLayout()); ButtonGroup bg = new ButtonGroup(); ArrayList<JRadioButton> arr = new ArrayList<JRadioButton>(); int n = 0; while (!resp.equals("Sair")) { resp = JOptionPane.showInputDialog("Introduza uma hipótese de resposta"); if (!resp.equals("Sair")) { rb1 = new JRadioButton(resp); arr.add(rb1); n++; } } for (int i = 0; i < n; i++) { bg.add(arr.get(i)); c.add(arr.get(i)); } try { out = new ObjectOutputStream(new FileOutputStream(System.getProperty("user.dir") + File.separator + "modelo.dat", true)); out.writeObject(c); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } Container c2 = getContentPane(); try { in = new ObjectInputStream(new FileInputStream(System.getProperty("user.dir") + File.separator + "modelo.dat")); c2 = (Container) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: new Criar_modelo().setVisible(true); this.setVisible(false); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Escolha_multipla2().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JSeparator jSeparator1; private javax.swing.JTextField jTextField1; // End of variables declaration } Compartilhar este post Link para o post Compartilhar em outros sites
Giovanni Ritchie 0 Denunciar post Postado Junho 12, 2010 esse código foi gerado pelo netBeans? Diga qual o enunciado do exercicio, talvez seja mais simples p mim explicar como fazer na mão... Abraço Compartilhar este post Link para o post Compartilhar em outros sites
cat_fich 0 Denunciar post Postado Junho 17, 2010 Muito obrigado pelo ajuda Giovanni Ritchie mas já consegui solucionar o problema. No entanto tenho outra dúvida exposta neste tópico Clique aqui. Se me puderes ajudar agradeço Compartilhar este post Link para o post Compartilhar em outros sites