шпаргалки
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

162 lines
5.5 KiB

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package propertiesswing2;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author denis
*/
public class MainFrame extends JFrame{
private Properties property;
private File file;
private JTextField urlField;
private JTextField usernameField;
private JTextField passwordField;
public MainFrame(){
setTitle("Properties");
Container root = getContentPane();
root.setLayout(new BorderLayout(20, 20));
//Добавляем группу кнопок
root.add(createButtons(), BorderLayout.NORTH);
//Добавляем группу меток и текстовых полей
root.add(createDataArea());
setSize(800, 300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
private JPanel createButtons(){
JPanel panel = new JPanel(new FlowLayout());
JButton loadButton = new JButton("LOAD");
JButton saveButton = new JButton("SAVE");
panel.add(loadButton);
panel.add(saveButton);
loadButton.addActionListener(e -> {
loadProperties();
if(property!=null){
if (urlField!=null) urlField.setText(property.getProperty("URL", ""));
if (usernameField!=null) usernameField.setText(property.getProperty("username", ""));
if (passwordField!=null) passwordField.setText(property.getProperty("password", ""));
}
});
saveButton.addActionListener(e -> {
String url = urlField!=null ? urlField.getText() : "";
String username = usernameField!=null ? usernameField.getText() : "";
String password = passwordField!=null ? passwordField.getText() : "";
if(property!=null){
property.put("URL", url);
property.put("username", username);
property.put("password", password);
}
saveProperties();
});
return panel;
}
private JPanel createDataArea(){
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
JLabel urlLabel = new JLabel("URL:");
urlField = new JTextField(50);
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(urlLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
panel.add(urlField, constraints);
JLabel usernameLabel = new JLabel("username:");
usernameField = new JTextField(50);
constraints.gridx = 0;
constraints.gridy = 1;
panel.add(usernameLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
panel.add(usernameField, constraints);
JLabel passwordLabel = new JLabel("password:");
passwordField = new JTextField(50);
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(passwordLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 2;
panel.add(passwordField, constraints);
return panel;
}
private void loadProperties(){
property = new Properties();
if(file==null) file = new File("C:\\PropDir\\prop2.properties");
//Проверяем существование файла
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
if(file.canRead()){
try (FileReader reader = new FileReader(file)) {
property.load(reader);
} catch (FileNotFoundException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private void saveProperties(){
if(file==null) file = new File("C:\\PropDir\\prop2.properties");
if(!file.exists()) try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
if(file.canWrite()){
try (FileWriter writer = new FileWriter(file)) {
property.store(writer, null);
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}