0
My problem is this, I’m developing a program that has several buttons that correspond to certain students, within each student I have personal information about and only this Student, I have a class that records everything in the same way only I wanted instead of I was doing every record class for each student I wanted that in each student he would pick up this class and if he had come from student X he would record in student X.txt if it was student Y recorded in student Y.txt
public class Gravação_Dados extends JFrame {
private JPanel contentPane;
private JTextPane textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Gravação_Dados frame = new Gravação_Dados();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Gravação_Dados() {
setLocationByPlatform(true);
setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Jo\u00E3o Gil\\workspace\\sala de aula\\pic\\Science-Classroom-icon.png"));
setTitle("Grava\u00E7\u00E3o Notas");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Detalhes/Notas");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
lblNewLabel.setBounds(10, 59, 89, 14);
contentPane.add(lblNewLabel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(100, 59, 317, 123);
contentPane.add(scrollPane);
textArea = new JTextPane();
scrollPane.setViewportView(textArea);
JButton btnNewButton = new JButton("Gravar");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
FileWriter writer= new FileWriter("C:\\Users\\João Gil\\workspace\\sala de aula\\pic\\notas.txt");
BufferedWriter bw=new BufferedWriter (writer);
textArea.write(bw);
bw.close();
textArea.setText("");
textArea.requestFocus();
dispose();
}
catch(Exception e1){
JOptionPane.showMessageDialog(null, e1);
}
}
});
btnNewButton.setBounds(293, 205, 89, 23);
contentPane.add(btnNewButton);
final JButton Abrir = new JButton("Abrir");
Abrir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Abrir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
FileReader reader=new FileReader("C:\\Users\\João Gil\\workspace\\sala de aula\\pic\\notas.txt");
BufferedReader br= new BufferedReader (reader);
textArea.read(br, null);
br.close();
textArea.requestFocus();
}
catch(Exception e1){
JOptionPane.showMessageDialog (null, e1);
}
}
});
}
});
Abrir.setBounds(168, 205, 89, 23);
contentPane.add(Abrir);
JButton Preciso_de_ajuda = new JButton("Preciso de Ajuda");
Preciso_de_ajuda.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Ajuda().setVisible(true);
}
});
Preciso_de_ajuda.setIcon(new ImageIcon("C:\\Users\\Jo\u00E3o Gil\\workspace\\sala de aula\\pic\\dica.png"));
Preciso_de_ajuda.setBackground(SystemColor.control);
Preciso_de_ajuda.setHorizontalAlignment(SwingConstants.LEFT);
Preciso_de_ajuda.setBounds(0, 0, 444, 23);
contentPane.add(Preciso_de_ajuda);
}
}
Basically I wanted when the user pressed the student button for example the program knew that I wanted to record this information in the corresponding file.txt. Here this edition this is the main menu:
public class PARTE2 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
PARTE2 frame = new PARTE2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PARTE2() {
setExtendedState(Frame.MAXIMIZED_BOTH);
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Jo\u00E3o Gil\\workspace\\sala de aula\\pic\\icons\\Science-Classroom-icon.png"));
setTitle("Planta da Sala de Aula");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton PC_1 = new JButton("PC 1");
PC_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno1_Magda().setVisible(true);
}
});
PC_1.setBounds(10, 586, 138, 84);
contentPane.add(PC_1);
JButton PC_2 = new JButton("PC 2");
PC_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno2_Carolina().setVisible(true);
}
});
PC_2.setBounds(10, 496, 138, 84);
contentPane.add(PC_2);
JButton PC_3 = new JButton("PC 3");
PC_3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno3_Pedro().setVisible(true);
}
});
PC_3.setBounds(10, 406, 138, 84);
contentPane.add(PC_3);
JButton PC_4 = new JButton("PC 4");
PC_4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno4_Alexandre ().setVisible(true);
}
});
PC_4.setBounds(10, 316, 138, 84);
contentPane.add(PC_4);
JButton PC_5 = new JButton("PC 5");
PC_5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno5_Tomas ().setVisible(true);
}
});
PC_5.setBounds(10, 226, 138, 84);
contentPane.add(PC_5);
JButton PC_6 = new JButton("PC 6");
PC_6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno6_Raul().setVisible(true);
}
});
PC_6.setBounds(10, 136, 138, 84);
contentPane.add(PC_6);
JButton PC_8 = new JButton("PC 8");
PC_8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno8_Jorge().setVisible(true);
}
});
JButton PC_7 = new JButton("PC 7");
PC_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Aluno7_Ruben().setVisible(true);
}
});
PC_7.setBounds(10, 46, 138, 84);
contentPane.add(PC_7);
PC_8.setBounds(269, 46, 200, 84);
contentPane.add(PC_8);
JButton PC_9 = new JButton("PC 9");
PC_9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno9_Joao().setVisible(true);
}
});
PC_9.setBounds(469, 46, 200, 84);
contentPane.add(PC_9);
JButton PC_10 = new JButton("PC 10");
PC_10.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno10_Miguel().setVisible(true);
}
});
PC_10.setBounds(669, 46, 200, 84);
contentPane.add(PC_10);
JButton PC_11 = new JButton("PC 11");
PC_11.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno11_Diogo().setVisible(true);
}
});
PC_11.setBounds(869, 46, 200, 84);
contentPane.add(PC_11);
JButton PC_12 = new JButton("PC 12");
PC_12.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno12_Miguel().setVisible(true);
}
});
PC_12.setBounds(1214, 46, 138, 84);
contentPane.add(PC_12);
JButton PC_13 = new JButton("PC 13");
PC_13.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno13_Francisco().setVisible(true);
}
});
PC_13.setBounds(1214, 136, 138, 84);
contentPane.add(PC_13);
JButton PC_14 = new JButton("PC 14");
PC_14.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno14_Ze().setVisible(true);
}
});
PC_14.setBounds(1214, 226, 138, 84);
contentPane.add(PC_14);
JButton PC_15 = new JButton("PC 15");
PC_15.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno15_Floriano().setVisible(true);
}
});
PC_15.setBounds(1214, 316, 138, 84);
contentPane.add(PC_15);
JButton PC_16 = new JButton("PC 16");
PC_16.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Aluno16_Ruben().setVisible(true);
}
});
PC_16.setBounds(1214, 406, 138, 84);
contentPane.add(PC_16);
JButton PC_17 = new JButton("PC 17");
PC_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Aluno17_Edmara().setVisible(true);
}
});
PC_17.setBounds(1214, 497, 138, 84);
contentPane.add(PC_17);
JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(SystemColor.menu);
menuBar.setBounds(0, 0, 1362, 21);
contentPane.add(menuBar);
JMenu ficheiro = new JMenu("Ficheiro ");
menuBar.add(ficheiro);
JMenuItem sair = new JMenuItem("Sair");
sair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] options = { "Sim", "Não" };
int selectedOption=JOptionPane.showOptionDialog(null, " Deseja realmente sair do programa?", "Planta Sala-Sair",
JOptionPane.INFORMATION_MESSAGE, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
if (selectedOption == JOptionPane.YES_OPTION) {
dispose();
}
}
});
sair.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
sair.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_MASK));
ficheiro.add(sair);
JMenu mnAjuda = new JMenu("Ajuda");
menuBar.add(mnAjuda);
JMenuItem precisoajuda = new JMenuItem("Preciso de Ajuda");
mnAjuda.add(precisoajuda);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("Sobre");
mntmNewMenuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Menu_Sobre().setVisible(true);
}
});
mnAjuda.add(mntmNewMenuItem_1);
JButton Secretaria = new JButton("Secret\u00E1ria");
Secretaria.setAlignmentX(Component.CENTER_ALIGNMENT);
Secretaria.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Relatorio_().setVisible(true);
}
});
Secretaria.setFont(new Font("Tahoma", Font.PLAIN, 22));
Secretaria.setBounds(404, 529, 554, 141);
contentPane.add(Secretaria);
}
}
That will give the window of the respective student:
public class Aluno7_Ruben extends JFrame {
protected static final File texto1 = null;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Aluno7_Ruben frame = new Aluno7_Ruben();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Aluno7_Ruben() {
setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Jo\u00E3o Gil\\workspace\\sala de aula\\pic\\icon\\Science-Classroom-icon.png"));
setTitle("PC7-Ruben Gato");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Detalhes/Notas:");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
lblNewLabel.setBounds(10, 106, 194, 23);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Localiza\u00E7\u00E3o da avaria:");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 17));
lblNewLabel_1.setBounds(10, 11, 169, 20);
contentPane.add(lblNewLabel_1);
File file = new File("C:\\Users\\João Gil\\workspace\\sala de aula\\pic\\notas.txt");
FileInputStream fis = null;
String texto = "";
try {
fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
texto += (char) content;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 128, 414, 85);
contentPane.add(scrollPane);
final JTextArea textArea1 = new JTextArea(texto);
scrollPane.setViewportView(textArea1);
textArea1.setEditable(false);
textArea1.setWrapStyleWord(true);
final JCheckBox Teclado = new JCheckBox ("Teclado");
Teclado.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Teclado.isSelected()){
try {
String texto_teclado = "O Teclado está danificado";
BufferedWriter writer = new BufferedWriter(
new FileWriter("C:\\Users\\João Gil\\workspace\\sala de aula\\pic\\notas.txt", true));
writer.write(texto_teclado);
writer.newLine();
textArea1.append(texto_teclado+"\n");
writer.close();
} catch (IOException e1) {
}
}
}
});
Teclado.setToolTipText("Selecionar caso esteja danificado");
Teclado.setBounds(6, 54, 94, 23);
contentPane.add(Teclado);
final JCheckBox Rato = new JCheckBox ("Rato");
Rato.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Rato.isSelected()){
try {
String texto_rato = "O Rato está danificado";
BufferedWriter writer = new BufferedWriter(
new FileWriter("C:\\Users\\João Gil\\workspace\\sala de aula\\pic\\notas.txt", true));
writer.write(texto_rato);
textArea1.append(texto_rato+"\n");
writer.newLine();
writer.close();
} catch (IOException e1) {
}
}
else{
}
}
});
Rato.setToolTipText("Selecionar caso esteja danificado");
Rato.setBounds(98, 54, 81, 23);
contentPane.add(Rato);
final JCheckBox Monitor = new JCheckBox ("Monitor");
Monitor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Monitor.isSelected()){
try {
String texto_monitor = "O Monitor está danificado";
BufferedWriter writer = new BufferedWriter(
new FileWriter("C:\\Users\\João Gil\\workspace\\sala de aula\\pic\\notas.txt", true));
writer.write(texto_monitor);
textArea1.append(texto_monitor+"\n");
writer.newLine();
writer.close();
} catch (IOException e1) {
}
}
else{
}
}
});
Monitor.setToolTipText("Selecionar caso esteja danificado");
Monitor.setBounds(181, 54, 96, 23);
contentPane.add(Monitor);
final JCheckBox Torre = new JCheckBox ("Torre");
Torre.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Torre.isSelected()){
try {
String texto_torre = "A Torre está danificada";
BufferedWriter writer = new BufferedWriter(
new FileWriter("C:\\Users\\João Gil\\workspace\\sala de aula\\pic\\notas.txt", true));
writer.write(texto_torre );
textArea1.append(texto_torre +"\n");
writer.newLine();
writer.close();
} catch (IOException e1) {
}
}
else{
}
}
});
Torre.setToolTipText("Selecionar caso esteja danificado");
Torre.setBounds(288, 54, 109, 23);
contentPane.add(Torre);
JButton Adicionar_notas = new JButton("Escrever novas notas");
Adicionar_notas.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FAZJA(evt);
}
});
Adicionar_notas.setBounds(137, 224, 159, 23);
contentPane.add(Adicionar_notas);
}
protected void FAZJA(ActionEvent evt) {
this.dispose();
new Gravação_Dados().setVisible(true);
}
}
@Meuchapeu do not understand why I edited my question I am Portuguese and not Brazilian so you do not have the right to correct things that are fine, so next try to help people instead of making corrections for everything and for nothing
– MD4tw
if I understand correctly your problem is in
write
and in thereader
that you wanted it to be dynamic? This is to create that path automatically?– jsantos1991
@Joaogil I’m sorry that the edition has displeased you, feel free to reverse it if it seems appropriate. For my part, however, I believe that it improved yes the question (by correctly formatting the code) and, although I am not versed in pt_PT, I see that one of the reviewers that approved that edition yeah, and if he didn’t find any problems in that issue I trust his judgment (feel free to disagree in the meantime).
– mgibsonbr
@mgibsonbr just to complete your comment, since this happened to me, http://meta.pt.stackoverflow.com/questions/1700/editar-alterar-conte%C3%Bado-da-resposta has here an equal problem that I took to the goal, so it can be more clarified...
– jsantos1991
The implementation must necessarily be that way, creating a file
.txt
per student? It would be better to use aSGBD
for persistence of information, or a fileXML
.– Renan Gomes
because from the beginning I try to write to txt documents because I don’t know how to use SGBD or XML files, but even so I have idea that this would be for example a variable called number for example and I would start and declare this variable in each student’s class when using the label class had a switch case, case number=1 writes to student 1.txt case number=2 writes to student 2.txt is it possible to do this somehow? @mxn
– MD4tw
@jsantos1991 yes I wanted the program to know where the data came from and then to save it in the right files
– MD4tw
@Joaogil I have understood what you want to do (I think), but I don’t know how to explain it to you, because I don’t know who calls that class
gravar_dados
, however I will draw up an answer to see if it helps you...– jsantos1991
new Aluno9_Joao().setVisible(true);
,new Aluno8_Jorge().setVisible(true);
,new Aluno5_Tomas ().setVisible(true);
. Please improve the object orientation of this business fast. This is awful. Do not copy and paste code! This way, you’re creating a monster and soon it will devour you!– Victor Stafusa