Created
June 6, 2019 15:48
-
-
Save boogie666/f7a9927c4d13d02a0d5221c01499b730 to your computer and use it in GitHub Desktop.
Agenda Saracului
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.itschool.agendaSaracului; | |
import java.awt.Dimension; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.WindowEvent; | |
import java.awt.event.WindowListener; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.JTextField; | |
import javax.swing.SpringLayout; | |
public class Main { | |
private static final int TEXT_FIELD_SIZE = 20; | |
private static final int PREFERRED_COMPONENT_HEIGHT = 20; | |
private static final int PREFERRED_COMPONENT_WIDTH = 105; | |
public static void main(String[] args) throws IOException { | |
FileWriter f = new FileWriter(new File("contacts.csv"), true); | |
PrintWriter p = new PrintWriter(new BufferedWriter(f)); | |
JFrame app = new JFrame(); | |
app.addWindowListener(new WindowListener() { | |
@Override | |
public void windowOpened(WindowEvent e) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void windowIconified(WindowEvent e) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void windowDeiconified(WindowEvent e) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void windowDeactivated(WindowEvent e) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void windowClosing(WindowEvent e) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void windowClosed(WindowEvent e) { | |
p.flush(); | |
p.close(); | |
} | |
@Override | |
public void windowActivated(WindowEvent e) { | |
// TODO Auto-generated method stub | |
} | |
}); | |
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
SpringLayout layout = new SpringLayout(); | |
JPanel addContactPanel = new JPanel(); | |
addContactPanel.setLayout(layout); | |
JButton addButton = new JButton("Add"); | |
JLabel lastNameLabel = new JLabel("Nume:"); | |
lastNameLabel.setPreferredSize(new Dimension(PREFERRED_COMPONENT_WIDTH, PREFERRED_COMPONENT_HEIGHT)); | |
JTextField lastNameInput = new JTextField(TEXT_FIELD_SIZE); | |
lastNameInput.setPreferredSize(new Dimension(PREFERRED_COMPONENT_WIDTH, PREFERRED_COMPONENT_HEIGHT)); | |
JLabel firstNameLabel = new JLabel("Prenume:"); | |
firstNameLabel.setPreferredSize(new Dimension(PREFERRED_COMPONENT_WIDTH, PREFERRED_COMPONENT_HEIGHT)); | |
JTextField firstNameInput = new JTextField(TEXT_FIELD_SIZE); | |
firstNameInput.setPreferredSize(new Dimension(PREFERRED_COMPONENT_WIDTH, PREFERRED_COMPONENT_HEIGHT)); | |
JLabel phoneLabel = new JLabel("Telefon:"); | |
phoneLabel.setPreferredSize(new Dimension(PREFERRED_COMPONENT_WIDTH, PREFERRED_COMPONENT_HEIGHT)); | |
JTextField phoneInput = new JTextField(TEXT_FIELD_SIZE); | |
phoneInput.setPreferredSize(new Dimension(PREFERRED_COMPONENT_WIDTH, PREFERRED_COMPONENT_HEIGHT)); | |
layout.putConstraint(SpringLayout.WEST, lastNameInput, 5, SpringLayout.EAST, lastNameLabel); | |
layout.putConstraint(SpringLayout.NORTH, firstNameLabel, 5, SpringLayout.SOUTH, lastNameLabel); | |
layout.putConstraint(SpringLayout.NORTH, firstNameInput, 5, SpringLayout.SOUTH, lastNameInput); | |
layout.putConstraint(SpringLayout.WEST, firstNameInput, 5, SpringLayout.EAST, firstNameLabel); | |
layout.putConstraint(SpringLayout.NORTH, phoneLabel, 5, SpringLayout.SOUTH, firstNameLabel); | |
layout.putConstraint(SpringLayout.NORTH, phoneInput, 5, SpringLayout.SOUTH, firstNameInput); | |
layout.putConstraint(SpringLayout.WEST, phoneInput, 5, SpringLayout.EAST, phoneLabel); | |
layout.putConstraint(SpringLayout.NORTH, addButton, 5, SpringLayout.SOUTH, phoneInput); | |
layout.putConstraint(SpringLayout.EAST, addButton, 0, SpringLayout.EAST, phoneInput); | |
addContactPanel.add(firstNameLabel); | |
addContactPanel.add(lastNameLabel); | |
addContactPanel.add(phoneLabel); | |
addContactPanel.add(firstNameInput); | |
addContactPanel.add(lastNameInput); | |
addContactPanel.add(phoneInput); | |
addContactPanel.add(addButton); | |
addButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String nume = lastNameInput.getText(); | |
String preNume = firstNameInput.getText(); | |
String telefon = phoneInput.getText(); | |
p.println(nume + ", " + preNume + ", " + telefon); | |
} | |
}); | |
app.add(addContactPanel); | |
app.pack(); | |
app.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment