Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
import java.awt.BorderLayout;
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.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class SimpleGui extends JFrame implements ActionListener, WindowListener {
private final List<String> values = new ArrayList<String>();
private JPanel listPanel;
private JButton addButton;
private JButton closeButton;
public SimpleGui() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(this);
this.setTitle("Simple test program.");
this.setContentPane(createMainPanel());
this.pack();
queryFile();
}
private JPanel createMainPanel() {
listPanel = new JPanel();
listPanel.setPreferredSize(new Dimension(100, 200));
listPanel.setBorder(BorderFactory.createEtchedBorder());
JScrollPane pane = new JScrollPane(listPanel);
addButton = new JButton("Add");
addButton.addActionListener(this);
closeButton = new JButton("Quit");
closeButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEtchedBorder());
buttonPanel.add(addButton);
buttonPanel.add(closeButton);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(pane, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
return panel;
}
private void queryFile() {
JFileChooser chooser = new JFileChooser();
int retVal = chooser.showOpenDialog(this);
if (retVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
loadFromFile(file);
}
}
private void loadFromFile(File file) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = reader.readLine()) != null) {
values.add(line);
addInformation(line);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("Could not find file.");
} catch (IOException e) {
System.out.println("Something went wrong during fileparsing.");
}
}
private void writeToFile(File file) {
try {
PrintWriter writer = new PrintWriter(new FileOutputStream(file));
for (String name : values) {
writer.println(name);
}
writer.close();
} catch (FileNotFoundException e) {
System.err.println("Could not write to file.");
return;
}
}
private void addInformation(String name) {
values.add(name);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEtchedBorder());
panel.add(new JLabel("Name: " + name));
listPanel.add(panel);
this.pack();
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == addButton) {
String name = JOptionPane.showInputDialog("Bitte Namen eingeben: ");
addInformation(name);
} else if (source == closeButton) {
windowClosing(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
this.setVisible(false);
}
}
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
if (values.isEmpty()) {
return;
}
JFileChooser chooser = new JFileChooser();
int retVal = chooser.showSaveDialog(this);
if (retVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
writeToFile(file);
}
System.exit(0);
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public static void main(String[] args) {
final SimpleGui gui = new SimpleGui();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui.setVisible(true);
}
});
}
}
import java.awt.BorderLayout;
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.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Passwortmanager extends JFrame implements ActionListener, WindowListener {
private final List<String> values = new ArrayList<String>();
private JPanel listPanel;
private JButton addButton;
private JButton closeButton;
public Passwortmanager() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(this);
this.setTitle("Passwortmanager(Test)");
this.setContentPane(createMainPanel());
this.pack();
queryFile();
}
private JPanel createMainPanel() {
listPanel = new JPanel();
listPanel.setPreferredSize(new Dimension(400, 200));
listPanel.setBorder(BorderFactory.createEtchedBorder());
JScrollPane pane = new JScrollPane(listPanel);
addButton = new JButton("Hinzufügen");
addButton.addActionListener(this);
closeButton = new JButton("Schließen");
closeButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEtchedBorder());
buttonPanel.add(addButton);
buttonPanel.add(closeButton);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(pane, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
return panel;
}
private void queryFile() {
JFileChooser chooser = new JFileChooser();
int retVal = chooser.showOpenDialog(this);
if (retVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
loadFromFile(file);
}
}
private void loadFromFile(File file) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = reader.readLine()) != null) {
values.add(line);
addInformation(line, line, line);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("konnte die Datei nicht finden.");
} catch (IOException e) {
System.out.println("Beim laden der Datei ist ein Fehler aufgetreten");
}
}
private void writeToFile(File file) {
try {
PrintWriter writer = new PrintWriter(new FileOutputStream(file));
for (String name : values) {
writer.println(name);
}
writer.close();
} catch (FileNotFoundException e) {
System.err.println("Konnte nicht in die Datei schreiben.");
return;
}
}
private void addInformation(String name, String passwort, String website) {
values.add(name);
values.add(passwort);
values.add(website);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEtchedBorder());
panel.add(new JLabel("Name: " + name + " | Passwort: " + passwort + " | Website: " + website));
listPanel.add(panel);
this.pack();
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == addButton) {
String name = JOptionPane.showInputDialog("Bitte Benutzernamen eingeben: ");
String passwort = JOptionPane.showInputDialog("Bitte Passwort eingeben: ");
String website = JOptionPane.showInputDialog("Bitte Namen der Website eingeben: ");
addInformation(name, passwort, website);
} else if (source == closeButton) {
windowClosing(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
this.setVisible(false);
}
}
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
if (values.isEmpty()) {
return;
}
JFileChooser chooser = new JFileChooser();
int retVal = chooser.showSaveDialog(this);
if (retVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
writeToFile(file);
}
System.exit(0);
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public static void main(String[] args) {
final Passwortmanager pm = new Passwortmanager();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
pm.setVisible(true);
}
});
}
}
public class DataContainer {
private String name;
private String passwd;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getPasswd() {
return passwd;
}
}
DataContainer container = values.get(1);
String line = container.getName() + ", " + container.getPasswd(),