学校项目直到午夜

School project until midnight

大家好,我必须用 javaits 做一个学校项目,比如保存杂志和一些烹饪食谱的数据库,每本杂志都是一个数组,烹饪食谱也是如此。我的问题是列出杂志和烹饪食谱因为我在 MessageDialog 上列出了它,我需要在其中列出它们:http://screenpresso.com/=1pPAg

这是我的代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import javax.swing.JTextField;
import javax.swing.JButton;


public class IntroduzirRevista extends JFrame {

private JPanel contentPane;
private JTextField textNome;
private JLabel lblEditora;
private JTextField textEditora;
private JLabel lblNRevista;
private JTextField textAno;
private JLabel lblNewLabel;
private JTextField textMes;
private JButton btnGuardar;

public static File revistas = new File("revistas.txt");
private JButton btnApagar;
private JButton btnSeguinte;
private JButton btnListarRevistas;
static String[][] revistas2 = new String[100][5];
int r;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                IntroduzirRevista frame = new IntroduzirRevista();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public IntroduzirRevista() {
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    setBounds(100, 100, 450, 275);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblGuardarRevista = new JLabel("Introduzir Revista");
    lblGuardarRevista.setFont(new Font("Tahoma", Font.BOLD, 15));
    lblGuardarRevista.setBounds(12, 13, 137, 16);
    contentPane.add(lblGuardarRevista);

    JLabel lblNome = new JLabel("Nome:");
    lblNome.setBounds(45, 53, 38, 16);
    contentPane.add(lblNome);

    textNome = new JTextField();
    textNome.setBounds(95, 50, 285, 22);
    contentPane.add(textNome);
    textNome.setColumns(10);

    lblEditora = new JLabel("Editora:");
    lblEditora.setBounds(45, 82, 45, 16);
    contentPane.add(lblEditora);

    textEditora = new JTextField();
    textEditora.setBounds(95, 79, 285, 22);
    contentPane.add(textEditora);
    textEditora.setColumns(10);

    lblNRevista = new JLabel("M\u00EAs:");
    lblNRevista.setBounds(45, 111, 38, 16);
    contentPane.add(lblNRevista);

    textAno = new JTextField();
    textAno.setBounds(95, 137, 285, 22);
    contentPane.add(textAno);
    textAno.setColumns(10);

    lblNewLabel = new JLabel("Ano:");
    lblNewLabel.setBounds(45, 140, 27, 16);
    contentPane.add(lblNewLabel);

    textMes = new JTextField();
    textMes.setBounds(95, 108, 285, 22);
    contentPane.add(textMes);
    textMes.setColumns(10);

    btnGuardar = new JButton("Guardar");
    btnGuardar.setBounds(45, 170, 101, 25);
    contentPane.add(btnGuardar);

    btnApagar = new JButton("Apagar");
    btnApagar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            JDialog.setDefaultLookAndFeelDecorated(true);
            //apagar o ficheiro
            int response = JOptionPane.showConfirmDialog(rootPane, "Deseja realmente apagar o artigo?", "Apagar artigo", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

            if (response == JOptionPane.NO_OPTION) {
            } else if (response == JOptionPane.YES_OPTION) {
                revistas.delete();
                textNome.setText(""); 
                textEditora.setText("");  
                textMes.setText("");
                textAno.setText("");
            }
        }
    });
    btnApagar.setBounds(244, 170, 136, 25);
    contentPane.add(btnApagar);

    btnSeguinte = new JButton("Novo");
    btnSeguinte.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //apaga o texto escrito anteriormente para introduzir o novo
            textNome.setText(""); 
            textEditora.setText("");  
            textMes.setText("");
            textAno.setText("");

        }
    });
    btnSeguinte.setBounds(45, 196, 101, 25);
    contentPane.add(btnSeguinte);

    btnListarRevistas = new JButton("Listar Revistas");
    btnListarRevistas.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //aqui le o ficheiro para poder mostrar todo o conteudo
            try {
                BufferedReader br = new BufferedReader(new FileReader(revistas.getAbsolutePath()));
                String conteudoLinha;

                while ((conteudoLinha = br.readLine()) != null) {
                    String[] texto = conteudoLinha.split(";");
                    JOptionPane.showMessageDialog(rootPane, "Conte�do completo da linha: "+conteudoLinha+"", "Todas as revistas", JOptionPane.INFORMATION_MESSAGE);
                }

                br.close();
                System.out.println();
                //mensagem de erro para se ouver problemas na cria��o do ficheiro
            } catch(IOException e) {
                System.out.println("ERRO - "+e.getMessage());
                JOptionPane.showMessageDialog(rootPane, "Erro na leitura dos ficheiros, por favor contacte o programador!", "Erro", JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }
        }
    });

    //bot�o guardar
    btnListarRevistas.setBounds(244, 196, 136, 25);
    contentPane.add(btnListarRevistas);

    btnGuardar.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent e) {

            if (textNome.getText().isEmpty()==false && textEditora.getText().isEmpty()==false && textMes.getText().isEmpty()==false && textAno.getText().isEmpty()==false) {
                //obter o texto escrito
                String nome = textNome.getText();
                String editora = textEditora.getText();
                int mes = Integer.parseInt(textMes.getText());
                int ano = Integer.parseInt(textAno.getText());

                //opera��o para guardar
                try {
                    BufferedWriter bw = new BufferedWriter(new FileWriter(revistas.getAbsolutePath(), true));
                    bw.write(nome+";"+editora+";"+mes+";"+ano+";");
                    bw.newLine();
                    bw.close();
                }catch (IOException e1) {
                    JOptionPane.showMessageDialog(rootPane, "Erro na cria��o do ficheiro!", "Erro", JOptionPane.ERROR_MESSAGE);
                }
                    JOptionPane.showMessageDialog(rootPane, "Ficheiro Gravado com sucesso!");
            }else{
                JOptionPane.showMessageDialog(rootPane, "Introduza a informa��o nos espa�os...", "Erro", JOptionPane.ERROR_MESSAGE);
                }
            }
    });
}

}

public void actionPerformed(ActionEvent arg0) {

        // Change the name to whatever you want
        JFrame frame2 = new JFrame("FRAME 2");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
        frame2.setLocationByPlatform(true);

        JPanel contentPane2 = new JPanel();
        contentPane2.setBackground(Color.DARK_GRAY);
        JButton hideButton = new JButton("Exit FRAME 2");
        JTextArea textArea = new JTextArea(5, 20);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setText("Text Example");
        // Hide or exit or quit, or add whatever you want to this frame
        contentPane2.add(hideButton);
        contentPane2.add(textArea);
        frame2.getContentPane().add(contentPane2);
        frame2.setSize(300, 300);
        frame2.setVisible(true);
    }